Skip to content
Merged
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
6 changes: 6 additions & 0 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2773,6 +2773,12 @@ func (p *Printer) getBinaryExpressionPrecedence(node *ast.BinaryExpression) (lef

func (p *Printer) emitBinaryExpression(node *ast.BinaryExpression) {
leftPrec, rightPrec := p.getBinaryExpressionPrecedence(node)
if emittedLeft := ast.SkipPartiallyEmittedExpressions(node.Left); ast.NodeIsSynthesized(emittedLeft) && emittedLeft.Kind == ast.KindBinaryExpression && mixingBinaryOperatorsRequiresParentheses(node.OperatorToken.Kind, emittedLeft.AsBinaryExpression().OperatorToken.Kind) {
leftPrec = ast.OperatorPrecedenceHighest
}
if emittedRight := ast.SkipPartiallyEmittedExpressions(node.Right); ast.NodeIsSynthesized(emittedRight) && emittedRight.Kind == ast.KindBinaryExpression && mixingBinaryOperatorsRequiresParentheses(node.OperatorToken.Kind, emittedRight.AsBinaryExpression().OperatorToken.Kind) {
rightPrec = ast.OperatorPrecedenceHighest
Comment thread
jakebailey marked this conversation as resolved.
}
state := p.enterNode(node.AsNode())
p.emitExpression(node.Left, leftPrec)
linesBeforeOperator := p.getLinesBetweenNodes(node.AsNode(), node.Left, node.OperatorToken)
Expand Down
68 changes: 68 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2517,3 +2517,71 @@ func TestPartiallyEmittedExpression(t *testing.T) {
.expression
.expression;`)
}

func TestParenthesizeBinaryExpressionMixingNullishCoalescing(t *testing.T) {
t.Parallel()

tests := []struct {
title string
innerOp ast.Kind
outerOp ast.Kind
side string
output string
}{
// inner ?? on left side of || or &&
{title: "BarBarWithLeftQuestionQuestion", innerOp: ast.KindQuestionQuestionToken, outerOp: ast.KindBarBarToken, side: "left", output: "(a ?? b) || c;"},
{title: "AmpersandAmpersandWithLeftQuestionQuestion", innerOp: ast.KindQuestionQuestionToken, outerOp: ast.KindAmpersandAmpersandToken, side: "left", output: "(a ?? b) && c;"},
// inner ?? on right side of || or &&
{title: "BarBarWithRightQuestionQuestion", innerOp: ast.KindQuestionQuestionToken, outerOp: ast.KindBarBarToken, side: "right", output: "a || (b ?? c);"},
{title: "AmpersandAmpersandWithRightQuestionQuestion", innerOp: ast.KindQuestionQuestionToken, outerOp: ast.KindAmpersandAmpersandToken, side: "right", output: "a && (b ?? c);"},
// inner || or && on left side of ??
{title: "QuestionQuestionWithLeftBarBar", innerOp: ast.KindBarBarToken, outerOp: ast.KindQuestionQuestionToken, side: "left", output: "(a || b) ?? c;"},
{title: "QuestionQuestionWithLeftAmpersandAmpersand", innerOp: ast.KindAmpersandAmpersandToken, outerOp: ast.KindQuestionQuestionToken, side: "left", output: "(a && b) ?? c;"},
// inner || or && on right side of ??
{title: "QuestionQuestionWithRightBarBar", innerOp: ast.KindBarBarToken, outerOp: ast.KindQuestionQuestionToken, side: "right", output: "a ?? (b || c);"},
{title: "QuestionQuestionWithRightAmpersandAmpersand", innerOp: ast.KindAmpersandAmpersandToken, outerOp: ast.KindQuestionQuestionToken, side: "right", output: "a ?? (b && c);"},
}

for _, tt := range tests {
t.Run(tt.title, func(t *testing.T) {
t.Parallel()
var factory ast.NodeFactory
innerExpr := factory.NewBinaryExpression(
nil, /*modifiers*/
factory.NewIdentifier("a"),
nil, /*typeNode*/
factory.NewToken(tt.innerOp),
factory.NewIdentifier("b"),
)
var outerExpr *ast.Node
if tt.side == "left" {
outerExpr = factory.NewBinaryExpression(
nil, /*modifiers*/
innerExpr, /*left: (a innerOp b)*/
nil, /*typeNode*/
factory.NewToken(tt.outerOp),
factory.NewIdentifier("c"),
)
} else {
outerExpr = factory.NewBinaryExpression(
nil, /*modifiers*/
factory.NewIdentifier("a"),
nil, /*typeNode*/
factory.NewToken(tt.outerOp),
innerExpr, /*right: (b innerOp c)*/
)
// adjust identifiers for right side
innerExpr.AsBinaryExpression().Left = factory.NewIdentifier("b")
innerExpr.AsBinaryExpression().Right = factory.NewIdentifier("c")
}
file := factory.NewSourceFile(ast.SourceFileParseOptions{FileName: "/file.ts", Path: "/file.ts"}, "", factory.NewNodeList(
[]*ast.Node{
factory.NewExpressionStatement(outerExpr),
},
), factory.NewToken(ast.KindEndOfFile))

parsetestutil.MarkSyntheticRecursive(file)
emittestutil.CheckEmit(t, nil, file.AsSourceFile(), tt.output)
})
}
}
10 changes: 10 additions & 0 deletions internal/printer/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,16 @@ func isBinaryOperation(node *ast.Node, token ast.Kind) bool {
node.AsBinaryExpression().OperatorToken.Kind == token
}

func mixingBinaryOperatorsRequiresParentheses(a ast.Kind, b ast.Kind) bool {
if a == ast.KindQuestionQuestionToken {
return b == ast.KindAmpersandAmpersandToken || b == ast.KindBarBarToken
}
if b == ast.KindQuestionQuestionToken {
return a == ast.KindAmpersandAmpersandToken || a == ast.KindBarBarToken
}
return false
}

func isImmediatelyInvokedFunctionExpressionOrArrowFunction(node *ast.Expression) bool {
node = ast.SkipPartiallyEmittedExpressions(node)
if !ast.IsCallExpression(node) {
Expand Down
Loading