Skip to content

Commit 8941e5f

Browse files
authored
Merge pull request #15006 from Microsoft/master-14895
[Master] Emit parenthesis around propert/element access expression of casted object literal expression
2 parents d8a24e3 + 12549f6 commit 8941e5f

10 files changed

+96
-5
lines changed

src/compiler/factory.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3489,8 +3489,7 @@ namespace ts {
34893489
}
34903490

34913491
export function parenthesizeConciseBody(body: ConciseBody): ConciseBody {
3492-
const emittedBody = skipPartiallyEmittedExpressions(body);
3493-
if (emittedBody.kind === SyntaxKind.ObjectLiteralExpression) {
3492+
if (!isBlock(body) && getLeftmostExpression(body).kind === SyntaxKind.ObjectLiteralExpression) {
34943493
return setTextRange(createParen(<Expression>body), body);
34953494
}
34963495

src/compiler/transformers/ts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,13 +2324,13 @@ namespace ts {
23242324
// code if the casted expression has a lower precedence than the rest of the
23252325
// expression.
23262326
//
2327+
// To preserve comments, we return a "PartiallyEmittedExpression" here which will
2328+
// preserve the position information of the original expression.
2329+
//
23272330
// Due to the auto-parenthesization rules used by the visitor and factory functions
23282331
// we can safely elide the parentheses here, as a new synthetic
23292332
// ParenthesizedExpression will be inserted if we remove parentheses too
23302333
// aggressively.
2331-
//
2332-
// To preserve comments, we return a "PartiallyEmittedExpression" here which will
2333-
// preserve the position information of the original expression.
23342334
return createPartiallyEmittedExpression(expression, node);
23352335
}
23362336

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts]
2+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
3+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
4+
5+
//// [emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.js]
6+
(function (x) { return ({ "1": "one", "2": "two" }[x]); });
7+
(function (x) { return ({ "1": "one", "2": "two" }.x); });
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts ===
2+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
3+
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 0, 1))
4+
>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 0, 41))
5+
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 0, 1))
6+
7+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
8+
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 1, 1))
9+
>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 1, 41))
10+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts ===
2+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
3+
>(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x] : (x: any) => string
4+
>x : any
5+
>({ "1": "one", "2": "two" } as { [key: string]: string })[x] : string
6+
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
7+
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
8+
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
9+
>"one" : "one"
10+
>"two" : "two"
11+
>key : string
12+
>x : any
13+
14+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
15+
>(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x : (x: any) => string
16+
>x : any
17+
>({ "1": "one", "2": "two" } as { [key: string]: string }).x : string
18+
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
19+
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
20+
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
21+
>"one" : "one"
22+
>"two" : "two"
23+
>key : string
24+
>x : string
25+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts]
2+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
3+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
4+
5+
//// [emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.js]
6+
(x) => ({ "1": "one", "2": "two" }[x]);
7+
(x) => ({ "1": "one", "2": "two" }.x);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts ===
2+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
3+
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 0, 1))
4+
>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 0, 41))
5+
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 0, 1))
6+
7+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
8+
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 1, 1))
9+
>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 1, 41))
10+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts ===
2+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
3+
>(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x] : (x: any) => string
4+
>x : any
5+
>({ "1": "one", "2": "two" } as { [key: string]: string })[x] : string
6+
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
7+
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
8+
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
9+
>"one" : "one"
10+
>"two" : "two"
11+
>key : string
12+
>x : any
13+
14+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
15+
>(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x : (x: any) => string
16+
>x : any
17+
>({ "1": "one", "2": "two" } as { [key: string]: string }).x : string
18+
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
19+
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
20+
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
21+
>"one" : "one"
22+
>"two" : "two"
23+
>key : string
24+
>x : string
25+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @target: es5
2+
3+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
4+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @target: es6
2+
3+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
4+
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;

0 commit comments

Comments
 (0)