From bfdb1cc3911298500a5d0a5b0e4d25037ae05ff2 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Sat, 30 Jun 2018 21:53:34 +0200 Subject: [PATCH 1/3] createExportAssignment: parenthesize nested class or function expression Fixes: #25222 --- src/compiler/factory.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index b8b77193ed067..25849cf6bd388 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -4185,12 +4185,16 @@ namespace ts { */ export function parenthesizeDefaultExpression(e: Expression) { const check = skipPartiallyEmittedExpressions(e); - return (check.kind === SyntaxKind.ClassExpression || - check.kind === SyntaxKind.FunctionExpression || - check.kind === SyntaxKind.CommaListExpression || - isBinaryExpression(check) && check.operatorToken.kind === SyntaxKind.CommaToken) - ? createParen(e) - : e; + let needsParens = check.kind === SyntaxKind.CommaListExpression || + isBinaryExpression(check) && check.operatorToken.kind === SyntaxKind.CommaToken; + if (!needsParens) { + switch (getLeftmostExpression(check, /*stopAtCallExpression*/ false).kind) { + case SyntaxKind.ClassExpression: + case SyntaxKind.FunctionExpression: + needsParens = true; + } + } + return needsParens ? createParen(e) : e; } /** From 3a84c028b5c44e2c56292be3fbdcc423b6511aa3 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Mon, 2 Jul 2018 17:58:38 +0200 Subject: [PATCH 2/3] add tests --- src/testRunner/tsconfig.json | 3 ++- src/testRunner/unittests/factory.ts | 30 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/testRunner/unittests/factory.ts diff --git a/src/testRunner/tsconfig.json b/src/testRunner/tsconfig.json index 3943d4f4f85ca..b02f1a6c17532 100644 --- a/src/testRunner/tsconfig.json +++ b/src/testRunner/tsconfig.json @@ -39,7 +39,7 @@ "unittests/extractTestHelpers.ts", "unittests/tsserverProjectSystem.ts", "unittests/typingsInstaller.ts", - + "unittests/asserts.ts", "unittests/base64.ts", "unittests/builder.ts", @@ -54,6 +54,7 @@ "unittests/extractConstants.ts", "unittests/extractFunctions.ts", "unittests/extractRanges.ts", + "unittests/factory.ts", "unittests/hostNewLineSupport.ts", "unittests/incrementalParser.ts", "unittests/initializeTSConfig.ts", diff --git a/src/testRunner/unittests/factory.ts b/src/testRunner/unittests/factory.ts new file mode 100644 index 0000000000000..3fe2fc29f6e89 --- /dev/null +++ b/src/testRunner/unittests/factory.ts @@ -0,0 +1,30 @@ +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, [ + ts.createProperty(/*decorators*/ undefined, [ts.createToken(ts.SyntaxKind.StaticKeyword)], "prop", /*questionOrExclamationToken*/ undefined, /*type*/ undefined, ts.createLiteral("1")), + ]); + checkExpression(clazz); + checkExpression(createPropertyAccess(clazz, "prop")); + + const func = createFunctionExpression(/*modifiers*/ undefined, /*asteriskToken*/ undefined, "fn", /*typeParameters*/ undefined, /*parameters*/ undefined, /*type*/ undefined, ts.createBlock([])); + checkExpression(func); + checkExpression(createCall(func, /*typeArguments*/ undefined, /*argumentsArray*/ undefined)); + + checkExpression(createBinary(createLiteral("a"), SyntaxKind.CommaToken, createLiteral("b"))); + checkExpression(createCommaList([createLiteral("a"), createLiteral("b")])); + }); + }); + }); +} From c39da7b0f4ddba80cccb845178d01a0036e784a5 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Mon, 2 Jul 2018 18:02:46 +0200 Subject: [PATCH 3/3] remove unnecessary qualifier --- src/testRunner/unittests/factory.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/testRunner/unittests/factory.ts b/src/testRunner/unittests/factory.ts index 3fe2fc29f6e89..2dc5e2f20ee79 100644 --- a/src/testRunner/unittests/factory.ts +++ b/src/testRunner/unittests/factory.ts @@ -13,12 +13,12 @@ namespace ts { } const clazz = createClassExpression(/*modifiers*/ undefined, "C", /*typeParameters*/ undefined, /*heritageClauses*/ undefined, [ - ts.createProperty(/*decorators*/ undefined, [ts.createToken(ts.SyntaxKind.StaticKeyword)], "prop", /*questionOrExclamationToken*/ undefined, /*type*/ undefined, ts.createLiteral("1")), + 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, ts.createBlock([])); + const func = createFunctionExpression(/*modifiers*/ undefined, /*asteriskToken*/ undefined, "fn", /*typeParameters*/ undefined, /*parameters*/ undefined, /*type*/ undefined, createBlock([])); checkExpression(func); checkExpression(createCall(func, /*typeArguments*/ undefined, /*argumentsArray*/ undefined));