Skip to content

Commit 1e36258

Browse files
authored
Fix unintentional parentheses in optional method call emit (microsoft#440)
1 parent 36805e8 commit 1e36258

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

internal/printer/printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ func (p *Printer) emitCallee(callee *ast.Expression, parentNode *ast.Node) {
22682268
// Parenthesize `new C` inside of a CallExpression so it is treated as `(new C)()` and not `new C()`
22692269
p.emitExpression(callee, ast.OperatorPrecedenceParentheses)
22702270
} else {
2271-
p.emitExpression(callee, ast.OperatorPrecedenceMember)
2271+
p.emitExpression(callee, core.IfElse(ast.IsOptionalChain(parentNode), ast.OperatorPrecedenceOptionalChain, ast.OperatorPrecedenceMember))
22722272
}
22732273
}
22742274

internal/printer/printer_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestEmit(t *testing.T) {
4141
{title: "PropertyAccess#9", input: `0o1.b`, output: `0o1.b;`},
4242
{title: "PropertyAccess#10", input: `10e1.b`, output: `10e1.b;`},
4343
{title: "PropertyAccess#11", input: `10E1.b`, output: `10E1.b;`},
44+
{title: "PropertyAccess#12", input: `a.b?.c`, output: `a.b?.c;`},
4445
{title: "ElementAccess#1", input: `a[b]`, output: `a[b];`},
4546
{title: "ElementAccess#2", input: `a?.[b]`, output: `a?.[b];`},
4647
{title: "ElementAccess#3", input: `a?.[b].c`, output: `a?.[b].c;`},
@@ -56,6 +57,7 @@ func TestEmit(t *testing.T) {
5657
{title: "CallExpression#10", input: `a?.<T>(b).c`, output: `a?.<T>(b).c;`},
5758
{title: "CallExpression#11", input: `a<T, U>()`, output: `a<T, U>();`},
5859
{title: "CallExpression#12", input: `a<T,>()`, output: `a<T,>();`},
60+
{title: "CallExpression#13", input: `a?.b()`, output: `a?.b();`},
5961
{title: "NewExpression#1", input: `new a`, output: `new a;`},
6062
{title: "NewExpression#2", input: `new a.b`, output: `new a.b;`},
6163
{title: "NewExpression#3", input: `new a()`, output: `new a();`},

0 commit comments

Comments
 (0)