Skip to content
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)) {
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