From f4470cd3422bb4d13da159e90e6e3b07fe9b792c Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Wed, 4 Jul 2018 20:47:06 +0200 Subject: [PATCH] createArrowFunction: parenthesize comma sequence Fixes: #25366 --- src/compiler/factory.ts | 22 ++++++++++++---------- src/testRunner/unittests/factory.ts | 2 ++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 689ab2835eb..c215daf7935 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 1651da64d43..5324970ecd5 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"))); }); }); });