Reuse getBinaryOperatorPrecedence (#22844)

* Reuse getBinaryOperatorPrecedence

* Code review

* Consistently sort cases in ascending order
This commit is contained in:
Andy authored and GitHub committed 2018-03-27 16:38:39 -07:00
1 parent 47d768ceb3
commit 07a890dbbb
2 files changed
+107 -154

No files matched your search

+3 -48
View File
@@ -3600,7 +3600,7 @@ namespace ts {
// reScanGreaterToken so that we merge token sequences like > and = into >=
reScanGreaterToken();
const newPrecedence = getBinaryOperatorPrecedence();
const newPrecedence = getBinaryOperatorPrecedence(token());
// Check the precedence to see if we should "take" this operator
// - For left associative operator (all operator but **), consume the operator,
@@ -3662,52 +3662,7 @@ namespace ts {
return false;
}
return getBinaryOperatorPrecedence() > 0;
}
function getBinaryOperatorPrecedence(): number {
switch (token()) {
case SyntaxKind.BarBarToken:
return 1;
case SyntaxKind.AmpersandAmpersandToken:
return 2;
case SyntaxKind.BarToken:
return 3;
case SyntaxKind.CaretToken:
return 4;
case SyntaxKind.AmpersandToken:
return 5;
case SyntaxKind.EqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsEqualsToken:
return 6;
case SyntaxKind.LessThanToken:
case SyntaxKind.GreaterThanToken:
case SyntaxKind.LessThanEqualsToken:
case SyntaxKind.GreaterThanEqualsToken:
case SyntaxKind.InstanceOfKeyword:
case SyntaxKind.InKeyword:
case SyntaxKind.AsKeyword:
return 7;
case SyntaxKind.LessThanLessThanToken:
case SyntaxKind.GreaterThanGreaterThanToken:
case SyntaxKind.GreaterThanGreaterThanGreaterThanToken:
return 8;
case SyntaxKind.PlusToken:
case SyntaxKind.MinusToken:
return 9;
case SyntaxKind.AsteriskToken:
case SyntaxKind.SlashToken:
case SyntaxKind.PercentToken:
return 10;
case SyntaxKind.AsteriskAsteriskToken:
return 11;
}
// -1 is lower than all other precedences. Returning it will cause binary expression
// parsing to stop.
return -1;
return getBinaryOperatorPrecedence(token()) > 0;
}
function makeBinaryExpression(left: Expression, operatorToken: BinaryOperatorToken, right: Expression): BinaryExpression {
@@ -3795,7 +3750,7 @@ namespace ts {
if (isUpdateExpression()) {
const updateExpression = parseUpdateExpression();
return token() === SyntaxKind.AsteriskAsteriskToken ?
<BinaryExpression>parseBinaryExpressionRest(getBinaryOperatorPrecedence(), updateExpression) :
<BinaryExpression>parseBinaryExpressionRest(getBinaryOperatorPrecedence(token()), updateExpression) :
updateExpression;
}