wrap subexpressions of conditional expressions in parens if necessary (#12420)

This commit is contained in:
Vladimir Matveev
2016-11-21 12:52:13 -08:00
committed by GitHub
parent 90ee161391
commit 4c6b94f16f
5 changed files with 80 additions and 4 deletions

View File

@@ -654,16 +654,16 @@ namespace ts {
if (whenFalse) {
// second overload
node.questionToken = <QuestionToken>questionTokenOrWhenTrue;
node.whenTrue = whenTrueOrWhenFalse;
node.whenTrue = parenthesizeSubexpressionOfConditionalExpression(whenTrueOrWhenFalse);
node.colonToken = <ColonToken>colonTokenOrLocation;
node.whenFalse = whenFalse;
node.whenFalse = parenthesizeSubexpressionOfConditionalExpression(whenFalse);
}
else {
// first overload
node.questionToken = createToken(SyntaxKind.QuestionToken);
node.whenTrue = <Expression>questionTokenOrWhenTrue;
node.whenTrue = parenthesizeSubexpressionOfConditionalExpression(<Expression>questionTokenOrWhenTrue);
node.colonToken = createToken(SyntaxKind.ColonToken);
node.whenFalse = whenTrueOrWhenFalse;
node.whenFalse = parenthesizeSubexpressionOfConditionalExpression(whenTrueOrWhenFalse);
}
return node;
}
@@ -2381,6 +2381,15 @@ namespace ts {
return condition;
}
function parenthesizeSubexpressionOfConditionalExpression(e: Expression): Expression {
// per ES grammar both 'whenTrue' and 'whenFalse' parts of conditional expression are assignment expressions
// so in case when comma expression is introduced as a part of previous transformations
// if should be wrapped in parens since comma operator has the lowest precedence
return e.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>e).operatorToken.kind === SyntaxKind.CommaToken
? createParen(e)
: e;
}
/**
* Wraps an expression in parentheses if it is needed in order to use the expression
* as the expression of a NewExpression node.