From 0a194f91a0a44c79bd5eb152825865940ccf407f Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 4 Apr 2017 11:15:03 -0700 Subject: [PATCH 1/4] Emit parenthesis around propert/element access expression of casted object literal expression --- src/compiler/transformers/ts.ts | 13 +++++++--- ...ssionOfCastedObjectLiteralExpressionES5.js | 7 ++++++ ...OfCastedObjectLiteralExpressionES5.symbols | 10 ++++++++ ...onOfCastedObjectLiteralExpressionES5.types | 25 +++++++++++++++++++ ...ssionOfCastedObjectLiteralExpressionES6.js | 7 ++++++ ...OfCastedObjectLiteralExpressionES6.symbols | 10 ++++++++ ...onOfCastedObjectLiteralExpressionES6.types | 25 +++++++++++++++++++ ...ssionOfCastedObjectLiteralExpressionES5.ts | 4 +++ ...ssionOfCastedObjectLiteralExpressionES6.ts | 4 +++ 9 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.js create mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.symbols create mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.types create mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.js create mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.symbols create mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.types create mode 100644 tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts create mode 100644 tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 6dc643b9543ee..50503e308d5c0 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2324,14 +2324,19 @@ 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. + const partialExpression = createPartiallyEmittedExpression(expression, node); + // 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); + // However, auto-parenthesization will not preserve parenthesis for the following case: ({ "1": "one", "2": "two" } as { [key: string]: string })[x]. + // so we have to manually preserve it here. + const shouldPreserveParen = (isPropertyAccessExpression(node.parent) || isElementAccessExpression(node.parent)) && + isObjectLiteralExpression((expression as PartiallyEmittedExpression).expression); + return shouldPreserveParen ? createParen(partialExpression) : partialExpression; } return visitEachChild(node, visitor, context); diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.js b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.js new file mode 100644 index 0000000000000..1b07e3304d033 --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.js @@ -0,0 +1,7 @@ +//// [emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts] +(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x]; +(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x; + +//// [emitAccessExpressionOfCastedObjectLiteralExpressionES5.js] +(function (x) { return ({ "1": "one", "2": "two" })[x]; }); +(function (x) { return ({ "1": "one", "2": "two" }).x; }); diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.symbols b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.symbols new file mode 100644 index 0000000000000..a106295ce09e5 --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.symbols @@ -0,0 +1,10 @@ +=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts === +(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x]; +>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 0, 1)) +>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 0, 41)) +>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 0, 1)) + +(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x; +>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 1, 1)) +>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 1, 41)) + diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.types b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.types new file mode 100644 index 0000000000000..82308ecfff9fe --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.types @@ -0,0 +1,25 @@ +=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.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 + diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.js b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.js new file mode 100644 index 0000000000000..7df1d4b8cf766 --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.js @@ -0,0 +1,7 @@ +//// [emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts] +(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x]; +(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x; + +//// [emitAccessExpressionOfCastedObjectLiteralExpressionES6.js] +(x) => ({ "1": "one", "2": "two" })[x]; +(x) => ({ "1": "one", "2": "two" }).x; diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.symbols b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.symbols new file mode 100644 index 0000000000000..0de1e97117c62 --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.symbols @@ -0,0 +1,10 @@ +=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts === +(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x]; +>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 0, 1)) +>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 0, 41)) +>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 0, 1)) + +(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x; +>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 1, 1)) +>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 1, 41)) + diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.types b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.types new file mode 100644 index 0000000000000..a669c6427cbf1 --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.types @@ -0,0 +1,25 @@ +=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.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 + diff --git a/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts new file mode 100644 index 0000000000000..31c7face08dbd --- /dev/null +++ b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts @@ -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; \ No newline at end of file diff --git a/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts new file mode 100644 index 0000000000000..ae3fe8a3f0156 --- /dev/null +++ b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts @@ -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; \ No newline at end of file From 82f3775956de1ac8ae0af7ff52871baf64b330b0 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 5 Apr 2017 10:13:10 -0700 Subject: [PATCH 2/4] Address PR: move the adding paren into factory function --- src/compiler/factory.ts | 3 ++- src/compiler/transformers/ts.ts | 9 ++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 66b3eb713e5fc..8d6390b6dc6dc 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -3492,7 +3492,8 @@ namespace ts { export function parenthesizeConciseBody(body: ConciseBody): ConciseBody { const emittedBody = skipPartiallyEmittedExpressions(body); - if (emittedBody.kind === SyntaxKind.ObjectLiteralExpression) { + const leftMostExpression = isExpression(emittedBody) ? getLeftmostExpression(emittedBody) : undefined; + if (leftMostExpression && leftMostExpression.kind === SyntaxKind.ObjectLiteralExpression) { return setTextRange(createParen(body), body); } diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 50503e308d5c0..8df760d51c08c 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2326,17 +2326,12 @@ namespace ts { // // To preserve comments, we return a "PartiallyEmittedExpression" here which will // preserve the position information of the original expression. - const partialExpression = createPartiallyEmittedExpression(expression, node); - + // // 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. - // However, auto-parenthesization will not preserve parenthesis for the following case: ({ "1": "one", "2": "two" } as { [key: string]: string })[x]. - // so we have to manually preserve it here. - const shouldPreserveParen = (isPropertyAccessExpression(node.parent) || isElementAccessExpression(node.parent)) && - isObjectLiteralExpression((expression as PartiallyEmittedExpression).expression); - return shouldPreserveParen ? createParen(partialExpression) : partialExpression; + return createPartiallyEmittedExpression(expression, node); } return visitEachChild(node, visitor, context); From 33524bd18c313b9fb0f2a47eda6e78733c7d4495 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 5 Apr 2017 10:26:28 -0700 Subject: [PATCH 3/4] Update test and baseline --- ...ssExpressionOfCastedObjectLiteralExpressionES5.js | 7 ------- ...ssExpressionOfCastedObjectLiteralExpressionES6.js | 7 ------- ...astedObjectLiteralExpressionInArrowFunctionES5.js | 7 +++++++ ...bjectLiteralExpressionInArrowFunctionES5.symbols} | 12 ++++++------ ...dObjectLiteralExpressionInArrowFunctionES5.types} | 2 +- ...astedObjectLiteralExpressionInArrowFunctionES6.js | 7 +++++++ ...bjectLiteralExpressionInArrowFunctionES6.symbols} | 12 ++++++------ ...dObjectLiteralExpressionInArrowFunctionES6.types} | 2 +- ...stedObjectLiteralExpressionInArrowFunctionES5.ts} | 0 ...stedObjectLiteralExpressionInArrowFunctionES6.ts} | 0 10 files changed, 28 insertions(+), 28 deletions(-) delete mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.js delete mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.js create mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.js rename tests/baselines/reference/{emitAccessExpressionOfCastedObjectLiteralExpressionES5.symbols => emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.symbols} (68%) rename tests/baselines/reference/{emitAccessExpressionOfCastedObjectLiteralExpressionES5.types => emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.types} (94%) create mode 100644 tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.js rename tests/baselines/reference/{emitAccessExpressionOfCastedObjectLiteralExpressionES6.symbols => emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.symbols} (68%) rename tests/baselines/reference/{emitAccessExpressionOfCastedObjectLiteralExpressionES6.types => emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.types} (94%) rename tests/cases/compiler/{emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts => emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts} (100%) rename tests/cases/compiler/{emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts => emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts} (100%) diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.js b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.js deleted file mode 100644 index 1b07e3304d033..0000000000000 --- a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.js +++ /dev/null @@ -1,7 +0,0 @@ -//// [emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts] -(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x]; -(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x; - -//// [emitAccessExpressionOfCastedObjectLiteralExpressionES5.js] -(function (x) { return ({ "1": "one", "2": "two" })[x]; }); -(function (x) { return ({ "1": "one", "2": "two" }).x; }); diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.js b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.js deleted file mode 100644 index 7df1d4b8cf766..0000000000000 --- a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.js +++ /dev/null @@ -1,7 +0,0 @@ -//// [emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts] -(x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x]; -(x) => ({ "1": "one", "2": "two" } as { [key: string]: string }).x; - -//// [emitAccessExpressionOfCastedObjectLiteralExpressionES6.js] -(x) => ({ "1": "one", "2": "two" })[x]; -(x) => ({ "1": "one", "2": "two" }).x; diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.js b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.js new file mode 100644 index 0000000000000..182e678b7bfc6 --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.js @@ -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); }); diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.symbols b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.symbols similarity index 68% rename from tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.symbols rename to tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.symbols index a106295ce09e5..36803f0049015 100644 --- a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.symbols +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.symbols @@ -1,10 +1,10 @@ -=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts === +=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts === (x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x]; ->x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 0, 1)) ->key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 0, 41)) ->x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 0, 1)) +>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(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 1, 1)) ->key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts, 1, 41)) +>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 1, 1)) +>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts, 1, 41)) diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.types b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.types similarity index 94% rename from tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.types rename to tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.types index 82308ecfff9fe..83637f261dee5 100644 --- a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES5.types +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.types @@ -1,4 +1,4 @@ -=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts === +=== 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 diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.js b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.js new file mode 100644 index 0000000000000..8d7999b0da767 --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.js @@ -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); diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.symbols b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.symbols similarity index 68% rename from tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.symbols rename to tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.symbols index 0de1e97117c62..20b10f84a22ac 100644 --- a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.symbols +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.symbols @@ -1,10 +1,10 @@ -=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts === +=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts === (x) => ({ "1": "one", "2": "two" } as { [key: string]: string })[x]; ->x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 0, 1)) ->key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 0, 41)) ->x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 0, 1)) +>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(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 1, 1)) ->key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts, 1, 41)) +>x : Symbol(x, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 1, 1)) +>key : Symbol(key, Decl(emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts, 1, 41)) diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.types b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.types similarity index 94% rename from tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.types rename to tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.types index a669c6427cbf1..f19255cd724eb 100644 --- a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionES6.types +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.types @@ -1,4 +1,4 @@ -=== tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts === +=== 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 diff --git a/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts similarity index 100% rename from tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES5.ts rename to tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts diff --git a/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts similarity index 100% rename from tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionES6.ts rename to tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts From 12549f69b085fb1ff463c4c38197e67d7f75a209 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 5 Apr 2017 12:14:46 -0700 Subject: [PATCH 4/4] Address PR --- src/compiler/factory.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 8d6390b6dc6dc..56b580f4f18bb 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -3491,9 +3491,7 @@ namespace ts { } export function parenthesizeConciseBody(body: ConciseBody): ConciseBody { - const emittedBody = skipPartiallyEmittedExpressions(body); - const leftMostExpression = isExpression(emittedBody) ? getLeftmostExpression(emittedBody) : undefined; - if (leftMostExpression && leftMostExpression.kind === SyntaxKind.ObjectLiteralExpression) { + if (!isBlock(body) && getLeftmostExpression(body).kind === SyntaxKind.ObjectLiteralExpression) { return setTextRange(createParen(body), body); }