Skip to content

Add option to omit semicolons in output #12

Closed

Description

@iki

E.g. semicolons: true to use semicolons at the end of statements (default), and semicolons: false to not use semicolons.

Activity

  1. rogeliog commented on Jan 10, 2017

    @rogeliog
    Contributor

    @jlongster If you are both accepting PRs and want this feature, I would be more than happy to help with a PR for this one.

  2. jlongster commented on Jan 10, 2017

    @jlongster
    Member

    I'm open to it, but it gets problematic fast. When you don't use semicolons, you run into things like this where you are forced to put a semicolon:

    var x = 5
    
    ;[1, 2, 3].forEach(...)

    The first time this would be parsed correctly, but we would output:

    var x = 5
    
    [1, 2, 3].forEach(...)

    which is invalid JS. (at least, not what you want.) This means that in order to support semicolon-less JS we need to code in all the ASI rules so we can see where semicolons are important. I'm not sure I'm willing to take on that complexity just yet.

    Feel free to play around with it. Code is powerful and you might convince me. My preference for evolving projects is to take it slowly though, and it's hard to remove things later than to add them, so let's play with it over time and see what we can do. I'd like to keep the number of options low, but this is such a divisive issue that it could be worthwhile.

  3. gaelollivier commented on Jan 10, 2017

    @gaelollivier

    Have two suggestions:

    • what about making this the default (and only) behavior ? I agree configuration options should be avoided so why not considering making this the standard ?
    • how about auto-detecting depending on the existing code ? If it has semi-colons, output semi-colons, if not, do not output them ?
  4. swansontec commented on Jan 10, 2017

    @swansontec

    I use standard.js, which is a popular no-configuration style guide & linter. They insist on a semicolon-free style, and the only rule needed to make this work is to never start a line with (, [, or `.

    I would love to see standard.js and prettier be compatible one day.

  5. K-Leon commented on Jan 10, 2017

    @K-Leon

    I would love seeing this integrated - is currently the only show stopper for me.

    // Edit: Standard Packe had 200k+ Downloads last Month - i think it's something that matters for a lot of people in the JS Community

  6. bakkot commented on Jan 11, 2017

    @bakkot
    Collaborator

    It's worth mentioning that "the only rule needed to make this work" will get progressively more complex as the language evolves. For example, under the popular public fields proposals these two classes differ:

    class A {
      set
      foo(x){} // setter for 'foo'
    }
    
    class B {
      set; // uninitialized field named 'set'
      foo(x){} // method named 'foo'
    }

    (Note that babylon currently gets these edge cases wrong in a number of exciting ways.)

    Presumably a no-semi style would have you writing

    class B {
      set
      ;foo(x){}
    }

    So, keep in mind that the complexity cost comes not just now but also in the future. (I don't think this is well understood by people using this style.)

  7. tunnckoCore commented on Jan 11, 2017

    @tunnckoCore

    Huh. I found cool thing. Because I'm moving to Atom and just using ESLint instead of Standard, i just love the combo of Linter, Linter-ESLint + eslintrc, and atom prettier :D

    It is just a whole show, because I had semi: [2, always], but atom linter can autofix (on save) the fixable errors, so I'm still no writing semicolons. Between that phase prettier prittifies and changes single quotes to double quotes (eslint warns me for a milliseconds there is doublequotes) and Atom Linter (because eslintrc) converts them back to singlequotes and insert the semicolons.

    There has a bit of delay between them and it can be seen what does what.

    Cool. I'm moving back to semicolons because the AtomLinter.
    As I know SublimeLinter don't have support for autofix.

    edit: So in short. I also wanted that option, but it seems AtomLinter comes to the rescue and can be easily fixed if you use ESLint, not some "standards" or "semistandards" :)

  8. MoOx commented on Jan 11, 2017

    @MoOx
    Contributor

    Just have a simple question.
    If people write

    var x = "thing"
    
    [1].forEach(...)

    how do you know the person wanted to write

    var x = "thing";
    
    [1].forEach(...);

    and not

    var x = "thing"[1].forEach(...)

    My point is "how do we know?" because if we know, we can add or prepend a semicolon, so same thing right?

  9. dtinth commented on Jan 11, 2017

    @dtinth
    Contributor

    I tried piping the result of prettier into standard --fix and the result is quite promising: prettier-standard-formatter.

    // Before
    var x = 5;
    [1,2,3].forEach(console.log);
    
    // After
    var x = 5;
    [ 1, 2, 3 ].forEach(console.log)
    // Before
    var x = "thing"
    [1].forEach(console.log)
    
    // After
    var x = 'thing'[1].forEach(console.log)

    If the user doesn’t include a semicolon in front, these two lines gets squashed into the same line, clearing any ambiguity.

  10. wmertens commented on Jan 11, 2017

    @wmertens

    @MoOx There is only one way that your example will be parsed (so one canonical AST), and only two ways that prettier should output it:

    with semi:

    var x = "thing";
    
    [1].forEach(...);
    

    without semi:

    var x = "thing"
    
    ;[1].forEach(...)
    

    The rule is simply: remove semi at end of line, and insert semi at beginning of line, before array literal, parens block, or template string.

  11. MoOx commented on Jan 11, 2017

    @MoOx
    Contributor

    There is only one way that your example will be parsed (so one canonical AST), and only two ways that prettier should output it

    I know that. My idea is "what if people write this by mistake". I guess the response is "pebkac".

  12. wmertens commented on Jan 11, 2017

    @wmertens

    @bakkot Yes, indeed, in this case the semicolon injection needs to happen after the keywords set and get in classes if used as class field names. Indeed, this is extra maintenance. But is it a lot of maintenance? Will there be a lot of cases like this? Personally I'm skeptical.

  13. timdorr commented on Jan 11, 2017

    @timdorr

    @MoOx

    how do you know the person wanted to write

    Seems as simple as looking that there's a linebreak/whitespace there and assuming they intended to have those two tokens be separate. Basically:

    "thing" [1] // implicit semicolon
    "thing"
    [1] // implicit semicolon
    "thing"[1] // no implicit semicolon
  14. added
    status:needs discussion
    on Jan 11, 2017
  15. dtinth commented on Jan 11, 2017

    @dtinth
    Contributor

    @timdorr

    Seems as simple as looking that there's a linebreak/whitespace there

    However, that goes against the rules of JavaScript. A JavaScript engine will parse it as "thing"[1] no matter what. I don’t think a code formatter should treat "thing" [1] as "thing";[1] which alters the meaning of the code.

    @MoOx

    what if people write this by mistake

    The no-unexpected-multiline ESLint rule will warn people about this 😃.

    P.S. I think the implications of using a semicolon-free style has already been well studied and there are already linter rules to avoid making mistakes in both styles. I would suggest to focus the discussion more on the complexity incurred and implementation effort required.

  16. 23 remaining items

  17. tunnckoCore commented on Jan 20, 2017

    @tunnckoCore

    Omg. "with tabs". I can't understand why complexing the tools (not talking only specifically for prettier) when there always are workarounds with other tools and existing workflow? But yea, our different styles in that community kills us.

  18. rhengles commented on Jan 20, 2017

    @rhengles
    Contributor

    @tunnckoCore

    For a few reasons:

    • It's a simple change
    • It's more efficient
    • I personally believe that tabs are the correct way to indent code
    • I won't compromise for a workaround. I want it as a first-class option.

    I know I can't change what the majority of the community uses, but I cannot and will not adapt into it simply because it is what the majority uses, but only if someone manages to convince me it is a technically better option.

  19. sheerun commented on Mar 16, 2017

    @sheerun
    Contributor

    I'm pasting the link to my package I've created specifically to fix this issue, as this thread is the first on google when you search for "prettier semicolons": https://github.com/sheerun/prettier-standard

    I hope prettier will support no-semi rule soon enough, though!

  20. MadelineRitchie commented on May 3, 2017

    @MadelineRitchie

    For anyone finding this via Google, note that prettier supports this option now, per this PR:
    #1129

  21. alexgvozden commented on Jul 6, 2017

    @alexgvozden

    eslint.autoFixOnSave is a great option to autofix your code

  22. locked and limited conversation to collaborators on Jul 6, 2017
  23. j-f1 commented on Jan 12, 2018

    @j-f1
    Member

    @vjeux Should this be unlocked? The option has been added.

  24. reopened this on Jan 12, 2018
  25. unlocked this conversation on Jan 12, 2018
  26. j-f1 commented on Jan 12, 2018

    @j-f1
    Member

    This has been added in #1129.

  27. added
    locked-due-to-inactivity
    on Jul 6, 2018
  28. locked as resolved and limited conversation to collaborators on Jul 6, 2018
  29. added
    status:has pr
    and removed
    status:needs discussion
    on Aug 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    locked-due-to-inactivitystatus:has pr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions