@@ -2762,44 +2762,66 @@ namespace ts {
27622762 function getTargetOfImportClause(node: ImportClause, dontResolveAlias: boolean): Symbol | undefined {
27632763 const moduleSymbol = resolveExternalModuleName(node, node.parent.moduleSpecifier);
27642764 if (moduleSymbol) {
2765- let exportDefaultSymbol: Symbol | undefined;
2766- if (isShorthandAmbientModuleSymbol(moduleSymbol)) {
2767- exportDefaultSymbol = moduleSymbol;
2768- }
2769- else {
2770- exportDefaultSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias);
2771- }
2772-
2773- const file = moduleSymbol.declarations?.find(isSourceFile);
2774- const hasDefaultOnly = isOnlyImportedAsDefault(node.parent.moduleSpecifier);
2775- const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, node.parent.moduleSpecifier);
2776- if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) {
2777- if (hasExportAssignmentSymbol(moduleSymbol)) {
2778- const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop";
2779- const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals);
2780- const exportAssignment = exportEqualsSymbol!.valueDeclaration;
2781- const err = error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), compilerOptionName);
2782-
2783- if (exportAssignment) {
2784- addRelatedInfo(err, createDiagnosticForNode(
2785- exportAssignment,
2786- Diagnostics.This_module_is_declared_with_using_export_and_can_only_be_used_with_a_default_import_when_using_the_0_flag,
2787- compilerOptionName
2788- ));
2789- }
2790- }
2791- else {
2792- reportNonDefaultExport(moduleSymbol, node);
2765+ return getTargetofModuleDefault(moduleSymbol, node, dontResolveAlias);
2766+ }
2767+ }
2768+
2769+ function getTargetofModuleDefault(moduleSymbol: Symbol, node: TypeOnlyCompatibleAliasDeclaration, dontResolveAlias: boolean) {
2770+ let exportDefaultSymbol: Symbol | undefined;
2771+ if (isShorthandAmbientModuleSymbol(moduleSymbol)) {
2772+ exportDefaultSymbol = moduleSymbol;
2773+ }
2774+ else {
2775+ exportDefaultSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias);
2776+ }
2777+
2778+ const file = moduleSymbol.declarations?.find(isSourceFile);
2779+ const specifier = getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node);
2780+ if (!specifier) {
2781+ return exportDefaultSymbol;
2782+ }
2783+ const hasDefaultOnly = isOnlyImportedAsDefault(specifier);
2784+ const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, specifier);
2785+ if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) {
2786+ if (hasExportAssignmentSymbol(moduleSymbol)) {
2787+ const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop";
2788+ const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals);
2789+ const exportAssignment = exportEqualsSymbol!.valueDeclaration;
2790+ const err = error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), compilerOptionName);
2791+
2792+ if (exportAssignment) {
2793+ addRelatedInfo(err, createDiagnosticForNode(
2794+ exportAssignment,
2795+ Diagnostics.This_module_is_declared_with_using_export_and_can_only_be_used_with_a_default_import_when_using_the_0_flag,
2796+ compilerOptionName
2797+ ));
27932798 }
27942799 }
2795- else if (hasSyntheticDefault || hasDefaultOnly) {
2796- // per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present
2797- const resolved = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
2798- markSymbolOfAliasDeclarationIfTypeOnly(node, moduleSymbol, resolved, /*overwriteTypeOnly*/ false);
2799- return resolved;
2800+ else if (isImportClause(node)) {
2801+ reportNonDefaultExport(moduleSymbol, node);
28002802 }
2801- markSymbolOfAliasDeclarationIfTypeOnly(node, exportDefaultSymbol, /*finalTarget*/ undefined, /*overwriteTypeOnly*/ false);
2802- return exportDefaultSymbol;
2803+ else {
2804+ errorNoModuleMemberSymbol(moduleSymbol, moduleSymbol, node, isImportOrExportSpecifier(node) && node.propertyName || node.name);
2805+ }
2806+ }
2807+ else if (hasSyntheticDefault || hasDefaultOnly) {
2808+ // per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present
2809+ const resolved = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
2810+ markSymbolOfAliasDeclarationIfTypeOnly(node, moduleSymbol, resolved, /*overwriteTypeOnly*/ false);
2811+ return resolved;
2812+ }
2813+ markSymbolOfAliasDeclarationIfTypeOnly(node, exportDefaultSymbol, /*finalTarget*/ undefined, /*overwriteTypeOnly*/ false);
2814+ return exportDefaultSymbol;
2815+ }
2816+
2817+ function getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node: TypeOnlyCompatibleAliasDeclaration): Expression | undefined {
2818+ switch (node.kind) {
2819+ case SyntaxKind.ImportClause: return node.parent.moduleSpecifier;
2820+ case SyntaxKind.ImportEqualsDeclaration: return isExternalModuleReference(node.moduleReference) ? node.moduleReference.expression : undefined;
2821+ case SyntaxKind.NamespaceImport: return node.parent.parent.moduleSpecifier;
2822+ case SyntaxKind.ImportSpecifier: return node.parent.parent.parent.moduleSpecifier;
2823+ case SyntaxKind.ExportSpecifier: return node.parent.parent.moduleSpecifier;
2824+ default: return Debug.assertNever(node);
28032825 }
28042826 }
28052827
@@ -2933,38 +2955,42 @@ namespace ts {
29332955 combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) :
29342956 symbolFromModule || symbolFromVariable;
29352957 if (!symbol) {
2936- const moduleName = getFullyQualifiedName(moduleSymbol, node);
2937- const declarationName = declarationNameToString(name);
2938- const suggestion = getSuggestedSymbolForNonexistentModule(name, targetSymbol);
2939- if (suggestion !== undefined) {
2940- const suggestionName = symbolToString(suggestion);
2941- const diagnostic = error(name, Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2, moduleName, declarationName, suggestionName);
2942- if (suggestion.valueDeclaration) {
2943- addRelatedInfo(diagnostic,
2944- createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestionName)
2945- );
2946- }
2947- }
2948- else {
2949- if (moduleSymbol.exports?.has(InternalSymbolName.Default)) {
2950- error(
2951- name,
2952- Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead,
2953- moduleName,
2954- declarationName
2955- );
2956- }
2957- else {
2958- reportNonExportedMember(node, name, declarationName, moduleSymbol, moduleName);
2959- }
2960- }
2958+ errorNoModuleMemberSymbol(moduleSymbol, targetSymbol, node, name);
29612959 }
29622960 return symbol;
29632961 }
29642962 }
29652963 }
29662964
2967- function reportNonExportedMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration, name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void {
2965+ function errorNoModuleMemberSymbol(moduleSymbol: Symbol, targetSymbol: Symbol, node: Node, name: Identifier) {
2966+ const moduleName = getFullyQualifiedName(moduleSymbol, node);
2967+ const declarationName = declarationNameToString(name);
2968+ const suggestion = getSuggestedSymbolForNonexistentModule(name, targetSymbol);
2969+ if (suggestion !== undefined) {
2970+ const suggestionName = symbolToString(suggestion);
2971+ const diagnostic = error(name, Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2, moduleName, declarationName, suggestionName);
2972+ if (suggestion.valueDeclaration) {
2973+ addRelatedInfo(diagnostic,
2974+ createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestionName)
2975+ );
2976+ }
2977+ }
2978+ else {
2979+ if (moduleSymbol.exports?.has(InternalSymbolName.Default)) {
2980+ error(
2981+ name,
2982+ Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead,
2983+ moduleName,
2984+ declarationName
2985+ );
2986+ }
2987+ else {
2988+ reportNonExportedMember(node, name, declarationName, moduleSymbol, moduleName);
2989+ }
2990+ }
2991+ }
2992+
2993+ function reportNonExportedMember(node: Node, name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void {
29682994 const localSymbol = moduleSymbol.valueDeclaration?.locals?.get(name.escapedText);
29692995 const exports = moduleSymbol.exports;
29702996 if (localSymbol) {
@@ -2989,7 +3015,7 @@ namespace ts {
29893015 }
29903016 }
29913017
2992- function reportInvalidImportEqualsExportMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration , name: Identifier, declarationName: string, moduleName: string) {
3018+ function reportInvalidImportEqualsExportMember(node: Node , name: Identifier, declarationName: string, moduleName: string) {
29933019 if (moduleKind >= ModuleKind.ES2015) {
29943020 const message = getESModuleInterop(compilerOptions) ? Diagnostics._0_can_only_be_imported_by_using_a_default_import :
29953021 Diagnostics._0_can_only_be_imported_by_turning_on_the_esModuleInterop_flag_and_using_a_default_import;
@@ -3010,6 +3036,13 @@ namespace ts {
30103036 }
30113037
30123038 function getTargetOfImportSpecifier(node: ImportSpecifier | BindingElement, dontResolveAlias: boolean): Symbol | undefined {
3039+ if (isImportSpecifier(node) && idText(node.propertyName || node.name) === InternalSymbolName.Default) {
3040+ const specifier = getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node);
3041+ const moduleSymbol = specifier && resolveExternalModuleName(node, specifier);
3042+ if (moduleSymbol) {
3043+ return getTargetofModuleDefault(moduleSymbol, node, dontResolveAlias);
3044+ }
3045+ }
30133046 const root = isBindingElement(node) ? getRootDeclaration(node) as VariableDeclaration : node.parent.parent.parent;
30143047 const commonJSPropertyAccess = getCommonJSPropertyAccess(root);
30153048 const resolved = getExternalModuleMember(root, commonJSPropertyAccess || node, dontResolveAlias);
@@ -3034,6 +3067,13 @@ namespace ts {
30343067 }
30353068
30363069 function getTargetOfExportSpecifier(node: ExportSpecifier, meaning: SymbolFlags, dontResolveAlias?: boolean) {
3070+ if (idText(node.propertyName || node.name) === InternalSymbolName.Default) {
3071+ const specifier = getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node);
3072+ const moduleSymbol = specifier && resolveExternalModuleName(node, specifier);
3073+ if (moduleSymbol) {
3074+ return getTargetofModuleDefault(moduleSymbol, node, !!dontResolveAlias);
3075+ }
3076+ }
30373077 const resolved = node.parent.parent.moduleSpecifier ?
30383078 getExternalModuleMember(node.parent.parent, node, dontResolveAlias) :
30393079 resolveEntityName(node.propertyName || node.name, meaning, /*ignoreErrors*/ false, dontResolveAlias);
0 commit comments