diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 66b3eb713e5fc..56b580f4f18bb 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -3491,8 +3491,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(body), body); } diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 6dc643b9543ee..8df760d51c08c 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -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); } 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/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.symbols b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.symbols new file mode 100644 index 0000000000000..36803f0049015 --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.symbols @@ -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)) + diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.types b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.types new file mode 100644 index 0000000000000..83637f261dee5 --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.types @@ -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 + 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/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.symbols b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.symbols new file mode 100644 index 0000000000000..20b10f84a22ac --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.symbols @@ -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)) + diff --git a/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.types b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.types new file mode 100644 index 0000000000000..f19255cd724eb --- /dev/null +++ b/tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.types @@ -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 + diff --git a/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.ts new file mode 100644 index 0000000000000..31c7face08dbd --- /dev/null +++ b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.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/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.ts new file mode 100644 index 0000000000000..ae3fe8a3f0156 --- /dev/null +++ b/tests/cases/compiler/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.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