Skip to content

Commit 16b13fe

Browse files
committed
Fix incorrect parenthesization logic for conditional expression branches
1 parent 64b3086 commit 16b13fe

5 files changed

+61
-1
lines changed

src/compiler/factory.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3954,7 +3954,9 @@ namespace ts {
39543954
// per ES grammar both 'whenTrue' and 'whenFalse' parts of conditional expression are assignment expressions
39553955
// so in case when comma expression is introduced as a part of previous transformations
39563956
// if should be wrapped in parens since comma operator has the lowest precedence
3957-
return e.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>e).operatorToken.kind === SyntaxKind.CommaToken
3957+
const emittedExpression = skipPartiallyEmittedExpressions(e);
3958+
return emittedExpression.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>emittedExpression).operatorToken.kind === SyntaxKind.CommaToken ||
3959+
emittedExpression.kind === SyntaxKind.CommaListExpression
39583960
? createParen(e)
39593961
: e;
39603962
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [transformParenthesizesConditionalSubexpression.ts]
2+
var K = 'k'
3+
var a = { p : (true ? { [K] : 'v'} : null) }
4+
var b = { p : (true ? { [K] : 'v'} as any : null) }
5+
6+
//// [transformParenthesizesConditionalSubexpression.js]
7+
var K = 'k';
8+
var a = { p: (true ? (_a = {}, _a[K] = 'v', _a) : null) };
9+
var b = { p: (true ? (_b = {}, _b[K] = 'v', _b) : null) };
10+
var _a, _b;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/transformParenthesizesConditionalSubexpression.ts ===
2+
var K = 'k'
3+
>K : Symbol(K, Decl(transformParenthesizesConditionalSubexpression.ts, 0, 3))
4+
5+
var a = { p : (true ? { [K] : 'v'} : null) }
6+
>a : Symbol(a, Decl(transformParenthesizesConditionalSubexpression.ts, 1, 3))
7+
>p : Symbol(p, Decl(transformParenthesizesConditionalSubexpression.ts, 1, 9))
8+
>K : Symbol(K, Decl(transformParenthesizesConditionalSubexpression.ts, 0, 3))
9+
10+
var b = { p : (true ? { [K] : 'v'} as any : null) }
11+
>b : Symbol(b, Decl(transformParenthesizesConditionalSubexpression.ts, 2, 3))
12+
>p : Symbol(p, Decl(transformParenthesizesConditionalSubexpression.ts, 2, 9))
13+
>K : Symbol(K, Decl(transformParenthesizesConditionalSubexpression.ts, 0, 3))
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/transformParenthesizesConditionalSubexpression.ts ===
2+
var K = 'k'
3+
>K : string
4+
>'k' : "k"
5+
6+
var a = { p : (true ? { [K] : 'v'} : null) }
7+
>a : { p: { [x: string]: string; }; }
8+
>{ p : (true ? { [K] : 'v'} : null) } : { p: { [x: string]: string; }; }
9+
>p : { [x: string]: string; }
10+
>(true ? { [K] : 'v'} : null) : { [x: string]: string; }
11+
>true ? { [K] : 'v'} : null : { [x: string]: string; }
12+
>true : true
13+
>{ [K] : 'v'} : { [x: string]: string; }
14+
>K : string
15+
>'v' : "v"
16+
>null : null
17+
18+
var b = { p : (true ? { [K] : 'v'} as any : null) }
19+
>b : { p: any; }
20+
>{ p : (true ? { [K] : 'v'} as any : null) } : { p: any; }
21+
>p : any
22+
>(true ? { [K] : 'v'} as any : null) : any
23+
>true ? { [K] : 'v'} as any : null : any
24+
>true : true
25+
>{ [K] : 'v'} as any : any
26+
>{ [K] : 'v'} : { [x: string]: string; }
27+
>K : string
28+
>'v' : "v"
29+
>null : null
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @target: es5
2+
var K = 'k'
3+
var a = { p : (true ? { [K] : 'v'} : null) }
4+
var b = { p : (true ? { [K] : 'v'} as any : null) }

0 commit comments

Comments
 (0)