Skip to content

Fix parenthesizer rules for manually constructed binary expressions with ?? and ||/&& mix #62311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/compiler/factory/parenthesizerRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ export function createParenthesizerRules(factory: NodeFactory): ParenthesizerRul
return parenthesizerRule;
}

function mixingBinaryOperatorsRequiresParentheses(a: SyntaxKind, b: SyntaxKind) {
if (a === SyntaxKind.QuestionQuestionToken) {
return b === SyntaxKind.AmpersandAmpersandToken || b === SyntaxKind.BarBarToken;
}
if (b === SyntaxKind.QuestionQuestionToken) {
return a === SyntaxKind.AmpersandAmpersandToken || a === SyntaxKind.BarBarToken;
}
return false;
}

/**
* Determines whether the operand to a BinaryExpression needs to be parenthesized.
*
Expand All @@ -121,6 +131,10 @@ export function createParenthesizerRules(factory: NodeFactory): ParenthesizerRul
* BinaryExpression.
*/
function binaryOperandNeedsParentheses(binaryOperator: SyntaxKind, operand: Expression, isLeftSideOfBinary: boolean, leftOperand: Expression | undefined) {
const emittedOperand = skipPartiallyEmittedExpressions(operand);
if (isBinaryExpression(emittedOperand) && mixingBinaryOperatorsRequiresParentheses(binaryOperator, emittedOperand.operatorToken.kind)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

mixing them without parentheses is a special grammar-based syntax error, so one can't simply handle both of those cases using precedence and associativity alone, and for that reason I'm also specialcasing this here

return true;
}
// If the operand has lower precedence, then it needs to be parenthesized to preserve the
// intent of the expression. For example, if the operand is `a + b` and the operator is
// `*`, then we need to parenthesize the operand to preserve the intended order of
Expand All @@ -140,7 +154,6 @@ export function createParenthesizerRules(factory: NodeFactory): ParenthesizerRul
// the intended order of operations: `(a ** b) ** c`
const binaryOperatorPrecedence = getOperatorPrecedence(SyntaxKind.BinaryExpression, binaryOperator);
const binaryOperatorAssociativity = getOperatorAssociativity(SyntaxKind.BinaryExpression, binaryOperator);
const emittedOperand = skipPartiallyEmittedExpressions(operand);
if (!isLeftSideOfBinary && operand.kind === SyntaxKind.ArrowFunction && binaryOperatorPrecedence > OperatorPrecedence.Assignment) {
// We need to parenthesize arrow functions on the right side to avoid it being
// parsed as parenthesized expression: `a && (() => {})`
Expand Down
306 changes: 306 additions & 0 deletions src/testRunner/unittests/printer.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(a ?? b) && c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a && (b ?? c);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(a ?? b) || c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a || (b ?? c);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ?? b, c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a, b ?? c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(a ?? b) == c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a == (b ?? c);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(a && b) ?? c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(a || b) ?? c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(a, b) ?? c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a == b ?? c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ?? b ?? c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ?? (b && c);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ?? (b || c);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ?? (b, c);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ?? b == c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ?? (b ?? c);
Loading