Skip to content

Commit 0abefec

Browse files
authored
Merge pull request #25439 from ajafff/factory-arrow-comma
createArrowFunction: parenthesize comma sequence
2 parents 383319d + c54c5f4 commit 0abefec

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-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
});

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8371,6 +8371,9 @@ declare namespace ts {
83718371
function parenthesizeElementTypeMembers(members: ReadonlyArray<TypeNode>): NodeArray<TypeNode>;
83728372
function parenthesizeTypeParameters(typeParameters: ReadonlyArray<TypeNode> | undefined): MutableNodeArray<TypeNode> | undefined;
83738373
function parenthesizeConciseBody(body: ConciseBody): ConciseBody;
8374+
function isCommaSequence(node: Expression): node is (BinaryExpression & {
8375+
operatorToken: Token<SyntaxKind.CommaToken>;
8376+
}) | CommaListExpression;
83748377
enum OuterExpressionKinds {
83758378
Parentheses = 1,
83768379
Assertions = 2,

0 commit comments

Comments
 (0)