Git Product home page Git Product logo

plugin-swift's Introduction

DEPRECATED

prettier/plugin-swift is deprecated. It hasn’t been updated since early 2018, and the Swift community seems to have moved towards Apple’s swift-format and Nick Lockwood’s SwiftFormat instead.

We hope to someday have plugins for one or both of these tools, but the Prettier team has no current plans to develop such an integration. If you do make something, let us know by opening an issue on the main Prettier repository, and we will consider adding a link to this README.


Prettier Swift

Swift Plugin for the Opinionated Code Formatter

Gitter Travis npm version code style: prettier Follow+Prettier+on+Twitter

What does it do?

⚠️ Warning ⚠️ This plugin might eat your homework in its early stage. It also depends on Swift's lib/Syntax which is in flux. Please stage or commit your code before performing any in-place formatting.

You can check out how it formatted Artsy's Eidolon.

Prerequisites

You need one of the following:

  1. A snapshot from March 30, 2018 or later,
  2. a recent manual build from Swift's master, or
  3. Swift 4.2 (once it's available as a beta or released completely).

Assuming you picked (1) please follow the installation instructions, which recommend updating your PATH at the end:

TOOLCHAIN=swift-DEVELOPMENT-SNAPSHOT-2018-03-30-a.xctoolchain
export PATH=/Library/Developer/Toolchains/${TOOLCHAIN}/usr/bin/:"${PATH}"

Alternatively you can pass the full path to the swiftc binary via the environment variable PRETTIER_SWIFT_SWIFTC:

TOOLCHAIN=swift-DEVELOPMENT-SNAPSHOT-2018-03-30-a.xctoolchain
export PRETTIER_SWIFT_SWIFTC=/Library/Developer/Toolchains/${TOOLCHAIN}/usr/bin/swiftc

Contributing

If you're interested in contributing to the development of Prettier for Swift, you can follow the CONTRIBUTING guide from Prettier, as it all applies to this repository too.

To test it out on a Swift file:

  • Clone this repository.
  • Run yarn.
  • Create a file called test.swift.
  • Run yarn prettier test.swift to check the output.

Install

yarn add --dev --exact prettier prettier/plugin-swift

Use

prettier --write "**/*.swift"

Maintainers


Marcel Jackwerth

plugin-swift's People

Contributors

azz avatar geektree0101 avatar j-f1 avatar mrcljx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

plugin-swift's Issues

Is this cool project alive?

Hello, I was glad to find a swift prettier package because I use VScode to edit my swift scripts
but it seems that this plugin only works with old versions of swift

will there be any updates on it?

Alive?

Is this project alive or dead?

Breaking Behavior on Operators

I haven't touched on breaking on operators except when dealing with as.

After going back and forth between two variants, I clearly find the first one easier to read:

let appViewController = auctionStoryboard.instantiateInitialViewController()
    as? AppViewController
let appViewController = auctionStoryboard.instantiateInitialViewController() as?
    AppViewController

I remembered that I read something related in the Python PEP8 style guide recently:

For decades the recommended style was to break after binary operators. But this can hurt readability in two ways: the operators tend to get scattered across different columns on the screen, and each operator is moved away from its operand and onto the previous line. Here, the eye has to do extra work to tell which items are added and which are subtracted:

# No: operators sit far away from their operands
income = (gross_wages +
          taxable_interest +
          (dividends - qualified_dividends) -
          ira_deduction -
          student_loan_interest)

To solve this readability problem, mathematicians and their publishers follow the opposite convention. Donald Knuth explains the traditional rule in his Computers and Typesetting series: "Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations" [3].

Following the tradition from mathematics usually results in more readable code:

# Yes: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

As this stands, I'd consider going with that recommendation as well, but I don't know whether this would have community backing.

Prevent `prettier-ignore` comment from disappearing

When a prettier-ignore comment is encountered, the node that it is attached to is copied. However it is expected that the range of that node includes the prettier-ignore comment.

This is not the case however, so the comment will disappear:

<node>let foo = [
  1
]</node> // prettier-ignore
<node>let foo = [
  1
]</node>

Potential stability issue

I have tried to run prettier on on our codebase and was not able to do so successfully.

The kind of errors were pretty similar to those mentioned in here.
After some searching around i stumbled upon this bug report, which seemed to explain the issues i was having.

In the comment section a libSyntax contributor stated:

I intentionally introduced null to represent missing node as a part of performance optimization.
For instance, every IdentifierExpr have optional DeclNameArguments (e.g. (x:y:)) node. Allocating it for every identifier expressions is definitely a waste of time and resource.

please do not use JSON representation, RawSyntax or SyntaxData directly. They are really implementation detail.

From what i could gather prettier uses swiftc -frontend -emit-syntax under the hood to obtain the JSON representation and then processes it.

Since this is not a officially supported API this might cause some problems down the road.

I thaught i should bring it up since its definitely something to be aware of.

Best regards,

Simon

Unhandled types

When running this on our project I discovered some types that aren't currently handled.

  • Backtick
  • ContinueStmt
  • DeferStmt
  • CompositionType
  • IsTypePattern

Error: libSyntax(UnknownDecl)

I've cloned this repo and tried to use prettier swift plugin but but I ran into the error. Here is some info:

$ echo $TOOLCHAIN
swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-20-a.xctoolchain
$ echo $PRETTIER_SWIFT_SWIFTC
/Users/username/Library/Developer/Toolchains/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-20-a.xctoolchain/usr/bin/swiftc
$ echo $PATH
/Users/username/Library/Developer/Toolchains/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-20-a.xctoolchain/usr/bin/:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS:/Users/username/Library/Android/sdk/platform-tools/:/Users/username/Library/Android/sdk/tools/
$ swift --version
Apple Swift version 4.1-dev (LLVM 260a172ffb, Clang cd84be6c42, Swift 04baf31321)
Target: x86_64-apple-darwin17.5.0
$ swiftc --version
Apple Swift version 4.1-dev (LLVM 260a172ffb, Clang cd84be6c42, Swift 04baf31321)
Target: x86_64-apple-darwin17.5.0
$ less accessors.swift
class A {
    func a() {
        return
    }
}
accessors.swift (END)
$ yarn prettier accessors.swift
yarn run v1.5.1
$ prettier --plugin=. --parser=swift accessors.swift

[error] accessors.swift: Error: libSyntax(UnknownDecl):⏎funca()⏎retur⏎⏎
[error]     at genericPrint (/Users/username/Development/Quest/plugin-swift/src/printer/generic.js:834:15)
[error]     at comments.printComments (/Users/username/Development/Quest/plugin-swift/src/printer/index.js:33:13)
[error]     at Object.printComments (/Users/username/Development/Quest/plugin-swift/node_modules/prettier/src/main/comments.js:1051:19)
[error]     at Object.module.exports [as print] (/Users/username/Development/Quest/plugin-swift/src/printer/index.js:31:14)
[error]     at genericPrint (/Users/username/Development/Quest/plugin-swift/node_modules/prettier/src/main/ast-to-doc.js:97:18)
[error]     at printGenerically (/Users/username/Development/Quest/plugin-swift/node_modules/prettier/src/main/ast-to-doc.js:34:13)
[error]     at FastPath.map (/Users/username/Development/Quest/plugin-swift/node_modules/prettier/src/common/fast-path.js:124:19)
[error]     at genericPrint (/Users/username/Development/Quest/plugin-swift/src/printer/generic.js:140:29)
[error]     at comments.printComments (/Users/username/Development/Quest/plugin-swift/src/printer/index.js:33:13)
[error]     at Object.printComments (/Users/username/Development/Quest/plugin-swift/node_modules/prettier/src/main/comments.js:1051:19)
[error]     at Object.module.exports [as print] (/Users/username/Development/Quest/plugin-swift/src/printer/index.js:31:14)
[error]     at genericPrint (/Users/username/Development/Quest/plugin-swift/node_modules/prettier/src/main/ast-to-doc.js:97:18)
[error]     at printGenerically (/Users/username/Development/Quest/plugin-swift/node_modules/prettier/src/main/ast-to-doc.js:34:13)
[error]     at FastPath.map (/Users/username/Development/Quest/plugin-swift/node_modules/prettier/src/common/fast-path.js:124:19)
[error]     at genericPrint (/Users/username/Development/Quest/plugin-swift/src/printer/generic.js:341:21)
[error]     at comments.printComments (/Users/username/Development/Quest/plugin-swift/src/printer/index.js:33:13)
error An unexpected error occurred: "Command failed.
Exit code: 2
Command: sh
Arguments: -c prettier --plugin=. --parser=swift accessors.swift
Directory: /Users/username/Development/Quest/plugin-swift
Output:
".
info If you think this is a bug, please open a bug report with the information provided in "/Users/username/Development/Quest/plugin-swift/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Somehow communicate Raw Text changes to Prettier

There are some syntax combinations that confuse libSyntax leading to a garbage AST.

The two main reasons are semicolons and braces, that can be removed safely:

foo(); // can be foo()
foo { (a) in a } // can be foo { a in a }

I scrub the AST once to detect those issues and if so, I clean them up and run swiftc again.

However now the text that was passed in by prettier does not match the actual code anymore and locations might be off. This is especially bad for the comment printer, which sometimes checks for newlines around nodes and it will access invalid locations.

So I need a way to communicate up to prettier that I did some preprocessing on the text.

Right now I have a local modification in prettier that finds a secret __text property that I set but this doesn't seem clean.

The only better option I can think of now is to have a new optional method on the parser.

text = parser.preprocess(text)
ast = parser.parse(text)

However that means that I need to parse the AST twice even if it was okay on the first step already.

@azz @vjeux You got any other ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.