Skip to content

createArrowFunction: parenthesize comma sequence #25439

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 2 commits into from
Jul 5, 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
22 changes: 12 additions & 10 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 && (<BinaryExpression>emittedExpression).operatorToken.kind === SyntaxKind.CommaToken ||
emittedExpression.kind === SyntaxKind.CommaListExpression
return isCommaSequence(emittedExpression)
? createParen(e)
: e;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<SyntaxKind.CommaToken>} | CommaListExpression {
return node.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>node).operatorToken.kind === SyntaxKind.CommaToken ||
node.kind === SyntaxKind.CommaListExpression;
}

export const enum OuterExpressionKinds {
Parentheses = 1 << 0,
Assertions = 1 << 1,
Expand Down
2 changes: 2 additions & 0 deletions src/testRunner/unittests/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
});
});
});
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8371,6 +8371,9 @@ declare namespace ts {
function parenthesizeElementTypeMembers(members: ReadonlyArray<TypeNode>): NodeArray<TypeNode>;
function parenthesizeTypeParameters(typeParameters: ReadonlyArray<TypeNode> | undefined): MutableNodeArray<TypeNode> | undefined;
function parenthesizeConciseBody(body: ConciseBody): ConciseBody;
function isCommaSequence(node: Expression): node is (BinaryExpression & {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be included in the declaration file. I'll open a separate issue for that

operatorToken: Token<SyntaxKind.CommaToken>;
}) | CommaListExpression;
enum OuterExpressionKinds {
Parentheses = 1,
Assertions = 2,
Expand Down