Skip to content

Commit 751f20e

Browse files
author
Andy
authored
Don't bother trying to get the name of a default export (#25773)
* Don't bother trying to get the name of a default export * Fix lint
1 parent d92c26d commit 751f20e

File tree

6 files changed

+45
-5
lines changed

6 files changed

+45
-5
lines changed

src/compiler/binder.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ namespace ts {
306306
}
307307

308308
function getDisplayName(node: Declaration): string {
309-
return isNamedDeclaration(node) ? declarationNameToString(node.name) : unescapeLeadingUnderscores(getDeclarationName(node)!); // TODO: GH#18217
309+
return isNamedDeclaration(node) ? declarationNameToString(node.name) : unescapeLeadingUnderscores(Debug.assertDefined(getDeclarationName(node)));
310310
}
311311

312312
/**
@@ -383,9 +383,11 @@ namespace ts {
383383
let message = symbol.flags & SymbolFlags.BlockScopedVariable
384384
? Diagnostics.Cannot_redeclare_block_scoped_variable_0
385385
: Diagnostics.Duplicate_identifier_0;
386+
let messageNeedsName = true;
386387

387388
if (symbol.flags & SymbolFlags.Enum || includes & SymbolFlags.Enum) {
388389
message = Diagnostics.Enum_declarations_can_only_merge_with_namespace_or_other_enum_declarations;
390+
messageNeedsName = false;
389391
}
390392

391393
if (symbol.declarations && symbol.declarations.length) {
@@ -394,6 +396,7 @@ namespace ts {
394396
// We'll know whether we have other default exports depending on if `symbol` already has a declaration list set.
395397
if (isDefaultExport) {
396398
message = Diagnostics.A_module_cannot_have_multiple_default_exports;
399+
messageNeedsName = false;
397400
}
398401
else {
399402
// This is to properly report an error in the case "export default { }" is after export default of class declaration or function declaration.
@@ -403,14 +406,16 @@ namespace ts {
403406
if (symbol.declarations && symbol.declarations.length &&
404407
(node.kind === SyntaxKind.ExportAssignment && !(<ExportAssignment>node).isExportEquals)) {
405408
message = Diagnostics.A_module_cannot_have_multiple_default_exports;
409+
messageNeedsName = false;
406410
}
407411
}
408412
}
409413

410-
forEach(symbol.declarations, declaration => {
411-
file.bindDiagnostics.push(createDiagnosticForNode(getNameOfDeclaration(declaration) || declaration, message, getDisplayName(declaration)));
412-
});
413-
file.bindDiagnostics.push(createDiagnosticForNode(getNameOfDeclaration(node) || node, message, getDisplayName(node)));
414+
const addError = (decl: Declaration): void => {
415+
file.bindDiagnostics.push(createDiagnosticForNode(getNameOfDeclaration(decl) || decl, message, messageNeedsName ? getDisplayName(decl) : undefined));
416+
};
417+
forEach(symbol.declarations, addError);
418+
addError(node);
414419

415420
symbol = createSymbol(SymbolFlags.None, name);
416421
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/duplicateDefaultExport.ts(1,1): error TS2528: A module cannot have multiple default exports.
2+
tests/cases/compiler/duplicateDefaultExport.ts(2,1): error TS2528: A module cannot have multiple default exports.
3+
4+
5+
==== tests/cases/compiler/duplicateDefaultExport.ts (2 errors) ====
6+
export default 0;
7+
~~~~~~~~~~~~~~~~~
8+
!!! error TS2528: A module cannot have multiple default exports.
9+
export default function() {}
10+
~~~~~~
11+
!!! error TS2528: A module cannot have multiple default exports.
12+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [duplicateDefaultExport.ts]
2+
export default 0;
3+
export default function() {}
4+
5+
6+
//// [duplicateDefaultExport.js]
7+
"use strict";
8+
exports.__esModule = true;
9+
exports["default"] = 0;
10+
function default_1() { }
11+
exports["default"] = default_1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/duplicateDefaultExport.ts ===
2+
export default 0;
3+
No type information for this code.export default function() {}
4+
No type information for this code.
5+
No type information for this code.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/duplicateDefaultExport.ts ===
2+
export default 0;
3+
No type information for this code.export default function() {}
4+
No type information for this code.
5+
No type information for this code.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default 0;
2+
export default function() {}

0 commit comments

Comments
 (0)