Repository navigation
no-semi - #1129
no-semi#1129
Conversation
|
cc @bakkot |
|
More interesting cases: while (false)
(function(){}()) // definitely do not start this line with a semicolonaReallyLongLine012345678901234567890123456789012345678901234567890123456789 *
(b + c) // or this onea;
/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.cclass 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. |
|
Nice, thanks @bakkot ! |
|
@bakkot – FYI, this one doesn't parse, so not including it class A {
static;
x(){}
} |
|
You're just using a version of babylon which is behind master. |
c0fa86f to
63fc59c
Compare
|
@vjeux Class properties aren't yet part of JS; it's an experimental feature. You'll notice that |
|
Hah! Nice 😄 |
2ffa62c to
cda1a0c
Compare
|
|
||
| exports[`no-semi.js 1`] = ` | ||
| " | ||
| // the \`if (true)\` isn't necessary; just wanted to use a block statement |
There was a problem hiding this comment.
you can use class x {} if you want something that fits in one line btw.
|
@bakkot my hunch is that the best way to handle will be by preserving the (I'll probably kick the can down the road on that particular issue, unless you sneak in a babylon upgrade before I merge 😉 ) |
|
The |
|
@bakkot care to lend some 👀 on test cases again? Apologies if I missed something you previously mentioned |
| return exprNeedsASIProtection(node.expression); | ||
| } | ||
|
|
||
| function classStmtNeedsASIProtection(path) { |
|
@lydell would you have time to run your fuzzer on this PR (or let us know how to do it)? |
| } | ||
|
|
||
| function printStatementSequence(path, options, print) { | ||
| function printStatementSequence(path, options, print, isClass) { |
There was a problem hiding this comment.
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
| } | ||
|
|
||
| function exprNeedsASIProtection(node) { | ||
| const maybeASIProblem = node.needsParens || |
There was a problem hiding this comment.
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?
| parser: 'babylon', | ||
|
|
||
| // Whether to use a semicolon-free style, similar to eslint's semi: never | ||
| noSemi: false |
There was a problem hiding this comment.
can you name this option semi and default to true. The option parser library will make --no-semi work
|
|
||
| function printExportDeclaration(path, options, print) { | ||
| const decl = path.getValue(); | ||
| const semi = options.noSemi ? "" : ";" |
There was a problem hiding this comment.
nit: two spaces, and missing semi :p But feel free to leave it as is, it'll go away when we reformat it again :)
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
This looks very weird indeed for the removal of newlines. May be related to #1136
There was a problem hiding this comment.
yeah, that makes sense. Want me to open an issue?
| ;[\\"m\\"]() { | ||
| 1 | ||
| } | ||
| ;*g() {} |
There was a problem hiding this comment.
this one is unfortunate, but I guess generator functions are not widely used
There was a problem hiding this comment.
yeah, it's ugly, but needed;
class X {
a = 1
*g() {}
}
throws a syntax error.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
c12df15 to
9834782
Compare
|
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. |
|
|
||
| // 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
We already have bracketSpacing as true by default and --no-bracket-spacing for turning it off.
There was a problem hiding this comment.
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.
| // 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; | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| return false; | ||
| } | ||
|
|
||
| return exprNeedsASIProtection(getLeftSide(node)); |
There was a problem hiding this comment.
If you were dealing with path instead, you'd do path.call(exprNeedsASIProtection, getLeftSideName(path)) or something like that. But I don't know.
| 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 || |
There was a problem hiding this comment.
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).
|
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 |
|
Sounds like the consensus is to leave 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. |
|
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. |
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? |
|
I'm doing a pretty big release right now (first one in 3 weeks), so let me know if you want this one included :) |
|
K, I have a doc based approach working locally but it's not quite as clean
as I was hoping (comments get messy, and testing is a bit tricky). So let's
ship this for now.
Thanks for the patience & input!
…On Tue, Apr 11, 2017, 13:25 Christopher Chedeau ***@***.***> wrote:
I'm doing a pretty big release right now (first one in 3 weeks), so let me
know if you want this one included :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1129 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAq_LvatPa4lMak99IPfFCKT-axVCU45ks5ru-HSgaJpZM4M0qcv>
.
|
|
<3 This release is going to be huge! |
|
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 |

Addresses #736