Merge pull request #15006 from Microsoft/master-14895

[Master] Emit parenthesis around propert/element access expression of casted object literal expression
This commit is contained in:
Yui 2017-04-05 13:03:24 -07:00 committed by GitHub
commit 8941e5f7b0
10 changed files with 96 additions and 5 deletions

View File

@ -3489,8 +3489,7 @@ namespace ts {
}
export function parenthesizeConciseBody(body: ConciseBody): ConciseBody {
const emittedBody = skipPartiallyEmittedExpressions(body);
if (emittedBody.kind === SyntaxKind.ObjectLiteralExpression) {
if (!isBlock(body) && getLeftmostExpression(body).kind === SyntaxKind.ObjectLiteralExpression) {
return setTextRange(createParen(<Expression>body), body);
}

View File

@ -2324,13 +2324,13 @@ namespace ts {
// code if the casted expression has a lower precedence than the rest of the
// expression.
//
// To preserve comments, we return a "PartiallyEmittedExpression" here which will
// preserve the position information of the original expression.
//
// Due to the auto-parenthesization rules used by the visitor and factory functions
// we can safely elide the parentheses here, as a new synthetic
// ParenthesizedExpression will be inserted if we remove parentheses too
// aggressively.
//
// To preserve comments, we return a "PartiallyEmittedExpression" here which will
// preserve the position information of the original expression.
return createPartiallyEmittedExpression(expression, node);
}

View File

@ -0,0 +1,7 @@
//// [emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts]
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
//// [emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.js]
(function (x) { return ({ "1": "one", "2": "two" }[x]); });
(function (x) { return ({ "1": "one", "2": "two" }.x); });

View File

@ -0,0 +1,10 @@
=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts ===
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 0, 1))
>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 0, 41))
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 0, 1))
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 1, 1))
>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 1, 41))

View File

@ -0,0 +1,25 @@
=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts ===
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
>(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x] : (x: any) => string
>x : any
>({ "1": "one", "2": "two" } as { [key: string]: string })[x] : string
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
>"one" : "one"
>"two" : "two"
>key : string
>x : any
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
>(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x : (x: any) => string
>x : any
>({ "1": "one", "2": "two" } as { [key: string]: string }).x : string
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
>"one" : "one"
>"two" : "two"
>key : string
>x : string

View File

@ -0,0 +1,7 @@
//// [emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts]
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
//// [emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.js]
(x) => ({ "1": "one", "2": "two" }[x]);
(x) => ({ "1": "one", "2": "two" }.x);

View File

@ -0,0 +1,10 @@
=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts ===
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 0, 1))
>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 0, 41))
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 0, 1))
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 1, 1))
>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 1, 41))

View File

@ -0,0 +1,25 @@
=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts ===
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
>(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x] : (x: any) => string
>x : any
>({ "1": "one", "2": "two" } as { [key: string]: string })[x] : string
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
>"one" : "one"
>"two" : "two"
>key : string
>x : any
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;
>(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x : (x: any) => string
>x : any
>({ "1": "one", "2": "two" } as { [key: string]: string }).x : string
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
>"one" : "one"
>"two" : "two"
>key : string
>x : string

View File

@ -0,0 +1,4 @@
// @target: es5
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;

View File

@ -0,0 +1,4 @@
// @target: es6
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x];
(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x;