@@ -2762,44 +2762,66 @@ namespace ts {
2762
2762
function getTargetOfImportClause(node: ImportClause, dontResolveAlias: boolean): Symbol | undefined {
2763
2763
const moduleSymbol = resolveExternalModuleName(node, node.parent.moduleSpecifier);
2764
2764
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
+ ));
2793
2798
}
2794
2799
}
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);
2800
2802
}
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);
2803
2825
}
2804
2826
}
2805
2827
@@ -2933,38 +2955,42 @@ namespace ts {
2933
2955
combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) :
2934
2956
symbolFromModule || symbolFromVariable;
2935
2957
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);
2961
2959
}
2962
2960
return symbol;
2963
2961
}
2964
2962
}
2965
2963
}
2966
2964
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 {
2968
2994
const localSymbol = moduleSymbol.valueDeclaration?.locals?.get(name.escapedText);
2969
2995
const exports = moduleSymbol.exports;
2970
2996
if (localSymbol) {
@@ -2989,7 +3015,7 @@ namespace ts {
2989
3015
}
2990
3016
}
2991
3017
2992
- function reportInvalidImportEqualsExportMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration , name: Identifier, declarationName: string, moduleName: string) {
3018
+ function reportInvalidImportEqualsExportMember(node: Node , name: Identifier, declarationName: string, moduleName: string) {
2993
3019
if (moduleKind >= ModuleKind.ES2015) {
2994
3020
const message = getESModuleInterop(compilerOptions) ? Diagnostics._0_can_only_be_imported_by_using_a_default_import :
2995
3021
Diagnostics._0_can_only_be_imported_by_turning_on_the_esModuleInterop_flag_and_using_a_default_import;
@@ -3010,6 +3036,13 @@ namespace ts {
3010
3036
}
3011
3037
3012
3038
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
+ }
3013
3046
const root = isBindingElement(node) ? getRootDeclaration(node) as VariableDeclaration : node.parent.parent.parent;
3014
3047
const commonJSPropertyAccess = getCommonJSPropertyAccess(root);
3015
3048
const resolved = getExternalModuleMember(root, commonJSPropertyAccess || node, dontResolveAlias);
@@ -3034,6 +3067,13 @@ namespace ts {
3034
3067
}
3035
3068
3036
3069
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
+ }
3037
3077
const resolved = node.parent.parent.moduleSpecifier ?
3038
3078
getExternalModuleMember(node.parent.parent, node, dontResolveAlias) :
3039
3079
resolveEntityName(node.propertyName || node.name, meaning, /*ignoreErrors*/ false, dontResolveAlias);
0 commit comments