Skip to content

Commit 1b24d47

Browse files
committed
Fix default class and function exports
1 parent ae5b72d commit 1b24d47

2 files changed

Lines changed: 56 additions & 74 deletions

File tree

src/compiler/core.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ namespace ts {
362362
: undefined;
363363
}
364364

365+
export function singleOrMany<T>(array: T[]): T | T[] {
366+
return array && array.length === 1
367+
? array[0]
368+
: array;
369+
}
370+
365371
/**
366372
* Returns the last element of an array if non-empty, undefined otherwise.
367373
*/

src/compiler/transformers/module/module.ts

Lines changed: 50 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ namespace ts {
178178
]);
179179
}
180180

181+
/**
182+
* Transforms a SourceFile into an AMD or UMD module body.
183+
*
184+
* @param node The SourceFile node.
185+
*/
181186
function transformAsynchronousModuleBody(node: SourceFile) {
182187
startLexicalEnvironment();
183188

@@ -195,7 +200,6 @@ namespace ts {
195200
addExportEqualsIfNeeded(statements, /*emitAsReturn*/ true);
196201

197202
const body = createBlock(statements, /*location*/ undefined, /*multiLine*/ true);
198-
199203
if (hasExportStars) {
200204
// If we have any `export * from ...` declarations
201205
// we need to inform the emitter to add the __export helper.
@@ -208,23 +212,17 @@ namespace ts {
208212
function addExportEqualsIfNeeded(statements: Statement[], emitAsReturn: boolean) {
209213
if (exportEquals && resolver.isValueAliasDeclaration(exportEquals)) {
210214
if (emitAsReturn) {
211-
statements.push(
212-
startOnNewLine(
213-
createReturn(exportEquals.expression)
214-
)
215-
);
215+
statements.push(createReturn(exportEquals.expression));
216216
}
217217
else {
218218
statements.push(
219-
startOnNewLine(
220-
createStatement(
221-
createAssignment(
222-
createPropertyAccess(
223-
createIdentifier("module"),
224-
"exports"
225-
),
226-
exportEquals.expression
227-
)
219+
createStatement(
220+
createAssignment(
221+
createPropertyAccess(
222+
createIdentifier("module"),
223+
"exports"
224+
),
225+
exportEquals.expression
228226
)
229227
)
230228
);
@@ -497,7 +495,7 @@ namespace ts {
497495
return createStatement(
498496
createObjectDefineProperty(
499497
createIdentifier("exports"),
500-
createLiteral("_esModule"),
498+
createLiteral("__esModule"),
501499
{ value: createLiteral(true) }
502500
)
503501
);
@@ -540,7 +538,7 @@ namespace ts {
540538

541539
function addExportMemberAssignment(statements: Statement[], node: FunctionDeclaration | ClassDeclaration) {
542540
if (hasModifier(node, ModifierFlags.Default)) {
543-
addExportDefault(statements, getSynthesizedClone(node.name), /*location*/ node);
541+
addExportDefault(statements, node.name ? getSynthesizedClone(node.name) : getGeneratedNameForNode(node), /*location*/ node);
544542
}
545543
else {
546544
statements.push(
@@ -592,79 +590,57 @@ namespace ts {
592590

593591
function visitFunctionDeclaration(node: FunctionDeclaration): VisitResult<Statement> {
594592
const statements: Statement[] = [];
595-
if (node.name) {
596-
if (hasModifier(node, ModifierFlags.Export)) {
597-
statements.push(
598-
createFunctionDeclaration(
599-
/*modifiers*/ undefined,
600-
/*asteriskToken*/ undefined,
601-
node.name,
602-
node.parameters,
603-
node.body,
604-
/*location*/ node
605-
)
606-
);
607-
608-
addExportMemberAssignment(statements, node);
609-
}
610-
else {
611-
statements.push(node);
612-
}
613-
614-
addExportMemberAssignments(statements, node.name);
615-
}
616-
else {
617-
Debug.assert(hasModifier(node, ModifierFlags.Default));
618-
addExportDefault(statements,
619-
createFunctionExpression(
593+
const name = node.name || getGeneratedNameForNode(node);
594+
if (hasModifier(node, ModifierFlags.Export)) {
595+
statements.push(
596+
createFunctionDeclaration(
597+
/*modifiers*/ undefined,
620598
/*asteriskToken*/ undefined,
621-
/*name*/ undefined,
599+
name,
622600
node.parameters,
623601
node.body,
624602
/*location*/ node
625-
),
626-
/*location*/ node
603+
)
627604
);
605+
606+
addExportMemberAssignment(statements, node);
607+
}
608+
else {
609+
statements.push(node);
628610
}
629-
return statements;
630-
}
631611

632-
function visitClassDeclaration(node: ClassDeclaration): VisitResult<Statement> {
633-
const statements: Statement[] = [];
634612
if (node.name) {
635-
if (hasModifier(node, ModifierFlags.Export)) {
636-
statements.push(
637-
createClassDeclaration(
638-
/*modifiers*/ undefined,
639-
node.name,
640-
node.heritageClauses,
641-
node.members,
642-
/*location*/ node
643-
)
644-
);
645-
646-
addExportMemberAssignment(statements, node);
647-
}
648-
else {
649-
addNode(statements, node);
650-
}
651-
652613
addExportMemberAssignments(statements, node.name);
653614
}
654-
else {
655-
Debug.assert(hasModifier(node, ModifierFlags.Default));
656-
addExportDefault(statements,
657-
createClassExpression(
658-
/*name*/ undefined,
615+
616+
return singleOrMany(statements);
617+
}
618+
619+
function visitClassDeclaration(node: ClassDeclaration): VisitResult<Statement> {
620+
const statements: Statement[] = [];
621+
const name = node.name || getGeneratedNameForNode(node);
622+
if (hasModifier(node, ModifierFlags.Export)) {
623+
statements.push(
624+
createClassDeclaration(
625+
/*modifiers*/ undefined,
626+
name,
659627
node.heritageClauses,
660628
node.members,
661629
/*location*/ node
662-
),
663-
/*location*/ node
630+
)
664631
);
632+
633+
addExportMemberAssignment(statements, node);
634+
}
635+
else {
636+
statements.push(node);
665637
}
666638

667-
return statements;
639+
if (node.name) {
640+
addExportMemberAssignments(statements, node.name);
641+
}
642+
643+
return singleOrMany(statements);
668644
}
669645

670646
function substituteExpression(node: Expression) {

0 commit comments

Comments
 (0)