Fix parenthesis for nullish coalescing#10269
Conversation
…operator and if any is a logical expression without a paren then throw an error
...ges/babel-parser/test/fixtures/experimental/nullish-coalescing-operator/and-nullish/input.js
Show resolved
Hide resolved
...-parser/test/fixtures/experimental/nullish-coalescing-operator/no-paren-and-nullish/input.js
Show resolved
Hide resolved
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11251/ |
jridgewell
left a comment
There was a problem hiding this comment.
This is excellent! A few suggestions, but this is almost done.
| * then raise an error | ||
| */ | ||
|
|
||
| if (op === tt.logicalOR) { |
There was a problem hiding this comment.
We can actually avoid this by lowering the precedence of the ?? operator compared to ||. You can do that by raising the binop for the operators in
babel/packages/babel-parser/src/tokenizer/types.js
Lines 142 to 156 in 4d30379
?? has lower precedence than || and the rest)
There was a problem hiding this comment.
Thank you @jridgewell. This change has been made.
...l-parser/test/fixtures/experimental/nullish-coalescing-operator/no-paren-nullish-or/input.js
Show resolved
Hide resolved
They're now incorrect
…ub.com/vivek12345/babel into fix-parenthesis-for-nullish-coalescing
| modulo: createBinop("%", 10), | ||
| star: createBinop("*", 10), | ||
| slash: createBinop("/", 10), | ||
| logicalOR: createBinop("||", 2), |
There was a problem hiding this comment.
priority of other operators incremented by +1 so that they have more priority over the nullishCoalescing operator which is as per spec.
There was a problem hiding this comment.
It seems like this incrementing of priority broke this test case
FAIL packages/babel-generator/test/index.js
● generation/typescript › cast as
There was a problem hiding this comment.
Why is priority important? It can be the same. a ?? b && c is an error anyway, regardless of which has higher priority.
There was a problem hiding this comment.
@nicolo-ribaudo earlier || and ?? had same priority which was not right. ?? should have lower priority then ||.
Do you think this approach is not right?
...l-parser/test/fixtures/experimental/nullish-coalescing-operator/no-paren-nullish-or/input.js
Show resolved
Hide resolved
| modulo: createBinop("%", 10), | ||
| star: createBinop("*", 10), | ||
| slash: createBinop("/", 10), | ||
| logicalOR: createBinop("||", 2), |
packages/babel-parser/test/fixtures/typescript/cast/as/output.json
Outdated
Show resolved
Hide resolved
… brackets in an ?? && || expression, test cases added
|
@jridgewell The build failed with this error Can we restart the build? |
…l operators to be equal added
jridgewell
left a comment
There was a problem hiding this comment.
This is a really great PR for a first time contributor! Thanks for the work!
|
Thank you @jridgewell for your patience and the kind words. It means a lot. First time contribution to babel has been a delight with your help and support 🎉 |
|
Nullish coalescing is only available if you enable |
| node.right.start, | ||
| `Nullish coalescing operator(??) requires parens when mixing with logical operators`, | ||
| ); | ||
| } |
There was a problem hiding this comment.
@jridgewell @vivek12345 Can I ask why you are checking the AST node property? It should be enough to use current token as a func arg and validate against it. That way you 1) improve performance 2) skip duplicate code 3) simplify
This will also improve the performance.
There was a problem hiding this comment.
It would only work for a && b ?? c, but not for a ?? b && c since in the second case the precedence is a ?? (b && c)
There was a problem hiding this comment.
It would only work for a && b ?? c, but not for a ?? b && c since in the second case the precedence is a ?? (b && c)
There was a problem hiding this comment.
@nicolo-ribaudo I'm confused because it will work for both lhs and rhs. I implemented it that way I described. See here . And it works very well to if you try it in the REPL
|
Regarding #10269 (review), I think that we should merge it since
|
|
What is left to be able to merge it? |
|
We are going to release a minor version with this patch soon! |
With this change all the following operations which are invalid will throw error and ask to add parenthesis to them