Code Monkey home page Code Monkey logo

plugin-swift's Introduction

Prettier Banner

Opinionated Code Formatter

JavaScript · TypeScript · Flow · JSX · JSON
CSS · SCSS · Less
HTML · Vue · Angular
GraphQL · Markdown · YAML
Your favorite language?

Github Actions Build Status Github Actions Build Status Github Actions Build Status Codecov Coverage Status Blazing Fast
npm version weekly downloads from npm code style: prettier Follow Prettier on Twitter

Intro

Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.

Input

foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());

Output

foo(
  reallyLongArg(),
  omgSoManyParameters(),
  IShouldRefactorThis(),
  isThereSeriouslyAnotherOne(),
);

Prettier can be run in your editor on-save, in a pre-commit hook, or in CI environments to ensure your codebase has a consistent style without devs ever having to post a nit-picky comment on a code review ever again!


Documentation

Install · Options · CLI · API

Playground


Badge

Show the world you're using Prettiercode style: prettier

[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

Contributing

See CONTRIBUTING.md.

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

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

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?

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.

Unhandled types

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

  • Backtick
  • ContinueStmt
  • DeferStmt
  • CompositionType
  • IsTypePattern

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.