diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 689ab2835eb71..c215daf7935a6 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -272,10 +272,9 @@ namespace ts { } function parenthesizeForComputedName(expression: Expression): Expression { - return (isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken) || - expression.kind === SyntaxKind.CommaListExpression ? - createParen(expression) : - expression; + return isCommaSequence(expression) + ? createParen(expression) + : expression; } export function createComputedPropertyName(expression: Expression) { @@ -4166,8 +4165,7 @@ namespace ts { // 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 const emittedExpression = skipPartiallyEmittedExpressions(e); - return emittedExpression.kind === SyntaxKind.BinaryExpression && (emittedExpression).operatorToken.kind === SyntaxKind.CommaToken || - emittedExpression.kind === SyntaxKind.CommaListExpression + return isCommaSequence(emittedExpression) ? createParen(e) : e; } @@ -4185,10 +4183,9 @@ namespace ts { */ export function parenthesizeDefaultExpression(e: Expression) { const check = skipPartiallyEmittedExpressions(e); - return (check.kind === SyntaxKind.ClassExpression || + return check.kind === SyntaxKind.ClassExpression || check.kind === SyntaxKind.FunctionExpression || - check.kind === SyntaxKind.CommaListExpression || - isBinaryExpression(check) && check.operatorToken.kind === SyntaxKind.CommaToken) + isCommaSequence(check) ? createParen(e) : e; } @@ -4374,13 +4371,18 @@ namespace ts { } export function parenthesizeConciseBody(body: ConciseBody): ConciseBody { - if (!isBlock(body) && getLeftmostExpression(body, /*stopAtCallExpressions*/ false).kind === SyntaxKind.ObjectLiteralExpression) { + if (!isBlock(body) && (isCommaSequence(body) || getLeftmostExpression(body, /*stopAtCallExpressions*/ false).kind === SyntaxKind.ObjectLiteralExpression)) { return setTextRange(createParen(body), body); } return body; } + export function isCommaSequence(node: Expression): node is BinaryExpression & {operatorToken: Token} | CommaListExpression { + return node.kind === SyntaxKind.BinaryExpression && (node).operatorToken.kind === SyntaxKind.CommaToken || + node.kind === SyntaxKind.CommaListExpression; + } + export const enum OuterExpressionKinds { Parentheses = 1 << 0, Assertions = 1 << 1, diff --git a/src/testRunner/unittests/factory.ts b/src/testRunner/unittests/factory.ts index 1651da64d43c4..5324970ecd5fe 100644 --- a/src/testRunner/unittests/factory.ts +++ b/src/testRunner/unittests/factory.ts @@ -18,6 +18,8 @@ namespace ts { checkBody(createPropertyAccess(createObjectLiteral(), "prop")); checkBody(createAsExpression(createPropertyAccess(createObjectLiteral(), "prop"), createTypeReferenceNode("T", /*typeArguments*/ undefined))); checkBody(createNonNullExpression(createPropertyAccess(createObjectLiteral(), "prop"))); + checkBody(createCommaList([createLiteral("a"), createLiteral("b")])); + checkBody(createBinary(createLiteral("a"), SyntaxKind.CommaToken, createLiteral("b"))); }); }); }); diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 99452b93d4763..a903b959d7d8c 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -8371,6 +8371,9 @@ declare namespace ts { function parenthesizeElementTypeMembers(members: ReadonlyArray): NodeArray; function parenthesizeTypeParameters(typeParameters: ReadonlyArray | undefined): MutableNodeArray | undefined; function parenthesizeConciseBody(body: ConciseBody): ConciseBody; + function isCommaSequence(node: Expression): node is (BinaryExpression & { + operatorToken: Token; + }) | CommaListExpression; enum OuterExpressionKinds { Parentheses = 1, Assertions = 2,