Skip to content

Commit 42d0279

Browse files
committed
Port "Fixed nullish coalesce operator's precedence"
1 parent 94ae2f2 commit 42d0279

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

internal/ast/precedence.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const (
4343
// ShortCircuitExpression
4444
// ShortCircuitExpression `?` AssignmentExpression `:` AssignmentExpression
4545
OperatorPrecedenceConditional
46+
// LogicalORExpression:
47+
// LogicalANDExpression
48+
// LogicalORExpression `||` LogicalANDExpression
49+
OperatorPrecedenceLogicalOR
4650
// ShortCircuitExpression:
4751
// LogicalORExpression
4852
// CoalesceExpression
@@ -52,10 +56,6 @@ const (
5256
// CoalesceExpression
5357
// BitwiseORExpression
5458
OperatorPrecedenceCoalesce
55-
// LogicalORExpression:
56-
// LogicalANDExpression
57-
// LogicalORExpression `||` LogicalANDExpression
58-
OperatorPrecedenceLogicalOR
5959
// LogicalANDExpression:
6060
// BitwiseORExpression
6161
// LogicalANDExprerssion `&&` BitwiseORExpression

internal/checker/checker.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12400,11 +12400,22 @@ func (c *Checker) getSyntacticTruthySemantics(node *ast.Node) PredicateSemantics
1240012400
}
1240112401

1240212402
func (c *Checker) checkNullishCoalesceOperands(left *ast.Node, right *ast.Node) {
12403-
if ast.IsBinaryExpression(left) && ast.IsLogicalBinaryOperator(left.AsBinaryExpression().OperatorToken.Kind) {
12404-
c.grammarErrorOnNode(left, diagnostics.X_0_and_1_operations_cannot_be_mixed_without_parentheses, scanner.TokenToString(left.AsBinaryExpression().OperatorToken.Kind), scanner.TokenToString(ast.KindQuestionQuestionToken))
12405-
}
12406-
if ast.IsBinaryExpression(right) && ast.IsLogicalBinaryOperator(right.AsBinaryExpression().OperatorToken.Kind) {
12407-
c.grammarErrorOnNode(right, diagnostics.X_0_and_1_operations_cannot_be_mixed_without_parentheses, scanner.TokenToString(right.AsBinaryExpression().OperatorToken.Kind), scanner.TokenToString(ast.KindQuestionQuestionToken))
12403+
if ast.IsBinaryExpression(left.Parent.Parent) {
12404+
operatorToken := left.Parent.Parent.AsBinaryExpression().OperatorToken
12405+
left := left.Parent.Parent.AsBinaryExpression().Left
12406+
if ast.IsBinaryExpression(left) && operatorToken.Kind == ast.KindBarBarToken {
12407+
c.grammarErrorOnNode(left, diagnostics.X_0_and_1_operations_cannot_be_mixed_without_parentheses, scanner.TokenToString(ast.KindQuestionQuestionToken), scanner.TokenToString(operatorToken.Kind))
12408+
}
12409+
} else if ast.IsBinaryExpression(left) {
12410+
operatorToken := left.AsBinaryExpression().OperatorToken
12411+
if operatorToken.Kind == ast.KindBarBarToken || operatorToken.Kind == ast.KindAmpersandAmpersandToken {
12412+
c.grammarErrorOnNode(left, diagnostics.X_0_and_1_operations_cannot_be_mixed_without_parentheses, scanner.TokenToString(operatorToken.Kind), scanner.TokenToString(ast.KindQuestionQuestionToken))
12413+
}
12414+
} else if ast.IsBinaryExpression(right) {
12415+
operatorToken := right.AsBinaryExpression().OperatorToken
12416+
if operatorToken.Kind == ast.KindAmpersandAmpersandToken {
12417+
c.grammarErrorOnNode(right, diagnostics.X_0_and_1_operations_cannot_be_mixed_without_parentheses, scanner.TokenToString(ast.KindQuestionQuestionToken), scanner.TokenToString(operatorToken.Kind))
12418+
}
1240812419
}
1240912420
leftTarget := ast.SkipOuterExpressions(left, ast.OEKAll)
1241012421
nullishSemantics := c.getSyntacticNullishnessSemantics(leftTarget)

0 commit comments

Comments
 (0)