Fix parenthesis for nullish coalescing (#10269)
* ♻️ added condition to check for left and right of nullish coalescing operator and if any is a logical expression without a paren then throw an error * 🐛 bugs fixed and test cases updated for babel parser * ♻️ code comments added * 🐛 spell error rectified * ♻️ failing test updated * 🐛 push tests after make build * Skip nullish-coalescing flow precedence tests They're now incorrect * ♻️ error message updated, binop priority of other logical operators +1 from ?? and * ♻️ increaed the binOp for in and instanceOf, added logic to print the brackets in an ?? && || expression, test cases added * 🐛 failing test fixed and comments updated * ♻️ new lines added between tests * ♻️ basic tests for checking the binOp of instanceOf, in and relational operators to be equal added * ♻️ new lines added in between tests
This commit is contained in:
committed by
Nicolò Ribaudo
parent
3e8a5c5e28
commit
b02e35c19a
@@ -97,5 +97,18 @@ export function needsParens(node, parent, printStack) {
|
||||
if (isOrHasCallExpression(node)) return true;
|
||||
}
|
||||
|
||||
/* this check is for NullishCoalescing being used with LogicalOperators like && and ||
|
||||
* For example when someone creates an ast programmaticaly like this
|
||||
* t.logicalExpression(
|
||||
* "??",
|
||||
* t.logicalExpression("||", t.identifier("a"), t.identifier("b")),
|
||||
* t.identifier("c"),
|
||||
* );
|
||||
* In the example above the AST is equivalent to writing a || b ?? c
|
||||
* This is incorrect because NullishCoalescing when used with LogicalExpressions should have parenthesis
|
||||
* The correct syntax is (a || b) ?? c, that is why we need parenthesis in this case
|
||||
*/
|
||||
if (t.isLogicalExpression(node) && parent.operator === "??") return true;
|
||||
|
||||
return find(expandedParens, node, parent, printStack);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user