diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 66b3eb713e5..8d6390b6dc6 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 50503e308d5..8df760d51c0 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);