diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index c215daf7935a6..7c8ec909eb57f 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -4183,11 +4183,15 @@ namespace ts { */ export function parenthesizeDefaultExpression(e: Expression) { const check = skipPartiallyEmittedExpressions(e); - return check.kind === SyntaxKind.ClassExpression || - check.kind === SyntaxKind.FunctionExpression || - isCommaSequence(check) - ? createParen(e) - : e; + let needsParens = isCommaSequence(check); + if (!needsParens) { + switch (getLeftmostExpression(check, /*stopAtCallExpression*/ false).kind) { + case SyntaxKind.ClassExpression: + case SyntaxKind.FunctionExpression: + needsParens = true; + } + } + return needsParens ? createParen(e) : e; } /** diff --git a/src/testRunner/unittests/factory.ts b/src/testRunner/unittests/factory.ts index 5324970ecd5fe..11ff65a855848 100644 --- a/src/testRunner/unittests/factory.ts +++ b/src/testRunner/unittests/factory.ts @@ -1,5 +1,32 @@ namespace ts { describe("FactoryAPI", () => { + describe("createExportAssignment", () => { + it("parenthesizes default export if necessary", () => { + function checkExpression(expression: Expression) { + const node = createExportAssignment( + /*decorators*/ undefined, + /*modifiers*/ undefined, + /*isExportEquals*/ false, + expression, + ); + assert.strictEqual(node.expression.kind, SyntaxKind.ParenthesizedExpression); + } + + const clazz = createClassExpression(/*modifiers*/ undefined, "C", /*typeParameters*/ undefined, /*heritageClauses*/ undefined, [ + createProperty(/*decorators*/ undefined, [createToken(SyntaxKind.StaticKeyword)], "prop", /*questionOrExclamationToken*/ undefined, /*type*/ undefined, createLiteral("1")), + ]); + checkExpression(clazz); + checkExpression(createPropertyAccess(clazz, "prop")); + + const func = createFunctionExpression(/*modifiers*/ undefined, /*asteriskToken*/ undefined, "fn", /*typeParameters*/ undefined, /*parameters*/ undefined, /*type*/ undefined, createBlock([])); + checkExpression(func); + checkExpression(createCall(func, /*typeArguments*/ undefined, /*argumentsArray*/ undefined)); + + checkExpression(createBinary(createLiteral("a"), SyntaxKind.CommaToken, createLiteral("b"))); + checkExpression(createCommaList([createLiteral("a"), createLiteral("b")])); + }); + }); + describe("createArrowFunction", () => { it("parenthesizes concise body if necessary", () => { function checkBody(body: ConciseBody) {