From e2d22fa5bc651b5e2ffaf032ad9f6fb1c91b83bd Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 6 Jul 2022 11:36:08 -0700 Subject: [PATCH 1/2] Unify default import resolution across specifier target codepaths --- src/compiler/checker.ts | 164 +++++++++++------- .../esModuleInteropDefaultImports.errors.txt | 67 +++++++ .../esModuleInteropDefaultImports.js | 103 +++++++++++ .../esModuleInteropDefaultImports.symbols | 121 +++++++++++++ .../esModuleInteropDefaultImports.types | 138 +++++++++++++++ ...deDefaultImports(module=node16).errors.txt | 64 +++++++ ...moduleNodeDefaultImports(module=node16).js | 65 +++++++ ...eNodeDefaultImports(module=node16).symbols | 114 ++++++++++++ ...uleNodeDefaultImports(module=node16).types | 131 ++++++++++++++ ...DefaultImports(module=nodenext).errors.txt | 64 +++++++ ...duleNodeDefaultImports(module=nodenext).js | 65 +++++++ ...odeDefaultImports(module=nodenext).symbols | 114 ++++++++++++ ...eNodeDefaultImports(module=nodenext).types | 131 ++++++++++++++ .../compiler/esModuleInteropDefaultImports.ts | 36 ++++ .../compiler/moduleNodeDefaultImports.ts | 33 ++++ 15 files changed, 1348 insertions(+), 62 deletions(-) create mode 100644 tests/baselines/reference/esModuleInteropDefaultImports.errors.txt create mode 100644 tests/baselines/reference/esModuleInteropDefaultImports.js create mode 100644 tests/baselines/reference/esModuleInteropDefaultImports.symbols create mode 100644 tests/baselines/reference/esModuleInteropDefaultImports.types create mode 100644 tests/baselines/reference/moduleNodeDefaultImports(module=node16).errors.txt create mode 100644 tests/baselines/reference/moduleNodeDefaultImports(module=node16).js create mode 100644 tests/baselines/reference/moduleNodeDefaultImports(module=node16).symbols create mode 100644 tests/baselines/reference/moduleNodeDefaultImports(module=node16).types create mode 100644 tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).errors.txt create mode 100644 tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).js create mode 100644 tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).symbols create mode 100644 tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).types create mode 100644 tests/cases/compiler/esModuleInteropDefaultImports.ts create mode 100644 tests/cases/compiler/moduleNodeDefaultImports.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8e24fee20606d..4e1e4766da4cb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2762,44 +2762,66 @@ namespace ts { function getTargetOfImportClause(node: ImportClause, dontResolveAlias: boolean): Symbol | undefined { const moduleSymbol = resolveExternalModuleName(node, node.parent.moduleSpecifier); if (moduleSymbol) { - let exportDefaultSymbol: Symbol | undefined; - if (isShorthandAmbientModuleSymbol(moduleSymbol)) { - exportDefaultSymbol = moduleSymbol; - } - else { - exportDefaultSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias); - } - - const file = moduleSymbol.declarations?.find(isSourceFile); - const hasDefaultOnly = isOnlyImportedAsDefault(node.parent.moduleSpecifier); - const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, node.parent.moduleSpecifier); - if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) { - if (hasExportAssignmentSymbol(moduleSymbol)) { - const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop"; - const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals); - const exportAssignment = exportEqualsSymbol!.valueDeclaration; - const err = error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), compilerOptionName); - - if (exportAssignment) { - addRelatedInfo(err, createDiagnosticForNode( - exportAssignment, - Diagnostics.This_module_is_declared_with_using_export_and_can_only_be_used_with_a_default_import_when_using_the_0_flag, - compilerOptionName - )); - } - } - else { - reportNonDefaultExport(moduleSymbol, node); + return getTargetofModuleDefault(moduleSymbol, node, dontResolveAlias); + } + } + + function getTargetofModuleDefault(moduleSymbol: Symbol, node: TypeOnlyCompatibleAliasDeclaration, dontResolveAlias: boolean) { + let exportDefaultSymbol: Symbol | undefined; + if (isShorthandAmbientModuleSymbol(moduleSymbol)) { + exportDefaultSymbol = moduleSymbol; + } + else { + exportDefaultSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias); + } + + const file = moduleSymbol.declarations?.find(isSourceFile); + const specifier = getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node); + if (!specifier) { + return exportDefaultSymbol; + } + const hasDefaultOnly = isOnlyImportedAsDefault(specifier); + const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, specifier); + if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) { + if (hasExportAssignmentSymbol(moduleSymbol)) { + const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop"; + const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals); + const exportAssignment = exportEqualsSymbol!.valueDeclaration; + const err = error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), compilerOptionName); + + if (exportAssignment) { + addRelatedInfo(err, createDiagnosticForNode( + exportAssignment, + Diagnostics.This_module_is_declared_with_using_export_and_can_only_be_used_with_a_default_import_when_using_the_0_flag, + compilerOptionName + )); } } - else if (hasSyntheticDefault || hasDefaultOnly) { - // per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present - const resolved = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias); - markSymbolOfAliasDeclarationIfTypeOnly(node, moduleSymbol, resolved, /*overwriteTypeOnly*/ false); - return resolved; + else if (isImportClause(node)) { + reportNonDefaultExport(moduleSymbol, node); } - markSymbolOfAliasDeclarationIfTypeOnly(node, exportDefaultSymbol, /*finalTarget*/ undefined, /*overwriteTypeOnly*/ false); - return exportDefaultSymbol; + else { + errorNoModuleMemberSymbol(moduleSymbol, moduleSymbol, node, isImportOrExportSpecifier(node) && node.propertyName || node.name); + } + } + else if (hasSyntheticDefault || hasDefaultOnly) { + // per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present + const resolved = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias); + markSymbolOfAliasDeclarationIfTypeOnly(node, moduleSymbol, resolved, /*overwriteTypeOnly*/ false); + return resolved; + } + markSymbolOfAliasDeclarationIfTypeOnly(node, exportDefaultSymbol, /*finalTarget*/ undefined, /*overwriteTypeOnly*/ false); + return exportDefaultSymbol; + } + + function getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node: TypeOnlyCompatibleAliasDeclaration): Expression | undefined { + switch (node.kind) { + case SyntaxKind.ImportClause: return node.parent.moduleSpecifier; + case SyntaxKind.ImportEqualsDeclaration: return isExternalModuleReference(node.moduleReference) ? node.moduleReference.expression : undefined; + case SyntaxKind.NamespaceImport: return node.parent.parent.moduleSpecifier; + case SyntaxKind.ImportSpecifier: return node.parent.parent.parent.moduleSpecifier; + case SyntaxKind.ExportSpecifier: return node.parent.parent.moduleSpecifier; + default: return Debug.assertNever(node); } } @@ -2933,38 +2955,42 @@ namespace ts { combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : symbolFromModule || symbolFromVariable; if (!symbol) { - const moduleName = getFullyQualifiedName(moduleSymbol, node); - const declarationName = declarationNameToString(name); - const suggestion = getSuggestedSymbolForNonexistentModule(name, targetSymbol); - if (suggestion !== undefined) { - const suggestionName = symbolToString(suggestion); - const diagnostic = error(name, Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2, moduleName, declarationName, suggestionName); - if (suggestion.valueDeclaration) { - addRelatedInfo(diagnostic, - createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestionName) - ); - } - } - else { - if (moduleSymbol.exports?.has(InternalSymbolName.Default)) { - error( - name, - Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead, - moduleName, - declarationName - ); - } - else { - reportNonExportedMember(node, name, declarationName, moduleSymbol, moduleName); - } - } + errorNoModuleMemberSymbol(moduleSymbol, targetSymbol, node, name); } return symbol; } } } - function reportNonExportedMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration, name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void { + function errorNoModuleMemberSymbol(moduleSymbol: Symbol, targetSymbol: Symbol, node: Node, name: Identifier) { + const moduleName = getFullyQualifiedName(moduleSymbol, node); + const declarationName = declarationNameToString(name); + const suggestion = getSuggestedSymbolForNonexistentModule(name, targetSymbol); + if (suggestion !== undefined) { + const suggestionName = symbolToString(suggestion); + const diagnostic = error(name, Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2, moduleName, declarationName, suggestionName); + if (suggestion.valueDeclaration) { + addRelatedInfo(diagnostic, + createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestionName) + ); + } + } + else { + if (moduleSymbol.exports?.has(InternalSymbolName.Default)) { + error( + name, + Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead, + moduleName, + declarationName + ); + } + else { + reportNonExportedMember(node, name, declarationName, moduleSymbol, moduleName); + } + } + } + + function reportNonExportedMember(node: Node, name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void { const localSymbol = moduleSymbol.valueDeclaration?.locals?.get(name.escapedText); const exports = moduleSymbol.exports; if (localSymbol) { @@ -2989,7 +3015,7 @@ namespace ts { } } - function reportInvalidImportEqualsExportMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration, name: Identifier, declarationName: string, moduleName: string) { + function reportInvalidImportEqualsExportMember(node: Node, name: Identifier, declarationName: string, moduleName: string) { if (moduleKind >= ModuleKind.ES2015) { const message = getESModuleInterop(compilerOptions) ? Diagnostics._0_can_only_be_imported_by_using_a_default_import : Diagnostics._0_can_only_be_imported_by_turning_on_the_esModuleInterop_flag_and_using_a_default_import; @@ -3010,6 +3036,13 @@ namespace ts { } function getTargetOfImportSpecifier(node: ImportSpecifier | BindingElement, dontResolveAlias: boolean): Symbol | undefined { + if (isImportSpecifier(node) && idText(node.propertyName || node.name) === InternalSymbolName.Default) { + const specifier = getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node); + const moduleSymbol = specifier && resolveExternalModuleName(node, specifier); + if (moduleSymbol) { + return getTargetofModuleDefault(moduleSymbol, node, dontResolveAlias); + } + } const root = isBindingElement(node) ? getRootDeclaration(node) as VariableDeclaration : node.parent.parent.parent; const commonJSPropertyAccess = getCommonJSPropertyAccess(root); const resolved = getExternalModuleMember(root, commonJSPropertyAccess || node, dontResolveAlias); @@ -3034,6 +3067,13 @@ namespace ts { } function getTargetOfExportSpecifier(node: ExportSpecifier, meaning: SymbolFlags, dontResolveAlias?: boolean) { + if (idText(node.propertyName || node.name) === InternalSymbolName.Default) { + const specifier = getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node); + const moduleSymbol = specifier && resolveExternalModuleName(node, specifier); + if (moduleSymbol) { + return getTargetofModuleDefault(moduleSymbol, node, !!dontResolveAlias); + } + } const resolved = node.parent.parent.moduleSpecifier ? getExternalModuleMember(node.parent.parent, node, dontResolveAlias) : resolveEntityName(node.propertyName || node.name, meaning, /*ignoreErrors*/ false, dontResolveAlias); diff --git a/tests/baselines/reference/esModuleInteropDefaultImports.errors.txt b/tests/baselines/reference/esModuleInteropDefaultImports.errors.txt new file mode 100644 index 0000000000000..f91da0382ed37 --- /dev/null +++ b/tests/baselines/reference/esModuleInteropDefaultImports.errors.txt @@ -0,0 +1,67 @@ +tests/cases/compiler/b.ts(15,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.ts(16,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.ts(17,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.ts(18,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.ts(19,6): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.ts(20,6): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + + +==== tests/cases/compiler/mod.ts (0 errors) ==== + declare function fun(): void; + export default fun; +==== tests/cases/compiler/a.ts (0 errors) ==== + import mod = require("./mod"); + export = mod; +==== tests/cases/compiler/b.ts (6 errors) ==== + import a from "./a"; + import { default as b } from "./a"; + import c, { default as d } from "./a"; + import * as self from "./b"; + export { default } from "./a"; + export { default as def } from "./a"; + + a === b; + b === c; + c === d; + d === self.default; + self.default === self.def; + + // should all fail + a(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + b(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + c(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + d(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + self.default(); + ~~~~~~~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + self.def(); + ~~~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + + // should all work + a.default(); + b.default(); + c.default(); + d.default(); + self.default.default(); + self.def.default(); \ No newline at end of file diff --git a/tests/baselines/reference/esModuleInteropDefaultImports.js b/tests/baselines/reference/esModuleInteropDefaultImports.js new file mode 100644 index 0000000000000..f893e0e968b99 --- /dev/null +++ b/tests/baselines/reference/esModuleInteropDefaultImports.js @@ -0,0 +1,103 @@ +//// [tests/cases/compiler/esModuleInteropDefaultImports.ts] //// + +//// [mod.ts] +declare function fun(): void; +export default fun; +//// [a.ts] +import mod = require("./mod"); +export = mod; +//// [b.ts] +import a from "./a"; +import { default as b } from "./a"; +import c, { default as d } from "./a"; +import * as self from "./b"; +export { default } from "./a"; +export { default as def } from "./a"; + +a === b; +b === c; +c === d; +d === self.default; +self.default === self.def; + +// should all fail +a(); +b(); +c(); +d(); +self.default(); +self.def(); + +// should all work +a.default(); +b.default(); +c.default(); +d.default(); +self.default.default(); +self.def.default(); + +//// [mod.js] +"use strict"; +exports.__esModule = true; +exports["default"] = fun; +//// [a.js] +"use strict"; +var mod = require("./mod"); +module.exports = mod; +//// [b.js] +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +exports.__esModule = true; +exports.def = exports["default"] = void 0; +var a_1 = __importDefault(require("./a")); +var a_2 = __importDefault(require("./a")); +var a_3 = __importDefault(require("./a")); +var self = __importStar(require("./b")); +var a_4 = require("./a"); +__createBinding(exports, a_4, "default"); +var a_5 = require("./a"); +__createBinding(exports, a_5, "default", "def"); +a_1["default"] === a_2["default"]; +a_2["default"] === a_3["default"]; +a_3["default"] === a_3["default"]; +a_3["default"] === self["default"]; +self["default"] === self.def; +// should all fail +(0, a_1["default"])(); +(0, a_2["default"])(); +(0, a_3["default"])(); +(0, a_3["default"])(); +self["default"](); +self.def(); +// should all work +a_1["default"]["default"](); +a_2["default"]["default"](); +a_3["default"]["default"](); +a_3["default"]["default"](); +self["default"]["default"](); +self.def["default"](); diff --git a/tests/baselines/reference/esModuleInteropDefaultImports.symbols b/tests/baselines/reference/esModuleInteropDefaultImports.symbols new file mode 100644 index 0000000000000..05c18c13e4cfb --- /dev/null +++ b/tests/baselines/reference/esModuleInteropDefaultImports.symbols @@ -0,0 +1,121 @@ +=== tests/cases/compiler/mod.ts === +declare function fun(): void; +>fun : Symbol(fun, Decl(mod.ts, 0, 0)) + +export default fun; +>fun : Symbol(fun, Decl(mod.ts, 0, 0)) + +=== tests/cases/compiler/a.ts === +import mod = require("./mod"); +>mod : Symbol(mod, Decl(a.ts, 0, 0)) + +export = mod; +>mod : Symbol(mod, Decl(a.ts, 0, 0)) + +=== tests/cases/compiler/b.ts === +import a from "./a"; +>a : Symbol(a, Decl(b.ts, 0, 6)) + +import { default as b } from "./a"; +>default : Symbol(mod, Decl(a.ts, 0, 30)) +>b : Symbol(b, Decl(b.ts, 1, 8)) + +import c, { default as d } from "./a"; +>c : Symbol(c, Decl(b.ts, 2, 6)) +>default : Symbol(mod, Decl(a.ts, 0, 30)) +>d : Symbol(d, Decl(b.ts, 2, 11)) + +import * as self from "./b"; +>self : Symbol(self, Decl(b.ts, 3, 6)) + +export { default } from "./a"; +>default : Symbol(self.default, Decl(b.ts, 4, 8)) + +export { default as def } from "./a"; +>default : Symbol(mod, Decl(a.ts, 0, 30)) +>def : Symbol(self.def, Decl(b.ts, 5, 8)) + +a === b; +>a : Symbol(a, Decl(b.ts, 0, 6)) +>b : Symbol(b, Decl(b.ts, 1, 8)) + +b === c; +>b : Symbol(b, Decl(b.ts, 1, 8)) +>c : Symbol(c, Decl(b.ts, 2, 6)) + +c === d; +>c : Symbol(c, Decl(b.ts, 2, 6)) +>d : Symbol(d, Decl(b.ts, 2, 11)) + +d === self.default; +>d : Symbol(d, Decl(b.ts, 2, 11)) +>self.default : Symbol(self.default, Decl(b.ts, 4, 8)) +>self : Symbol(self, Decl(b.ts, 3, 6)) +>default : Symbol(self.default, Decl(b.ts, 4, 8)) + +self.default === self.def; +>self.default : Symbol(self.default, Decl(b.ts, 4, 8)) +>self : Symbol(self, Decl(b.ts, 3, 6)) +>default : Symbol(self.default, Decl(b.ts, 4, 8)) +>self.def : Symbol(self.def, Decl(b.ts, 5, 8)) +>self : Symbol(self, Decl(b.ts, 3, 6)) +>def : Symbol(self.def, Decl(b.ts, 5, 8)) + +// should all fail +a(); +>a : Symbol(a, Decl(b.ts, 0, 6)) + +b(); +>b : Symbol(b, Decl(b.ts, 1, 8)) + +c(); +>c : Symbol(c, Decl(b.ts, 2, 6)) + +d(); +>d : Symbol(d, Decl(b.ts, 2, 11)) + +self.default(); +>self.default : Symbol(self.default, Decl(b.ts, 4, 8)) +>self : Symbol(self, Decl(b.ts, 3, 6)) +>default : Symbol(self.default, Decl(b.ts, 4, 8)) + +self.def(); +>self.def : Symbol(self.def, Decl(b.ts, 5, 8)) +>self : Symbol(self, Decl(b.ts, 3, 6)) +>def : Symbol(self.def, Decl(b.ts, 5, 8)) + +// should all work +a.default(); +>a.default : Symbol(a.default, Decl(mod.ts, 0, 29)) +>a : Symbol(a, Decl(b.ts, 0, 6)) +>default : Symbol(a.default, Decl(mod.ts, 0, 29)) + +b.default(); +>b.default : Symbol(a.default, Decl(mod.ts, 0, 29)) +>b : Symbol(b, Decl(b.ts, 1, 8)) +>default : Symbol(a.default, Decl(mod.ts, 0, 29)) + +c.default(); +>c.default : Symbol(a.default, Decl(mod.ts, 0, 29)) +>c : Symbol(c, Decl(b.ts, 2, 6)) +>default : Symbol(a.default, Decl(mod.ts, 0, 29)) + +d.default(); +>d.default : Symbol(a.default, Decl(mod.ts, 0, 29)) +>d : Symbol(d, Decl(b.ts, 2, 11)) +>default : Symbol(a.default, Decl(mod.ts, 0, 29)) + +self.default.default(); +>self.default.default : Symbol(a.default, Decl(mod.ts, 0, 29)) +>self.default : Symbol(self.default, Decl(b.ts, 4, 8)) +>self : Symbol(self, Decl(b.ts, 3, 6)) +>default : Symbol(self.default, Decl(b.ts, 4, 8)) +>default : Symbol(a.default, Decl(mod.ts, 0, 29)) + +self.def.default(); +>self.def.default : Symbol(a.default, Decl(mod.ts, 0, 29)) +>self.def : Symbol(self.def, Decl(b.ts, 5, 8)) +>self : Symbol(self, Decl(b.ts, 3, 6)) +>def : Symbol(self.def, Decl(b.ts, 5, 8)) +>default : Symbol(a.default, Decl(mod.ts, 0, 29)) + diff --git a/tests/baselines/reference/esModuleInteropDefaultImports.types b/tests/baselines/reference/esModuleInteropDefaultImports.types new file mode 100644 index 0000000000000..961e7063b2f9e --- /dev/null +++ b/tests/baselines/reference/esModuleInteropDefaultImports.types @@ -0,0 +1,138 @@ +=== tests/cases/compiler/mod.ts === +declare function fun(): void; +>fun : () => void + +export default fun; +>fun : () => void + +=== tests/cases/compiler/a.ts === +import mod = require("./mod"); +>mod : typeof mod + +export = mod; +>mod : typeof mod + +=== tests/cases/compiler/b.ts === +import a from "./a"; +>a : typeof a + +import { default as b } from "./a"; +>default : typeof a +>b : typeof a + +import c, { default as d } from "./a"; +>c : typeof a +>default : typeof a +>d : typeof a + +import * as self from "./b"; +>self : typeof self + +export { default } from "./a"; +>default : typeof a + +export { default as def } from "./a"; +>default : typeof a +>def : typeof a + +a === b; +>a === b : boolean +>a : typeof a +>b : typeof a + +b === c; +>b === c : boolean +>b : typeof a +>c : typeof a + +c === d; +>c === d : boolean +>c : typeof a +>d : typeof a + +d === self.default; +>d === self.default : boolean +>d : typeof a +>self.default : typeof a +>self : typeof self +>default : typeof a + +self.default === self.def; +>self.default === self.def : boolean +>self.default : typeof a +>self : typeof self +>default : typeof a +>self.def : typeof a +>self : typeof self +>def : typeof a + +// should all fail +a(); +>a() : any +>a : typeof a + +b(); +>b() : any +>b : typeof a + +c(); +>c() : any +>c : typeof a + +d(); +>d() : any +>d : typeof a + +self.default(); +>self.default() : any +>self.default : typeof a +>self : typeof self +>default : typeof a + +self.def(); +>self.def() : any +>self.def : typeof a +>self : typeof self +>def : typeof a + +// should all work +a.default(); +>a.default() : void +>a.default : () => void +>a : typeof a +>default : () => void + +b.default(); +>b.default() : void +>b.default : () => void +>b : typeof a +>default : () => void + +c.default(); +>c.default() : void +>c.default : () => void +>c : typeof a +>default : () => void + +d.default(); +>d.default() : void +>d.default : () => void +>d : typeof a +>default : () => void + +self.default.default(); +>self.default.default() : void +>self.default.default : () => void +>self.default : typeof a +>self : typeof self +>default : typeof a +>default : () => void + +self.def.default(); +>self.def.default() : void +>self.def.default : () => void +>self.def : typeof a +>self : typeof self +>def : typeof a +>default : () => void + diff --git a/tests/baselines/reference/moduleNodeDefaultImports(module=node16).errors.txt b/tests/baselines/reference/moduleNodeDefaultImports(module=node16).errors.txt new file mode 100644 index 0000000000000..f90724d92338b --- /dev/null +++ b/tests/baselines/reference/moduleNodeDefaultImports(module=node16).errors.txt @@ -0,0 +1,64 @@ +tests/cases/compiler/b.mts(15,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(16,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(17,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(18,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(19,6): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(20,6): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + + +==== tests/cases/compiler/mod.cts (0 errors) ==== + declare function fun(): void; + export default fun; +==== tests/cases/compiler/b.mts (6 errors) ==== + import a from "./mod.cjs"; + import { default as b } from "./mod.cjs"; + import c, { default as d } from "./mod.cjs"; + import * as self from "./b.mjs"; + export { default } from "./mod.cjs"; + export { default as def } from "./mod.cjs"; + + a === b; + b === c; + c === d; + d === self.default; + self.default === self.def; + + // should all fail + a(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + b(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + c(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + d(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + self.default(); + ~~~~~~~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + self.def(); + ~~~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + + // should all work + a.default(); + b.default(); + c.default(); + d.default(); + self.default.default(); + self.def.default(); \ No newline at end of file diff --git a/tests/baselines/reference/moduleNodeDefaultImports(module=node16).js b/tests/baselines/reference/moduleNodeDefaultImports(module=node16).js new file mode 100644 index 0000000000000..1d6e31ec0afb2 --- /dev/null +++ b/tests/baselines/reference/moduleNodeDefaultImports(module=node16).js @@ -0,0 +1,65 @@ +//// [tests/cases/compiler/moduleNodeDefaultImports.ts] //// + +//// [mod.cts] +declare function fun(): void; +export default fun; +//// [b.mts] +import a from "./mod.cjs"; +import { default as b } from "./mod.cjs"; +import c, { default as d } from "./mod.cjs"; +import * as self from "./b.mjs"; +export { default } from "./mod.cjs"; +export { default as def } from "./mod.cjs"; + +a === b; +b === c; +c === d; +d === self.default; +self.default === self.def; + +// should all fail +a(); +b(); +c(); +d(); +self.default(); +self.def(); + +// should all work +a.default(); +b.default(); +c.default(); +d.default(); +self.default.default(); +self.def.default(); + +//// [mod.cjs] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = fun; +//// [b.mjs] +import a from "./mod.cjs"; +import { default as b } from "./mod.cjs"; +import c, { default as d } from "./mod.cjs"; +import * as self from "./b.mjs"; +export { default } from "./mod.cjs"; +export { default as def } from "./mod.cjs"; +a === b; +b === c; +c === d; +d === self.default; +self.default === self.def; +// should all fail +a(); +b(); +c(); +d(); +self.default(); +self.def(); +// should all work +a.default(); +b.default(); +c.default(); +d.default(); +self.default.default(); +self.def.default(); diff --git a/tests/baselines/reference/moduleNodeDefaultImports(module=node16).symbols b/tests/baselines/reference/moduleNodeDefaultImports(module=node16).symbols new file mode 100644 index 0000000000000..7d475bb0e7e6a --- /dev/null +++ b/tests/baselines/reference/moduleNodeDefaultImports(module=node16).symbols @@ -0,0 +1,114 @@ +=== tests/cases/compiler/mod.cts === +declare function fun(): void; +>fun : Symbol(fun, Decl(mod.cts, 0, 0)) + +export default fun; +>fun : Symbol(fun, Decl(mod.cts, 0, 0)) + +=== tests/cases/compiler/b.mts === +import a from "./mod.cjs"; +>a : Symbol(a, Decl(b.mts, 0, 6)) + +import { default as b } from "./mod.cjs"; +>default : Symbol(a, Decl(mod.cts, 0, 0)) +>b : Symbol(b, Decl(b.mts, 1, 8)) + +import c, { default as d } from "./mod.cjs"; +>c : Symbol(c, Decl(b.mts, 2, 6)) +>default : Symbol(a, Decl(mod.cts, 0, 0)) +>d : Symbol(d, Decl(b.mts, 2, 11)) + +import * as self from "./b.mjs"; +>self : Symbol(self, Decl(b.mts, 3, 6)) + +export { default } from "./mod.cjs"; +>default : Symbol(self.default, Decl(b.mts, 4, 8)) + +export { default as def } from "./mod.cjs"; +>default : Symbol(a, Decl(mod.cts, 0, 0)) +>def : Symbol(self.def, Decl(b.mts, 5, 8)) + +a === b; +>a : Symbol(a, Decl(b.mts, 0, 6)) +>b : Symbol(b, Decl(b.mts, 1, 8)) + +b === c; +>b : Symbol(b, Decl(b.mts, 1, 8)) +>c : Symbol(c, Decl(b.mts, 2, 6)) + +c === d; +>c : Symbol(c, Decl(b.mts, 2, 6)) +>d : Symbol(d, Decl(b.mts, 2, 11)) + +d === self.default; +>d : Symbol(d, Decl(b.mts, 2, 11)) +>self.default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>default : Symbol(self.default, Decl(b.mts, 4, 8)) + +self.default === self.def; +>self.default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self.def : Symbol(self.def, Decl(b.mts, 5, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>def : Symbol(self.def, Decl(b.mts, 5, 8)) + +// should all fail +a(); +>a : Symbol(a, Decl(b.mts, 0, 6)) + +b(); +>b : Symbol(b, Decl(b.mts, 1, 8)) + +c(); +>c : Symbol(c, Decl(b.mts, 2, 6)) + +d(); +>d : Symbol(d, Decl(b.mts, 2, 11)) + +self.default(); +>self.default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>default : Symbol(self.default, Decl(b.mts, 4, 8)) + +self.def(); +>self.def : Symbol(self.def, Decl(b.mts, 5, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>def : Symbol(self.def, Decl(b.mts, 5, 8)) + +// should all work +a.default(); +>a.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>a : Symbol(a, Decl(b.mts, 0, 6)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +b.default(); +>b.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>b : Symbol(b, Decl(b.mts, 1, 8)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +c.default(); +>c.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>c : Symbol(c, Decl(b.mts, 2, 6)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +d.default(); +>d.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>d : Symbol(d, Decl(b.mts, 2, 11)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +self.default.default(); +>self.default.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>self.default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>default : Symbol(self.default, Decl(b.mts, 4, 8)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +self.def.default(); +>self.def.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>self.def : Symbol(self.def, Decl(b.mts, 5, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>def : Symbol(self.def, Decl(b.mts, 5, 8)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + diff --git a/tests/baselines/reference/moduleNodeDefaultImports(module=node16).types b/tests/baselines/reference/moduleNodeDefaultImports(module=node16).types new file mode 100644 index 0000000000000..653dac121b8b0 --- /dev/null +++ b/tests/baselines/reference/moduleNodeDefaultImports(module=node16).types @@ -0,0 +1,131 @@ +=== tests/cases/compiler/mod.cts === +declare function fun(): void; +>fun : () => void + +export default fun; +>fun : () => void + +=== tests/cases/compiler/b.mts === +import a from "./mod.cjs"; +>a : typeof a + +import { default as b } from "./mod.cjs"; +>default : typeof a +>b : typeof a + +import c, { default as d } from "./mod.cjs"; +>c : typeof a +>default : typeof a +>d : typeof a + +import * as self from "./b.mjs"; +>self : typeof self + +export { default } from "./mod.cjs"; +>default : typeof a + +export { default as def } from "./mod.cjs"; +>default : typeof a +>def : typeof a + +a === b; +>a === b : boolean +>a : typeof a +>b : typeof a + +b === c; +>b === c : boolean +>b : typeof a +>c : typeof a + +c === d; +>c === d : boolean +>c : typeof a +>d : typeof a + +d === self.default; +>d === self.default : boolean +>d : typeof a +>self.default : typeof a +>self : typeof self +>default : typeof a + +self.default === self.def; +>self.default === self.def : boolean +>self.default : typeof a +>self : typeof self +>default : typeof a +>self.def : typeof a +>self : typeof self +>def : typeof a + +// should all fail +a(); +>a() : any +>a : typeof a + +b(); +>b() : any +>b : typeof a + +c(); +>c() : any +>c : typeof a + +d(); +>d() : any +>d : typeof a + +self.default(); +>self.default() : any +>self.default : typeof a +>self : typeof self +>default : typeof a + +self.def(); +>self.def() : any +>self.def : typeof a +>self : typeof self +>def : typeof a + +// should all work +a.default(); +>a.default() : void +>a.default : () => void +>a : typeof a +>default : () => void + +b.default(); +>b.default() : void +>b.default : () => void +>b : typeof a +>default : () => void + +c.default(); +>c.default() : void +>c.default : () => void +>c : typeof a +>default : () => void + +d.default(); +>d.default() : void +>d.default : () => void +>d : typeof a +>default : () => void + +self.default.default(); +>self.default.default() : void +>self.default.default : () => void +>self.default : typeof a +>self : typeof self +>default : typeof a +>default : () => void + +self.def.default(); +>self.def.default() : void +>self.def.default : () => void +>self.def : typeof a +>self : typeof self +>def : typeof a +>default : () => void + diff --git a/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).errors.txt b/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).errors.txt new file mode 100644 index 0000000000000..f90724d92338b --- /dev/null +++ b/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).errors.txt @@ -0,0 +1,64 @@ +tests/cases/compiler/b.mts(15,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(16,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(17,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(18,1): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(19,6): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. +tests/cases/compiler/b.mts(20,6): error TS2349: This expression is not callable. + Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + + +==== tests/cases/compiler/mod.cts (0 errors) ==== + declare function fun(): void; + export default fun; +==== tests/cases/compiler/b.mts (6 errors) ==== + import a from "./mod.cjs"; + import { default as b } from "./mod.cjs"; + import c, { default as d } from "./mod.cjs"; + import * as self from "./b.mjs"; + export { default } from "./mod.cjs"; + export { default as def } from "./mod.cjs"; + + a === b; + b === c; + c === d; + d === self.default; + self.default === self.def; + + // should all fail + a(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + b(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + c(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + d(); + ~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + self.default(); + ~~~~~~~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + self.def(); + ~~~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'typeof import("tests/cases/compiler/mod")' has no call signatures. + + // should all work + a.default(); + b.default(); + c.default(); + d.default(); + self.default.default(); + self.def.default(); \ No newline at end of file diff --git a/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).js b/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).js new file mode 100644 index 0000000000000..1d6e31ec0afb2 --- /dev/null +++ b/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).js @@ -0,0 +1,65 @@ +//// [tests/cases/compiler/moduleNodeDefaultImports.ts] //// + +//// [mod.cts] +declare function fun(): void; +export default fun; +//// [b.mts] +import a from "./mod.cjs"; +import { default as b } from "./mod.cjs"; +import c, { default as d } from "./mod.cjs"; +import * as self from "./b.mjs"; +export { default } from "./mod.cjs"; +export { default as def } from "./mod.cjs"; + +a === b; +b === c; +c === d; +d === self.default; +self.default === self.def; + +// should all fail +a(); +b(); +c(); +d(); +self.default(); +self.def(); + +// should all work +a.default(); +b.default(); +c.default(); +d.default(); +self.default.default(); +self.def.default(); + +//// [mod.cjs] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = fun; +//// [b.mjs] +import a from "./mod.cjs"; +import { default as b } from "./mod.cjs"; +import c, { default as d } from "./mod.cjs"; +import * as self from "./b.mjs"; +export { default } from "./mod.cjs"; +export { default as def } from "./mod.cjs"; +a === b; +b === c; +c === d; +d === self.default; +self.default === self.def; +// should all fail +a(); +b(); +c(); +d(); +self.default(); +self.def(); +// should all work +a.default(); +b.default(); +c.default(); +d.default(); +self.default.default(); +self.def.default(); diff --git a/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).symbols b/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).symbols new file mode 100644 index 0000000000000..7d475bb0e7e6a --- /dev/null +++ b/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).symbols @@ -0,0 +1,114 @@ +=== tests/cases/compiler/mod.cts === +declare function fun(): void; +>fun : Symbol(fun, Decl(mod.cts, 0, 0)) + +export default fun; +>fun : Symbol(fun, Decl(mod.cts, 0, 0)) + +=== tests/cases/compiler/b.mts === +import a from "./mod.cjs"; +>a : Symbol(a, Decl(b.mts, 0, 6)) + +import { default as b } from "./mod.cjs"; +>default : Symbol(a, Decl(mod.cts, 0, 0)) +>b : Symbol(b, Decl(b.mts, 1, 8)) + +import c, { default as d } from "./mod.cjs"; +>c : Symbol(c, Decl(b.mts, 2, 6)) +>default : Symbol(a, Decl(mod.cts, 0, 0)) +>d : Symbol(d, Decl(b.mts, 2, 11)) + +import * as self from "./b.mjs"; +>self : Symbol(self, Decl(b.mts, 3, 6)) + +export { default } from "./mod.cjs"; +>default : Symbol(self.default, Decl(b.mts, 4, 8)) + +export { default as def } from "./mod.cjs"; +>default : Symbol(a, Decl(mod.cts, 0, 0)) +>def : Symbol(self.def, Decl(b.mts, 5, 8)) + +a === b; +>a : Symbol(a, Decl(b.mts, 0, 6)) +>b : Symbol(b, Decl(b.mts, 1, 8)) + +b === c; +>b : Symbol(b, Decl(b.mts, 1, 8)) +>c : Symbol(c, Decl(b.mts, 2, 6)) + +c === d; +>c : Symbol(c, Decl(b.mts, 2, 6)) +>d : Symbol(d, Decl(b.mts, 2, 11)) + +d === self.default; +>d : Symbol(d, Decl(b.mts, 2, 11)) +>self.default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>default : Symbol(self.default, Decl(b.mts, 4, 8)) + +self.default === self.def; +>self.default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self.def : Symbol(self.def, Decl(b.mts, 5, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>def : Symbol(self.def, Decl(b.mts, 5, 8)) + +// should all fail +a(); +>a : Symbol(a, Decl(b.mts, 0, 6)) + +b(); +>b : Symbol(b, Decl(b.mts, 1, 8)) + +c(); +>c : Symbol(c, Decl(b.mts, 2, 6)) + +d(); +>d : Symbol(d, Decl(b.mts, 2, 11)) + +self.default(); +>self.default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>default : Symbol(self.default, Decl(b.mts, 4, 8)) + +self.def(); +>self.def : Symbol(self.def, Decl(b.mts, 5, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>def : Symbol(self.def, Decl(b.mts, 5, 8)) + +// should all work +a.default(); +>a.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>a : Symbol(a, Decl(b.mts, 0, 6)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +b.default(); +>b.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>b : Symbol(b, Decl(b.mts, 1, 8)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +c.default(); +>c.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>c : Symbol(c, Decl(b.mts, 2, 6)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +d.default(); +>d.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>d : Symbol(d, Decl(b.mts, 2, 11)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +self.default.default(); +>self.default.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>self.default : Symbol(self.default, Decl(b.mts, 4, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>default : Symbol(self.default, Decl(b.mts, 4, 8)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + +self.def.default(); +>self.def.default : Symbol(a.default, Decl(mod.cts, 0, 29)) +>self.def : Symbol(self.def, Decl(b.mts, 5, 8)) +>self : Symbol(self, Decl(b.mts, 3, 6)) +>def : Symbol(self.def, Decl(b.mts, 5, 8)) +>default : Symbol(a.default, Decl(mod.cts, 0, 29)) + diff --git a/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).types b/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).types new file mode 100644 index 0000000000000..653dac121b8b0 --- /dev/null +++ b/tests/baselines/reference/moduleNodeDefaultImports(module=nodenext).types @@ -0,0 +1,131 @@ +=== tests/cases/compiler/mod.cts === +declare function fun(): void; +>fun : () => void + +export default fun; +>fun : () => void + +=== tests/cases/compiler/b.mts === +import a from "./mod.cjs"; +>a : typeof a + +import { default as b } from "./mod.cjs"; +>default : typeof a +>b : typeof a + +import c, { default as d } from "./mod.cjs"; +>c : typeof a +>default : typeof a +>d : typeof a + +import * as self from "./b.mjs"; +>self : typeof self + +export { default } from "./mod.cjs"; +>default : typeof a + +export { default as def } from "./mod.cjs"; +>default : typeof a +>def : typeof a + +a === b; +>a === b : boolean +>a : typeof a +>b : typeof a + +b === c; +>b === c : boolean +>b : typeof a +>c : typeof a + +c === d; +>c === d : boolean +>c : typeof a +>d : typeof a + +d === self.default; +>d === self.default : boolean +>d : typeof a +>self.default : typeof a +>self : typeof self +>default : typeof a + +self.default === self.def; +>self.default === self.def : boolean +>self.default : typeof a +>self : typeof self +>default : typeof a +>self.def : typeof a +>self : typeof self +>def : typeof a + +// should all fail +a(); +>a() : any +>a : typeof a + +b(); +>b() : any +>b : typeof a + +c(); +>c() : any +>c : typeof a + +d(); +>d() : any +>d : typeof a + +self.default(); +>self.default() : any +>self.default : typeof a +>self : typeof self +>default : typeof a + +self.def(); +>self.def() : any +>self.def : typeof a +>self : typeof self +>def : typeof a + +// should all work +a.default(); +>a.default() : void +>a.default : () => void +>a : typeof a +>default : () => void + +b.default(); +>b.default() : void +>b.default : () => void +>b : typeof a +>default : () => void + +c.default(); +>c.default() : void +>c.default : () => void +>c : typeof a +>default : () => void + +d.default(); +>d.default() : void +>d.default : () => void +>d : typeof a +>default : () => void + +self.default.default(); +>self.default.default() : void +>self.default.default : () => void +>self.default : typeof a +>self : typeof self +>default : typeof a +>default : () => void + +self.def.default(); +>self.def.default() : void +>self.def.default : () => void +>self.def : typeof a +>self : typeof self +>def : typeof a +>default : () => void + diff --git a/tests/cases/compiler/esModuleInteropDefaultImports.ts b/tests/cases/compiler/esModuleInteropDefaultImports.ts new file mode 100644 index 0000000000000..0ddd2df567756 --- /dev/null +++ b/tests/cases/compiler/esModuleInteropDefaultImports.ts @@ -0,0 +1,36 @@ +// @esModuleInterop: true +// @filename: mod.ts +declare function fun(): void; +export default fun; +// @filename: a.ts +import mod = require("./mod"); +export = mod; +// @filename: b.ts +import a from "./a"; +import { default as b } from "./a"; +import c, { default as d } from "./a"; +import * as self from "./b"; +export { default } from "./a"; +export { default as def } from "./a"; + +a === b; +b === c; +c === d; +d === self.default; +self.default === self.def; + +// should all fail +a(); +b(); +c(); +d(); +self.default(); +self.def(); + +// should all work +a.default(); +b.default(); +c.default(); +d.default(); +self.default.default(); +self.def.default(); \ No newline at end of file diff --git a/tests/cases/compiler/moduleNodeDefaultImports.ts b/tests/cases/compiler/moduleNodeDefaultImports.ts new file mode 100644 index 0000000000000..906de682bd610 --- /dev/null +++ b/tests/cases/compiler/moduleNodeDefaultImports.ts @@ -0,0 +1,33 @@ +// @module: node16,nodenext +// @filename: mod.cts +declare function fun(): void; +export default fun; +// @filename: b.mts +import a from "./mod.cjs"; +import { default as b } from "./mod.cjs"; +import c, { default as d } from "./mod.cjs"; +import * as self from "./b.mjs"; +export { default } from "./mod.cjs"; +export { default as def } from "./mod.cjs"; + +a === b; +b === c; +c === d; +d === self.default; +self.default === self.def; + +// should all fail +a(); +b(); +c(); +d(); +self.default(); +self.def(); + +// should all work +a.default(); +b.default(); +c.default(); +d.default(); +self.default.default(); +self.def.default(); \ No newline at end of file From cf010b517d22ad3745d7f0556743bb504a502910 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 18 Aug 2022 15:13:49 -0700 Subject: [PATCH 2/2] Use differing type aliases, per request --- src/compiler/checker.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5a325cfc0599b..8c8101e87374c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2802,7 +2802,7 @@ namespace ts { } } - function getTargetofModuleDefault(moduleSymbol: Symbol, node: TypeOnlyCompatibleAliasDeclaration, dontResolveAlias: boolean) { + function getTargetofModuleDefault(moduleSymbol: Symbol, node: ImportClause | ImportOrExportSpecifier, dontResolveAlias: boolean) { let exportDefaultSymbol: Symbol | undefined; if (isShorthandAmbientModuleSymbol(moduleSymbol)) { exportDefaultSymbol = moduleSymbol; @@ -2812,7 +2812,7 @@ namespace ts { } const file = moduleSymbol.declarations?.find(isSourceFile); - const specifier = getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node); + const specifier = getModuleSpecifierForImportOrExport(node); if (!specifier) { return exportDefaultSymbol; } @@ -2850,7 +2850,7 @@ namespace ts { return exportDefaultSymbol; } - function getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node: TypeOnlyCompatibleAliasDeclaration): Expression | undefined { + function getModuleSpecifierForImportOrExport(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportOrExportSpecifier): Expression | undefined { switch (node.kind) { case SyntaxKind.ImportClause: return node.parent.moduleSpecifier; case SyntaxKind.ImportEqualsDeclaration: return isExternalModuleReference(node.moduleReference) ? node.moduleReference.expression : undefined; @@ -3073,7 +3073,7 @@ namespace ts { function getTargetOfImportSpecifier(node: ImportSpecifier | BindingElement, dontResolveAlias: boolean): Symbol | undefined { if (isImportSpecifier(node) && idText(node.propertyName || node.name) === InternalSymbolName.Default) { - const specifier = getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node); + const specifier = getModuleSpecifierForImportOrExport(node); const moduleSymbol = specifier && resolveExternalModuleName(node, specifier); if (moduleSymbol) { return getTargetofModuleDefault(moduleSymbol, node, dontResolveAlias); @@ -3104,7 +3104,7 @@ namespace ts { function getTargetOfExportSpecifier(node: ExportSpecifier, meaning: SymbolFlags, dontResolveAlias?: boolean) { if (idText(node.propertyName || node.name) === InternalSymbolName.Default) { - const specifier = getModuleSpecifierForTypeOnlyCompatibleAliasDeclaration(node); + const specifier = getModuleSpecifierForImportOrExport(node); const moduleSymbol = specifier && resolveExternalModuleName(node, specifier); if (moduleSymbol) { return getTargetofModuleDefault(moduleSymbol, node, !!dontResolveAlias);