Skip to content

no-semi - #1129

Merged
vjeux merged 1 commit into
prettier:masterfrom
rattrayalex:no-semi
Apr 11, 2017
Merged

no-semi#1129
vjeux merged 1 commit into
prettier:masterfrom
rattrayalex:no-semi

Conversation

@rattrayalex

@rattrayalex rattrayalex commented Apr 5, 2017 •

Copy link
Copy Markdown
Collaborator

Addresses #736

@vjeux

vjeux commented Apr 5, 2017

Copy link
Copy Markdown
Contributor

cc @bakkot

@bakkot

bakkot commented Apr 5, 2017

Copy link
Copy Markdown
Collaborator

More interesting cases:

while (false)
  (function(){}()) // definitely do not start this line with a semicolon
aReallyLongLine012345678901234567890123456789012345678901234567890123456789 *
  (b + c) // or this one
a;
/b/g.test('b') // you have a test with a regex, but in yours omitting the semicolon would be a parse error instead of a different AST. Having the flag makes a difference.
a; // Necessary semicolon
::b.c
class A {
  async; // The semicolon is *not* necessary, because of a '[no LineTerminator here]' restriction
  x(){}
}
class A {
  static; // The semicolon *is* necessary
  x(){}
}

There's probably some involving flow and JSX too, but I'm not as familiar with their grammars.

@rattrayalex

Copy link
Copy Markdown
Collaborator Author

Nice, thanks @bakkot !

@rattrayalex

rattrayalex commented Apr 5, 2017 •

Copy link
Copy Markdown
Collaborator Author

@bakkot – FYI, this one doesn't parse, so not including it

class A {
  static; 
  x(){}
}

@bakkot

bakkot commented Apr 5, 2017

Copy link
Copy Markdown
Collaborator

@rattrayalex Does too :)

You're just using a version of babylon which is behind master.

@rattrayalex
rattrayalex force-pushed the no-semi branch 2 times, most recently from c0fa86f to 63fc59c Compare April 5, 2017 23:41
@vjeux

vjeux commented Apr 5, 2017

Copy link
Copy Markdown
Contributor

Doesn't seem to parse on Chrome

image

@bakkot

bakkot commented Apr 5, 2017

Copy link
Copy Markdown
Collaborator

@vjeux Class properties aren't yet part of JS; it's an experimental feature. You'll notice that class Y { foo; } doesn't parse either.

@rattrayalex

Copy link
Copy Markdown
Collaborator Author

Hah! Nice 😄
I'll add a commented-out version with a TODO for now then.

@rattrayalex
rattrayalex force-pushed the no-semi branch 5 times, most recently from 2ffa62c to cda1a0c Compare April 6, 2017 00:44

