From 54edde889229c228dda0064327ffa91beae27a6e Mon Sep 17 00:00:00 2001 From: Henry Mercer Date: Tue, 19 Sep 2017 23:58:03 +0100 Subject: [PATCH] Fix property access bug instead by wrapping entire access in brackets Modify parenthesizeExpressionForExpressionStatement to add brackets around an expression statement in which the left-most expression is an object literal. --- src/compiler/factory.ts | 17 ++++++----------- .../reference/propertyAccessOnObjectLiteral.js | 4 ++-- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index ebb428640da..50004ba0127 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -3875,18 +3875,14 @@ namespace ts { */ export function parenthesizeForAccess(expression: Expression): LeftHandSideExpression { // isLeftHandSideExpression is almost the correct criterion for when it is not necessary - // to parenthesize the expression before a dot. There are two known exceptions: + // to parenthesize the expression before a dot. The known exception is: // // NewExpression: // new C.x -> not the same as (new C).x // - // ObjectLiteral: - // {a:1}.toString() -> is incorrect syntax, should be ({a:1}).toString() - // const emittedExpression = skipPartiallyEmittedExpressions(expression); if (isLeftHandSideExpression(emittedExpression) - && (!isNewExpression(emittedExpression) || (emittedExpression).arguments) - && !isObjectLiteralExpression(emittedExpression)) { + && (emittedExpression.kind !== SyntaxKind.NewExpression || (emittedExpression).arguments)) { return expression; } @@ -3945,11 +3941,10 @@ namespace ts { return recreateOuterExpressions(expression, mutableCall, OuterExpressionKinds.PartiallyEmittedExpressions); } } - else { - const leftmostExpressionKind = getLeftmostExpression(emittedExpression).kind; - if (leftmostExpressionKind === SyntaxKind.ObjectLiteralExpression || leftmostExpressionKind === SyntaxKind.FunctionExpression) { - return setTextRange(createParen(expression), expression); - } + + const leftmostExpressionKind = getLeftmostExpression(emittedExpression).kind; + if (leftmostExpressionKind === SyntaxKind.ObjectLiteralExpression || leftmostExpressionKind === SyntaxKind.FunctionExpression) { + return setTextRange(createParen(expression), expression); } return expression; diff --git a/tests/baselines/reference/propertyAccessOnObjectLiteral.js b/tests/baselines/reference/propertyAccessOnObjectLiteral.js index de584ece44c..4f1f8b042c9 100644 --- a/tests/baselines/reference/propertyAccessOnObjectLiteral.js +++ b/tests/baselines/reference/propertyAccessOnObjectLiteral.js @@ -14,7 +14,7 @@ var A = /** @class */ (function () { } return A; }()); -({}).toString(); +({}.toString()); (function () { - ({}).toString(); + ({}.toString()); })();