From 3c130d1317f588fb9a67f473f0e15fba931f4e99 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Fri, 27 Mar 2020 23:33:01 +0200 Subject: [PATCH] feat(37092): improve error message about missing default export (#37212) --- src/compiler/checker.ts | 37 +++++++++++++------ src/compiler/diagnosticMessages.json | 4 ++ .../reference/exportStar-amd.errors.txt | 1 + .../baselines/reference/exportStar.errors.txt | 1 + 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e0672300f1cd4..e0d8d8ec5b8d6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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) { @@ -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); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 195fc43dbb12d..c591e030fe46d 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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 diff --git a/tests/baselines/reference/exportStar-amd.errors.txt b/tests/baselines/reference/exportStar-amd.errors.txt index a667c90224e93..86a365b6a3bcc 100644 --- a/tests/baselines/reference/exportStar-amd.errors.txt +++ b/tests/baselines/reference/exportStar-amd.errors.txt @@ -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; diff --git a/tests/baselines/reference/exportStar.errors.txt b/tests/baselines/reference/exportStar.errors.txt index a667c90224e93..86a365b6a3bcc 100644 --- a/tests/baselines/reference/exportStar.errors.txt +++ b/tests/baselines/reference/exportStar.errors.txt @@ -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;