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.
This commit is contained in:
Henry Mercer 2017-09-19 23:58:03 +01:00
parent 76ef97449c
commit 54edde8892
2 changed files with 8 additions and 13 deletions

View File

@ -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) || (<NewExpression>emittedExpression).arguments)
&& !isObjectLiteralExpression(emittedExpression)) {
&& (emittedExpression.kind !== SyntaxKind.NewExpression || (<NewExpression>emittedExpression).arguments)) {
return <LeftHandSideExpression>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;

View File

@ -14,7 +14,7 @@ var A = /** @class */ (function () {
}
return A;
}());
({}).toString();
({}.toString());
(function () {
({}).toString();
({}.toString());
})();