Skip to content

Fix incorrect parenthesization logic for conditional expression branches #21216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2018
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
4 changes: 3 additions & 1 deletion src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3954,7 +3954,9 @@ namespace ts {
// per ES grammar both 'whenTrue' and 'whenFalse' parts of conditional expression are assignment expressions
// so in case when comma expression is introduced as a part of previous transformations
// if should be wrapped in parens since comma operator has the lowest precedence
return e.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>e).operatorToken.kind === SyntaxKind.CommaToken
const emittedExpression = skipPartiallyEmittedExpressions(e);
return emittedExpression.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>emittedExpression).operatorToken.kind === SyntaxKind.CommaToken ||
emittedExpression.kind === SyntaxKind.CommaListExpression
? createParen(e)
: e;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [transformParenthesizesConditionalSubexpression.ts]
var K = 'k'
var a = { p : (true ? { [K] : 'v'} : null) }
var b = { p : (true ? { [K] : 'v'} as any : null) }

//// [transformParenthesizesConditionalSubexpression.js]
var K = 'k';
var a = { p: (true ? (_a = {}, _a[K] = 'v', _a) : null) };
var b = { p: (true ? (_b = {}, _b[K] = 'v', _b) : null) };
var _a, _b;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/transformParenthesizesConditionalSubexpression.ts ===
var K = 'k'
>K : Symbol(K, Decl(transformParenthesizesConditionalSubexpression.ts, 0, 3))

var a = { p : (true ? { [K] : 'v'} : null) }
>a : Symbol(a, Decl(transformParenthesizesConditionalSubexpression.ts, 1, 3))
>p : Symbol(p, Decl(transformParenthesizesConditionalSubexpression.ts, 1, 9))
>K : Symbol(K, Decl(transformParenthesizesConditionalSubexpression.ts, 0, 3))

var b = { p : (true ? { [K] : 'v'} as any : null) }
>b : Symbol(b, Decl(transformParenthesizesConditionalSubexpression.ts, 2, 3))
>p : Symbol(p, Decl(transformParenthesizesConditionalSubexpression.ts, 2, 9))
>K : Symbol(K, Decl(transformParenthesizesConditionalSubexpression.ts, 0, 3))

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/transformParenthesizesConditionalSubexpression.ts ===
var K = 'k'
>K : string
>'k' : "k"

var a = { p : (true ? { [K] : 'v'} : null) }
>a : { p: { [x: string]: string; }; }
>{ p : (true ? { [K] : 'v'} : null) } : { p: { [x: string]: string; }; }
>p : { [x: string]: string; }
>(true ? { [K] : 'v'} : null) : { [x: string]: string; }
>true ? { [K] : 'v'} : null : { [x: string]: string; }
>true : true
>{ [K] : 'v'} : { [x: string]: string; }
>K : string
>'v' : "v"
>null : null

var b = { p : (true ? { [K] : 'v'} as any : null) }
>b : { p: any; }
>{ p : (true ? { [K] : 'v'} as any : null) } : { p: any; }
>p : any
>(true ? { [K] : 'v'} as any : null) : any
>true ? { [K] : 'v'} as any : null : any
>true : true
>{ [K] : 'v'} as any : any
>{ [K] : 'v'} : { [x: string]: string; }
>K : string
>'v' : "v"
>null : null

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @target: es5
var K = 'k'
var a = { p : (true ? { [K] : 'v'} : null) }
var b = { p : (true ? { [K] : 'v'} as any : null) }