Skip to content

Commit fd007e7

Browse files
authored
Merge pull request microsoft#25341 from ajafff/factory-export-default
createExportAssignment: parenthesize nested class or function expression
2 parents 1785041 + 25610de commit fd007e7

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/compiler/factory.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -4183,11 +4183,15 @@ namespace ts {
41834183
*/
41844184
export function parenthesizeDefaultExpression(e: Expression) {
41854185
const check = skipPartiallyEmittedExpressions(e);
4186-
return check.kind === SyntaxKind.ClassExpression ||
4187-
check.kind === SyntaxKind.FunctionExpression ||
4188-
isCommaSequence(check)
4189-
? createParen(e)
4190-
: e;
4186+
let needsParens = isCommaSequence(check);
4187+
if (!needsParens) {
4188+
switch (getLeftmostExpression(check, /*stopAtCallExpression*/ false).kind) {
4189+
case SyntaxKind.ClassExpression:
4190+
case SyntaxKind.FunctionExpression:
4191+
needsParens = true;
4192+
}
4193+
}
4194+
return needsParens ? createParen(e) : e;
41914195
}
41924196

41934197
/**

src/testRunner/unittests/factory.ts

+27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
namespace ts {
22
describe("FactoryAPI", () => {
3+
describe("createExportAssignment", () => {
4+
it("parenthesizes default export if necessary", () => {
5+
function checkExpression(expression: Expression) {
6+
const node = createExportAssignment(
7+
/*decorators*/ undefined,
8+
/*modifiers*/ undefined,
9+
/*isExportEquals*/ false,
10+
expression,
11+
);
12+
assert.strictEqual(node.expression.kind, SyntaxKind.ParenthesizedExpression);
13+
}
14+
15+
const clazz = createClassExpression(/*modifiers*/ undefined, "C", /*typeParameters*/ undefined, /*heritageClauses*/ undefined, [
16+
createProperty(/*decorators*/ undefined, [createToken(SyntaxKind.StaticKeyword)], "prop", /*questionOrExclamationToken*/ undefined, /*type*/ undefined, createLiteral("1")),
17+
]);
18+
checkExpression(clazz);
19+
checkExpression(createPropertyAccess(clazz, "prop"));
20+
21+
const func = createFunctionExpression(/*modifiers*/ undefined, /*asteriskToken*/ undefined, "fn", /*typeParameters*/ undefined, /*parameters*/ undefined, /*type*/ undefined, createBlock([]));
22+
checkExpression(func);
23+
checkExpression(createCall(func, /*typeArguments*/ undefined, /*argumentsArray*/ undefined));
24+
25+
checkExpression(createBinary(createLiteral("a"), SyntaxKind.CommaToken, createLiteral("b")));
26+
checkExpression(createCommaList([createLiteral("a"), createLiteral("b")]));
27+
});
28+
});
29+
330
describe("createArrowFunction", () => {
431
it("parenthesizes concise body if necessary", () => {
532
function checkBody(body: ConciseBody) {

0 commit comments

Comments
 (0)