Skip to content

Commit 54edde8

Browse files
committed
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.
1 parent 76ef974 commit 54edde8

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/compiler/factory.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,18 +3875,14 @@ namespace ts {
38753875
*/
38763876
export function parenthesizeForAccess(expression: Expression): LeftHandSideExpression {
38773877
// isLeftHandSideExpression is almost the correct criterion for when it is not necessary
3878-
// to parenthesize the expression before a dot. There are two known exceptions:
3878+
// to parenthesize the expression before a dot. The known exception is:
38793879
//
38803880
// NewExpression:
38813881
// new C.x -> not the same as (new C).x
38823882
//
3883-
// ObjectLiteral:
3884-
// {a:1}.toString() -> is incorrect syntax, should be ({a:1}).toString()
3885-
//
38863883
const emittedExpression = skipPartiallyEmittedExpressions(expression);
38873884
if (isLeftHandSideExpression(emittedExpression)
3888-
&& (!isNewExpression(emittedExpression) || (<NewExpression>emittedExpression).arguments)
3889-
&& !isObjectLiteralExpression(emittedExpression)) {
3885+
&& (emittedExpression.kind !== SyntaxKind.NewExpression || (<NewExpression>emittedExpression).arguments)) {
38903886
return <LeftHandSideExpression>expression;
38913887
}
38923888

@@ -3945,11 +3941,10 @@ namespace ts {
39453941
return recreateOuterExpressions(expression, mutableCall, OuterExpressionKinds.PartiallyEmittedExpressions);
39463942
}
39473943
}
3948-
else {
3949-
const leftmostExpressionKind = getLeftmostExpression(emittedExpression).kind;
3950-
if (leftmostExpressionKind === SyntaxKind.ObjectLiteralExpression || leftmostExpressionKind === SyntaxKind.FunctionExpression) {
3951-
return setTextRange(createParen(expression), expression);
3952-
}
3944+
3945+
const leftmostExpressionKind = getLeftmostExpression(emittedExpression).kind;
3946+
if (leftmostExpressionKind === SyntaxKind.ObjectLiteralExpression || leftmostExpressionKind === SyntaxKind.FunctionExpression) {
3947+
return setTextRange(createParen(expression), expression);
39533948
}
39543949

39553950
return expression;

tests/baselines/reference/propertyAccessOnObjectLiteral.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var A = /** @class */ (function () {
1414
}
1515
return A;
1616
}());
17-
({}).toString();
17+
({}.toString());
1818
(function () {
19-
({}).toString();
19+
({}.toString());
2020
})();

0 commit comments

Comments
 (0)