exports[`no-semi.js 1`] = `
"
// the \`if (true)\` isn't necessary; just wanted to use a block statement

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use class x {} if you want something that fits in one line btw.

@rattrayalex

rattrayalex commented Apr 6, 2017 •

Copy link
Copy Markdown
Collaborator Author

@bakkot my hunch is that the best way to handle

class A {
  static; 
  x(){}
}

will be by preserving the ; after the static – does that sound right to you? Would be a break from how we do the other stuff.

(I'll probably kick the can down the road on that particular issue, unless you sneak in a babylon upgrade before I merge 😉 )

@vjeux

vjeux commented Apr 6, 2017

Copy link
Copy Markdown
Contributor

The static/get is such an edge case that it's totally fine to keep it at the end of the line. I doubt anyone will ever write code like this, and if that person does, then they'll be positively surprised that we didn't break their code :p

@rattrayalex

Copy link
Copy Markdown
Collaborator Author

@bakkot care to lend some 👀 on test cases again? Apologies if I missed something you previously mentioned

Comment thread src/printer.js Outdated
return exprNeedsASIProtection(node.expression);
}

function classStmtNeedsASIProtection(path) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can kill it now

@vjeux

vjeux commented Apr 6, 2017

Copy link
Copy Markdown
Contributor

@lydell would you have time to run your fuzzer on this PR (or let us know how to do it)?

Comment thread src/printer.js Outdated
}

function printStatementSequence(path, options, print) {
function printStatementSequence(path, options, print, isClass) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's technically "more correct", I think I'm going to get rid of this isClass thing because it's not necessary and stinks a bit

Comment thread src/printer.js
}

function exprNeedsASIProtection(node) {
const maybeASIProblem = node.needsParens ||

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where needsParens is used.

exprNeedsASIProtection is recursive, and recurses on the basis of the ast nodes via getLeftSide, just above. So it'll be a bit of a pain to move to using path.needsParens(), in addition to being slower.

But this is pretty ugly.

Thoughts?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vjeux any thoughts on this?

@rattrayalex rattrayalex changed the title WIP - no-semi tests no-semi Apr 6, 2017
Comment thread README.md Outdated
parser: 'babylon',

// Whether to use a semicolon-free style, similar to eslint's semi: never
noSemi: false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you name this option semi and default to true. The option parser library will make --no-semi work

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure!

Comment thread src/printer.js Outdated

function printExportDeclaration(path, options, print) {
const decl = path.getValue();
const semi = options.noSemi ? "" : ";"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: two spaces, and missing semi :p But feel free to leave it as is, it'll go away when we reformat it again :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, looks like the codebase is overdue for a run of npm run format:all, I see loads of changes (not all of them look great? what's with the removal of all the newlines?)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to do format:all after each release since the number of PRs was near 0 but we skipped doing that during the last one and had a bunch of changes. It would be greatly needed indeed!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very weird indeed for the removal of newlines. May be related to #1136

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that makes sense. Want me to open an issue?

;[\\"m\\"]() {
1
}
;*g() {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is unfortunate, but I guess generator functions are not widely used

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's ugly, but needed;

class X {
  a = 1
  *g() {}
}

throws a syntax error.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class X {
  f() {}
  *g() {}
}

doesn't though, which is pretty common. But yeah, given where we put semi, I think it's okay to put it there.

@rattrayalex rattrayalex Apr 6, 2017 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I kind of think it'd be worthwhile to break the "just put a ; at the start of every line" rule for class methods (especially since the universe of possibilities is far smaller).

It wouldn't be too hard to check the preceding node, and only print ; if it's not a method.

@bakkot @lydell what do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't even know semicolons where allowed there! So 👍 for "breaking the rule" (which was only very loosely defined in the first place anyway) whenever it makes sense.

@rattrayalex
rattrayalex force-pushed the no-semi branch 2 times, most recently from c12df15 to 9834782 Compare April 9, 2017 19:19
@rattrayalex

Copy link
Copy Markdown
Collaborator Author

I checked for other insertions of ; and found a few and fixed them.

I think this is ready pending final review, though I'd also like to put up a straw man PR with semi: false as the default option so folks can audit the output. I tried looking myself on the command line for a bit and it was a bit much.

Thinking about it more, I also think a solution based on the printed doc would actually be more correct, robust, and performant than the AST solution in this case. Not sure it's worth putting together though.

Comment thread README.md

// Whether to add a semicolon at the end of every line (semi: true),
// or only at the beginning of lines that may introduce ASI failures (semi: false)
semi: true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's easier to have options that are by default false, so that people can just add them on the CLI easier. How about "noSemi" and the CLI arg would be --no-semi? For some reason I just like the args themselves describing the change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jlongster I made the recommendation for it to be true by default. This way the argument parser that we use makes --no-semi work. But maybe it's too clever.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have bracketSpacing as true by default and --no-bracket-spacing for turning it off.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, yeah the CLI arg is the main thing I was thinking about. I guess it's fine if the API option is different and the CLI one is --no-semi.

Comment thread src/printer.js
// For now, we're just passing that information by mutating the AST here,
// but it would be nice to find a cleaner way to do this.
node.needsParens = needsParens;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my idea: I don't see any reason why we can't have access to the path instead of the node, and we can just call path.needsParens() multiple times. To fix the performance hit, we could cache that method so if it's already been calculated it just returns the value.

The only problem is that the path abstraction using mutation heavily, so writing the cache isn't too straight-forward, but it's doable. I think we've talked about making the path abstraction better anyway.

I'll make another comment below about this. But I don't think this code is complex enough to block this PR from landing. We can work on this later. I feel OK about letting this stuff in prettier because we aren't exposing these objects over an API or anything so these hacks don't impact users, as long as the output is good.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I think if we were to make a change at this point I'd rather go to reading tokens. It dodges this issue through elegance rather than complexity, and should be more robust/performant to boot.

My primary thinking is that it's fundamentally an output-token-based problem, not an AST problem (other than only applying it to statements, which is useful). The question you're trying to answer when deciding whether to prepend a semicolon isn't "is this a TypeCastExpression" or what have you, it's "does this start with a paren".

As the algorithm stands, we're vulnerable to parens being inserted in the future in places that we're not currently looking for them. It's only through extensive smoke tests that I was able to discover cases like ArrowFunctionExpressions, which sometimes begin with a paren, for example.

Note that I think the class body stuff should probably still be done with the AST, since the path is more appropriate there.

... maybe it's time I stop talking and start doing with this, and we can see how it looks. I'll add as a second commit, and will be happy to go back to the AST approach if that's what we prefer.

Comment thread src/printer.js
return false;
}

return exprNeedsASIProtection(getLeftSide(node));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you were dealing with path instead, you'd do path.call(exprNeedsASIProtection, getLeftSideName(path)) or something like that. But I don't know.

Comment thread src/printer.js
function exprNeedsASIProtection(node) {
// HACK: node.needsParens is added in `genericPrint()` for the sole purpose
// of being used here. It'd be preferable to find a cleaner way to do this.
const maybeASIProblem = node.needsParens ||

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this is recursive, and the top-level statement kicks this off. I'm assuming it's possible for a node to be mutated deep down when printing, and then the recursion here eventually picks that up? I thought at first you only need to check top-level statements, but I think it's more complex than that.

As I said above I'm not too worried about merging this as is, and if we ever get around to cleaning it up, we can. We should keep this in mind if we ever encourage people to pass ASTs straight in though, as we will be modifying it (though we modify it with comments anyway).

@jlongster

Copy link
Copy Markdown
Member

This looks pretty straight-forward in general, and all the complicated logic is isolated to its own section. Great work! Just talking out loud in my comments, but I don't think much needs to be changed before merging. The main thing is the name of the option. What do you think about --no-semi? I think it makes it clear that we encourage semicolons and its the default.

@rattrayalex

rattrayalex commented Apr 11, 2017 •

Copy link
Copy Markdown
Collaborator Author

Sounds like the consensus is to leave semi: false and tell users to use --no-semi at the command line, which is fine.

I'd like to take a crack today at the doc-based approach; if I'm not able to get it done today or if y'all don't end up finding it preferable, I'm happy to ship this.

EDIT: You're also welcome to merge, and I can propose the alternative approach as a follow-up PR.

@jlongster

Copy link
Copy Markdown
Member

I see where you are going with the doc-based approach. It might be simpler for most of the use cases, but not sure how you'd solve some of the edge cases. It'd be interesting for you to try it, but I'll leave it up to @vjeux if he wants to go ahead and merge this. My gut feeling is we should and you can land a new implementation later if it's simpler and works.

@jlongster

Copy link
Copy Markdown
Member

Note that I think the class body stuff should probably still be done with the AST, since the path is more appropriate there.

Oh, those are the edge cases I was thinking about. In that case, if you can spend a little time on that approach you could let us know if it looks promising enough to hold off?

@vjeux

vjeux commented Apr 11, 2017

Copy link
Copy Markdown
Contributor

I'm doing a pretty big release right now (first one in 3 weeks), so let me know if you want this one included :)

@rattrayalex

rattrayalex commented Apr 11, 2017 via email

Copy link
Copy Markdown
Collaborator Author

@vjeux
vjeux merged commit 36bec87 into prettier:master Apr 11, 2017
@vjeux

vjeux commented Apr 11, 2017

Copy link
Copy Markdown
Contributor

<3 This release is going to be huge!

@frol frol mentioned this pull request Apr 12, 2017
@frol

frol commented Apr 12, 2017

Copy link
Copy Markdown

I have just tried it with my project and the output is nice and 100% valid! Thank you so much!

P.S. I couldn't find --no-semi option on the CLI utility, so I patched it slightly just so it has semi: false by default in it. Is that intended or exposing the CLI parameter should be just be done in a separate PR?

@rattrayalex
rattrayalex deleted the no-semi branch April 13, 2017 01:50
@azz azz mentioned this pull request Jul 4, 2017
@lock lock Bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 20, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants