mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-09 02:30:15 -06:00
Merge pull request #25341 from ajafff/factory-export-default
createExportAssignment: parenthesize nested class or function expression
This commit is contained in:
commit
fd007e747d
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user