Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(37092): Improve error when default-importing from a module that export *s from a module with a default export #37212

Merged
merged 2 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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