Skip to content

Commit e2d22fa

Browse files
committed
Unify default import resolution across specifier target codepaths
1 parent 63d0574 commit e2d22fa

15 files changed

+1348
-62
lines changed

src/compiler/checker.ts

+102-62
Original file line numberDiff line numberDiff line change
@@ -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);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
tests/cases/compiler/b.ts(15,1): error TS2349: This expression is not callable.
2+
Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
3+
tests/cases/compiler/b.ts(16,1): error TS2349: This expression is not callable.
4+
Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
5+
tests/cases/compiler/b.ts(17,1): error TS2349: This expression is not callable.
6+
Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
7+
tests/cases/compiler/b.ts(18,1): error TS2349: This expression is not callable.
8+
Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
9+
tests/cases/compiler/b.ts(19,6): error TS2349: This expression is not callable.
10+
Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
11+
tests/cases/compiler/b.ts(20,6): error TS2349: This expression is not callable.
12+
Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
13+
14+
15+
==== tests/cases/compiler/mod.ts (0 errors) ====
16+
declare function fun(): void;
17+
export default fun;
18+
==== tests/cases/compiler/a.ts (0 errors) ====
19+
import mod = require("./mod");
20+
export = mod;
21+
==== tests/cases/compiler/b.ts (6 errors) ====
22+
import a from "./a";
23+
import { default as b } from "./a";
24+
import c, { default as d } from "./a";
25+
import * as self from "./b";
26+
export { default } from "./a";
27+
export { default as def } from "./a";
28+
29+
a === b;
30+
b === c;
31+
c === d;
32+
d === self.default;
33+
self.default === self.def;
34+
35+
// should all fail
36+
a();
37+
~
38+
!!! error TS2349: This expression is not callable.
39+
!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
40+
b();
41+
~
42+
!!! error TS2349: This expression is not callable.
43+
!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
44+
c();
45+
~
46+
!!! error TS2349: This expression is not callable.
47+
!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
48+
d();
49+
~
50+
!!! error TS2349: This expression is not callable.
51+
!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
52+
self.default();
53+
~~~~~~~
54+
!!! error TS2349: This expression is not callable.
55+
!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
56+
self.def();
57+
~~~
58+
!!! error TS2349: This expression is not callable.
59+
!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures.
60+
61+
// should all work
62+
a.default();
63+
b.default();
64+
c.default();
65+
d.default();
66+
self.default.default();
67+
self.def.default();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//// [tests/cases/compiler/esModuleInteropDefaultImports.ts] ////
2+
3+
//// [mod.ts]
4+
declare function fun(): void;
5+
export default fun;
6+
//// [a.ts]
7+
import mod = require("./mod");
8+
export = mod;
9+
//// [b.ts]
10+
import a from "./a";
11+
import { default as b } from "./a";
12+
import c, { default as d } from "./a";
13+
import * as self from "./b";
14+
export { default } from "./a";
15+
export { default as def } from "./a";
16+
17+
a === b;
18+
b === c;
19+
c === d;
20+
d === self.default;
21+
self.default === self.def;
22+
23+
// should all fail
24+
a();
25+
b();
26+
c();
27+
d();
28+
self.default();
29+
self.def();
30+
31+
// should all work
32+
a.default();
33+
b.default();
34+
c.default();
35+
d.default();
36+
self.default.default();
37+
self.def.default();
38+
39+
//// [mod.js]
40+
"use strict";
41+
exports.__esModule = true;
42+
exports["default"] = fun;
43+
//// [a.js]
44+
"use strict";
45+
var mod = require("./mod");
46+
module.exports = mod;
47+
//// [b.js]
48+
"use strict";
49+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
50+
if (k2 === undefined) k2 = k;
51+
var desc = Object.getOwnPropertyDescriptor(m, k);
52+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
53+
desc = { enumerable: true, get: function() { return m[k]; } };
54+
}
55+
Object.defineProperty(o, k2, desc);
56+
}) : (function(o, m, k, k2) {
57+
if (k2 === undefined) k2 = k;
58+
o[k2] = m[k];
59+
}));
60+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
61+
Object.defineProperty(o, "default", { enumerable: true, value: v });
62+
}) : function(o, v) {
63+
o["default"] = v;
64+
});
65+
var __importStar = (this && this.__importStar) || function (mod) {
66+
if (mod && mod.__esModule) return mod;
67+
var result = {};
68+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
69+
__setModuleDefault(result, mod);
70+
return result;
71+
};
72+
var __importDefault = (this && this.__importDefault) || function (mod) {
73+
return (mod && mod.__esModule) ? mod : { "default": mod };
74+
};
75+
exports.__esModule = true;
76+
exports.def = exports["default"] = void 0;
77+
var a_1 = __importDefault(require("./a"));
78+
var a_2 = __importDefault(require("./a"));
79+
var a_3 = __importDefault(require("./a"));
80+
var self = __importStar(require("./b"));
81+
var a_4 = require("./a");
82+
__createBinding(exports, a_4, "default");
83+
var a_5 = require("./a");
84+
__createBinding(exports, a_5, "default", "def");
85+
a_1["default"] === a_2["default"];
86+
a_2["default"] === a_3["default"];
87+
a_3["default"] === a_3["default"];
88+
a_3["default"] === self["default"];
89+
self["default"] === self.def;
90+
// should all fail
91+
(0, a_1["default"])();
92+
(0, a_2["default"])();
93+
(0, a_3["default"])();
94+
(0, a_3["default"])();
95+
self["default"]();
96+
self.def();
97+
// should all work
98+
a_1["default"]["default"]();
99+
a_2["default"]["default"]();
100+
a_3["default"]["default"]();
101+
a_3["default"]["default"]();
102+
self["default"]["default"]();
103+
self.def["default"]();

0 commit comments

Comments
 (0)