Skip to content

Commit f4470cd

Browse files
committed
createArrowFunction: parenthesize comma sequence
Fixes: microsoft#25366
1 parent 84f5aa5 commit f4470cd

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/compiler/factory.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,9 @@ namespace ts {
272272
}
273273

274274
function parenthesizeForComputedName(expression: Expression): Expression {
275-
return (isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken) ||
276-
expression.kind === SyntaxKind.CommaListExpression ?
277-
createParen(expression) :
278-
expression;
275+
return isCommaSequence(expression)
276+
? createParen(expression)
277+
: expression;
279278
}
280279

281280
export function createComputedPropertyName(expression: Expression) {
@@ -4166,8 +4165,7 @@ namespace ts {
41664165
// so in case when comma expression is introduced as a part of previous transformations
41674166
// if should be wrapped in parens since comma operator has the lowest precedence
41684167
const emittedExpression = skipPartiallyEmittedExpressions(e);
4169-
return emittedExpression.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>emittedExpression).operatorToken.kind === SyntaxKind.CommaToken ||
4170-
emittedExpression.kind === SyntaxKind.CommaListExpression
4168+
return isCommaSequence(emittedExpression)
41714169
? createParen(e)
41724170
: e;
41734171
}
@@ -4185,10 +4183,9 @@ namespace ts {
41854183
*/
41864184
export function parenthesizeDefaultExpression(e: Expression) {
41874185
const check = skipPartiallyEmittedExpressions(e);
4188-
return (check.kind === SyntaxKind.ClassExpression ||
4186+
return check.kind === SyntaxKind.ClassExpression ||
41894187
check.kind === SyntaxKind.FunctionExpression ||
4190-
check.kind === SyntaxKind.CommaListExpression ||
4191-
isBinaryExpression(check) && check.operatorToken.kind === SyntaxKind.CommaToken)
4188+
isCommaSequence(check)
41924189
? createParen(e)
41934190
: e;
41944191
}
@@ -4374,13 +4371,18 @@ namespace ts {
43744371
}
43754372

43764373
export function parenthesizeConciseBody(body: ConciseBody): ConciseBody {
4377-
if (!isBlock(body) && getLeftmostExpression(body, /*stopAtCallExpressions*/ false).kind === SyntaxKind.ObjectLiteralExpression) {
4374+
if (!isBlock(body) && (isCommaSequence(body) || getLeftmostExpression(body, /*stopAtCallExpressions*/ false).kind === SyntaxKind.ObjectLiteralExpression)) {
43784375
return setTextRange(createParen(body), body);
43794376
}
43804377

43814378
return body;
43824379
}
43834380

4381+
export function isCommaSequence(node: Expression): node is BinaryExpression & {operatorToken: Token<SyntaxKind.CommaToken>} | CommaListExpression {
4382+
return node.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>node).operatorToken.kind === SyntaxKind.CommaToken ||
4383+
node.kind === SyntaxKind.CommaListExpression;
4384+
}
4385+
43844386
export const enum OuterExpressionKinds {
43854387
Parentheses = 1 << 0,
43864388
Assertions = 1 << 1,

src/testRunner/unittests/factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace ts {
1818
checkBody(createPropertyAccess(createObjectLiteral(), "prop"));
1919
checkBody(createAsExpression(createPropertyAccess(createObjectLiteral(), "prop"), createTypeReferenceNode("T", /*typeArguments*/ undefined)));
2020
checkBody(createNonNullExpression(createPropertyAccess(createObjectLiteral(), "prop")));
21+
checkBody(createCommaList([createLiteral("a"), createLiteral("b")]));
22+
checkBody(createBinary(createLiteral("a"), SyntaxKind.CommaToken, createLiteral("b")));
2123
});
2224
});
2325
});

0 commit comments

Comments
 (0)