Skip to content

Commit f16d599

Browse files
author
Kanchalai Tanglertsampan
committed
Issues an error when there are more than one export default
1 parent 669191e commit f16d599

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/compiler/binder.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,18 @@ namespace ts {
337337
? Diagnostics.Cannot_redeclare_block_scoped_variable_0
338338
: Diagnostics.Duplicate_identifier_0;
339339

340+
// If the current node has NodeFlags.Default (e.g. if the node is class declaration or function declaration)
341+
// and there is already another default export (i.e. symbol.declarations is not empty), we need to report such error
342+
if (isDefaultExport && symbol.declarations) {
343+
message = Diagnostics.A_module_cannot_have_multiple_default_exports;
344+
}
345+
340346
forEach(symbol.declarations, declaration => {
341-
if (declaration.flags & NodeFlags.Default) {
347+
// Error on multiple export default in the following case:
348+
// 1. multiple export default of class declaration or function declaration by checking NodeFlags.Default
349+
// 2. multiple export default of export assignment. This one doesn't have NodeFlags.Default on (as export default doesn't considered as modifiers)
350+
if ((declaration.flags & NodeFlags.Default) ||
351+
(declaration.kind === SyntaxKind.ExportAssignment && !(<ExportAssignment>node).isExportEquals)) {
342352
message = Diagnostics.A_module_cannot_have_multiple_default_exports;
343353
}
344354
});
@@ -1898,7 +1908,9 @@ namespace ts {
18981908
}
18991909
else {
19001910
// An export default clause with an expression exports a value
1901-
declareSymbol(container.symbol.exports, container.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes | SymbolFlags.AliasExcludes);
1911+
// We want to exclude both class and function here, this is necessary to issue an error when there are both
1912+
// default export-assignment and default export function and class declaration.
1913+
declareSymbol(container.symbol.exports, container.symbol, node, SymbolFlags.Property, SymbolFlags.Class | SymbolFlags.Function | SymbolFlags.Property);
19021914
}
19031915
}
19041916

0 commit comments

Comments
 (0)