Skip to content

Commit

Permalink
feat(37092): improve error message about missing default export (#37212)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored Mar 27, 2020
1 parent 7f59949 commit 3c130d1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2388,18 +2388,7 @@ namespace ts {
));
}
else {
if (moduleSymbol.exports && moduleSymbol.exports.has(node.symbol.escapedName)) {
error(
node.name,
Diagnostics.Module_0_has_no_default_export_Did_you_mean_to_use_import_1_from_0_instead,
symbolToString(moduleSymbol),
symbolToString(node.symbol),
);
}
else {
error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol));
}

reportNonDefaultExport(moduleSymbol, node);
}
}
else if (hasSyntheticDefault) {
Expand All @@ -2413,6 +2402,30 @@ namespace ts {
}
}

function reportNonDefaultExport(moduleSymbol: Symbol, node: ImportClause) {
if (moduleSymbol.exports?.has(node.symbol.escapedName)) {
error(
node.name,
Diagnostics.Module_0_has_no_default_export_Did_you_mean_to_use_import_1_from_0_instead,
symbolToString(moduleSymbol),
symbolToString(node.symbol),
);
}
else {
const diagnostic = error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol));
const exportStar = moduleSymbol.exports?.get(InternalSymbolName.ExportStar);
if (exportStar) {
const defaultExport = find(exportStar.declarations, decl => !!(
isExportDeclaration(decl) && decl.moduleSpecifier &&
resolveExternalModuleName(decl, decl.moduleSpecifier)?.exports?.has(InternalSymbolName.Default)
));
if (defaultExport) {
addRelatedInfo(diagnostic, createDiagnosticForNode(defaultExport, Diagnostics.export_Asterisk_does_not_re_export_a_default));
}
}
}
}

function getTargetOfNamespaceImport(node: NamespaceImport, dontResolveAlias: boolean): Symbol | undefined {
const moduleSpecifier = node.parent.parent.moduleSpecifier;
const immediate = resolveExternalModuleName(node, moduleSpecifier);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,10 @@
"category": "Error",
"code": 1194
},
"'export *' does not re-export a default.": {
"category": "Error",
"code": 1195
},
"Catch clause variable cannot have a type annotation.": {
"category": "Error",
"code": 1196
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/exportStar-amd.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tests/cases/conformance/es6/modules/t4.ts(3,1): error TS2308: Module "./t1" has
import hello, { x, y, z, foo } from "./t4";
~~~~~
!!! error TS1192: Module '"tests/cases/conformance/es6/modules/t4"' has no default export.
!!! related TS1195 tests/cases/conformance/es6/modules/t4.ts:2:1: 'export *' does not re-export a default.
hello;
x;
y;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/exportStar.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tests/cases/conformance/es6/modules/t4.ts(3,1): error TS2308: Module "./t1" has
import hello, { x, y, z, foo } from "./t4";
~~~~~
!!! error TS1192: Module '"tests/cases/conformance/es6/modules/t4"' has no default export.
!!! related TS1195 tests/cases/conformance/es6/modules/t4.ts:2:1: 'export *' does not re-export a default.
hello;
x;
y;
Expand Down

0 comments on commit 3c130d1

Please sign in to comment.