@@ -2798,44 +2798,66 @@ namespace ts {
27982798 function getTargetOfImportClause(node: ImportClause, dontResolveAlias: boolean): Symbol | undefined {
27992799 const moduleSymbol = resolveExternalModuleName(node, node.parent.moduleSpecifier);
28002800 if (moduleSymbol) {
2801- let exportDefaultSymbol: Symbol | undefined;
2802- if (isShorthandAmbientModuleSymbol(moduleSymbol)) {
2803- exportDefaultSymbol = moduleSymbol;
2804- }
2805- else {
2806- exportDefaultSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias);
2807- }
2808-
2809- const file = moduleSymbol.declarations?.find(isSourceFile);
2810- const hasDefaultOnly = isOnlyImportedAsDefault(node.parent.moduleSpecifier);
2811- const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, node.parent.moduleSpecifier);
2812- if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) {
2813- if (hasExportAssignmentSymbol(moduleSymbol)) {
2814- const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop";
2815- const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals);
2816- const exportAssignment = exportEqualsSymbol!.valueDeclaration;
2817- const err = error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), compilerOptionName);
2818-
2819- if (exportAssignment) {
2820- addRelatedInfo(err, createDiagnosticForNode(
2821- exportAssignment,
2822- Diagnostics.This_module_is_declared_with_export_and_can_only_be_used_with_a_default_import_when_using_the_0_flag,
2823- compilerOptionName
2824- ));
2825- }
2826- }
2827- else {
2828- reportNonDefaultExport(moduleSymbol, node);
2801+ return getTargetofModuleDefault(moduleSymbol, node, dontResolveAlias);
2802+ }
2803+ }
2804+
2805+ function getTargetofModuleDefault(moduleSymbol: Symbol, node: ImportClause | ImportOrExportSpecifier, dontResolveAlias: boolean) {
2806+ let exportDefaultSymbol: Symbol | undefined;
2807+ if (isShorthandAmbientModuleSymbol(moduleSymbol)) {
2808+ exportDefaultSymbol = moduleSymbol;
2809+ }
2810+ else {
2811+ exportDefaultSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias);
2812+ }
2813+
2814+ const file = moduleSymbol.declarations?.find(isSourceFile);
2815+ const specifier = getModuleSpecifierForImportOrExport(node);
2816+ if (!specifier) {
2817+ return exportDefaultSymbol;
2818+ }
2819+ const hasDefaultOnly = isOnlyImportedAsDefault(specifier);
2820+ const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, specifier);
2821+ if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) {
2822+ if (hasExportAssignmentSymbol(moduleSymbol)) {
2823+ const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop";
2824+ const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals);
2825+ const exportAssignment = exportEqualsSymbol!.valueDeclaration;
2826+ const err = error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), compilerOptionName);
2827+
2828+ if (exportAssignment) {
2829+ addRelatedInfo(err, createDiagnosticForNode(
2830+ exportAssignment,
2831+ Diagnostics.This_module_is_declared_with_export_and_can_only_be_used_with_a_default_import_when_using_the_0_flag,
2832+ compilerOptionName
2833+ ));
28292834 }
28302835 }
2831- else if (hasSyntheticDefault || hasDefaultOnly) {
2832- // per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present
2833- const resolved = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
2834- markSymbolOfAliasDeclarationIfTypeOnly(node, moduleSymbol, resolved, /*overwriteTypeOnly*/ false);
2835- return resolved;
2836+ else if (isImportClause(node)) {
2837+ reportNonDefaultExport(moduleSymbol, node);
28362838 }
2837- markSymbolOfAliasDeclarationIfTypeOnly(node, exportDefaultSymbol, /*finalTarget*/ undefined, /*overwriteTypeOnly*/ false);
2838- return exportDefaultSymbol;
2839+ else {
2840+ errorNoModuleMemberSymbol(moduleSymbol, moduleSymbol, node, isImportOrExportSpecifier(node) && node.propertyName || node.name);
2841+ }
2842+ }
2843+ else if (hasSyntheticDefault || hasDefaultOnly) {
2844+ // per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present
2845+ const resolved = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
2846+ markSymbolOfAliasDeclarationIfTypeOnly(node, moduleSymbol, resolved, /*overwriteTypeOnly*/ false);
2847+ return resolved;
2848+ }
2849+ markSymbolOfAliasDeclarationIfTypeOnly(node, exportDefaultSymbol, /*finalTarget*/ undefined, /*overwriteTypeOnly*/ false);
2850+ return exportDefaultSymbol;
2851+ }
2852+
2853+ function getModuleSpecifierForImportOrExport(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportOrExportSpecifier): Expression | undefined {
2854+ switch (node.kind) {
2855+ case SyntaxKind.ImportClause: return node.parent.moduleSpecifier;
2856+ case SyntaxKind.ImportEqualsDeclaration: return isExternalModuleReference(node.moduleReference) ? node.moduleReference.expression : undefined;
2857+ case SyntaxKind.NamespaceImport: return node.parent.parent.moduleSpecifier;
2858+ case SyntaxKind.ImportSpecifier: return node.parent.parent.parent.moduleSpecifier;
2859+ case SyntaxKind.ExportSpecifier: return node.parent.parent.moduleSpecifier;
2860+ default: return Debug.assertNever(node);
28392861 }
28402862 }
28412863
@@ -2969,38 +2991,42 @@ namespace ts {
29692991 combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) :
29702992 symbolFromModule || symbolFromVariable;
29712993 if (!symbol) {
2972- const moduleName = getFullyQualifiedName(moduleSymbol, node);
2973- const declarationName = declarationNameToString(name);
2974- const suggestion = getSuggestedSymbolForNonexistentModule(name, targetSymbol);
2975- if (suggestion !== undefined) {
2976- const suggestionName = symbolToString(suggestion);
2977- const diagnostic = error(name, Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2, moduleName, declarationName, suggestionName);
2978- if (suggestion.valueDeclaration) {
2979- addRelatedInfo(diagnostic,
2980- createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestionName)
2981- );
2982- }
2983- }
2984- else {
2985- if (moduleSymbol.exports?.has(InternalSymbolName.Default)) {
2986- error(
2987- name,
2988- Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead,
2989- moduleName,
2990- declarationName
2991- );
2992- }
2993- else {
2994- reportNonExportedMember(node, name, declarationName, moduleSymbol, moduleName);
2995- }
2996- }
2994+ errorNoModuleMemberSymbol(moduleSymbol, targetSymbol, node, name);
29972995 }
29982996 return symbol;
29992997 }
30002998 }
30012999 }
30023000
3003- function reportNonExportedMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration, name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void {
3001+ function errorNoModuleMemberSymbol(moduleSymbol: Symbol, targetSymbol: Symbol, node: Node, name: Identifier) {
3002+ const moduleName = getFullyQualifiedName(moduleSymbol, node);
3003+ const declarationName = declarationNameToString(name);
3004+ const suggestion = getSuggestedSymbolForNonexistentModule(name, targetSymbol);
3005+ if (suggestion !== undefined) {
3006+ const suggestionName = symbolToString(suggestion);
3007+ const diagnostic = error(name, Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2, moduleName, declarationName, suggestionName);
3008+ if (suggestion.valueDeclaration) {
3009+ addRelatedInfo(diagnostic,
3010+ createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestionName)
3011+ );
3012+ }
3013+ }
3014+ else {
3015+ if (moduleSymbol.exports?.has(InternalSymbolName.Default)) {
3016+ error(
3017+ name,
3018+ Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead,
3019+ moduleName,
3020+ declarationName
3021+ );
3022+ }
3023+ else {
3024+ reportNonExportedMember(node, name, declarationName, moduleSymbol, moduleName);
3025+ }
3026+ }
3027+ }
3028+
3029+ function reportNonExportedMember(node: Node, name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void {
30043030 const localSymbol = moduleSymbol.valueDeclaration?.locals?.get(name.escapedText);
30053031 const exports = moduleSymbol.exports;
30063032 if (localSymbol) {
@@ -3025,7 +3051,7 @@ namespace ts {
30253051 }
30263052 }
30273053
3028- function reportInvalidImportEqualsExportMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration , name: Identifier, declarationName: string, moduleName: string) {
3054+ function reportInvalidImportEqualsExportMember(node: Node , name: Identifier, declarationName: string, moduleName: string) {
30293055 if (moduleKind >= ModuleKind.ES2015) {
30303056 const message = getESModuleInterop(compilerOptions) ? Diagnostics._0_can_only_be_imported_by_using_a_default_import :
30313057 Diagnostics._0_can_only_be_imported_by_turning_on_the_esModuleInterop_flag_and_using_a_default_import;
@@ -3046,6 +3072,13 @@ namespace ts {
30463072 }
30473073
30483074 function getTargetOfImportSpecifier(node: ImportSpecifier | BindingElement, dontResolveAlias: boolean): Symbol | undefined {
3075+ if (isImportSpecifier(node) && idText(node.propertyName || node.name) === InternalSymbolName.Default) {
3076+ const specifier = getModuleSpecifierForImportOrExport(node);
3077+ const moduleSymbol = specifier && resolveExternalModuleName(node, specifier);
3078+ if (moduleSymbol) {
3079+ return getTargetofModuleDefault(moduleSymbol, node, dontResolveAlias);
3080+ }
3081+ }
30493082 const root = isBindingElement(node) ? getRootDeclaration(node) as VariableDeclaration : node.parent.parent.parent;
30503083 const commonJSPropertyAccess = getCommonJSPropertyAccess(root);
30513084 const resolved = getExternalModuleMember(root, commonJSPropertyAccess || node, dontResolveAlias);
@@ -3070,6 +3103,13 @@ namespace ts {
30703103 }
30713104
30723105 function getTargetOfExportSpecifier(node: ExportSpecifier, meaning: SymbolFlags, dontResolveAlias?: boolean) {
3106+ if (idText(node.propertyName || node.name) === InternalSymbolName.Default) {
3107+ const specifier = getModuleSpecifierForImportOrExport(node);
3108+ const moduleSymbol = specifier && resolveExternalModuleName(node, specifier);
3109+ if (moduleSymbol) {
3110+ return getTargetofModuleDefault(moduleSymbol, node, !!dontResolveAlias);
3111+ }
3112+ }
30733113 const resolved = node.parent.parent.moduleSpecifier ?
30743114 getExternalModuleMember(node.parent.parent, node, dontResolveAlias) :
30753115 resolveEntityName(node.propertyName || node.name, meaning, /*ignoreErrors*/ false, dontResolveAlias);
0 commit comments