From 144468816243e987537b74416c65b38f44b26d86 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 1 Oct 2021 17:45:45 +0000 Subject: [PATCH] Cherry-pick PR #46156 into release-4.5 Component commits: 7e3a80af3b Modify synthetic default generation code for dual-mode module resolution --- src/compiler/checker.ts | 30 ++++++--- ...SynchronousCallErrors(module=node12).types | 12 ++-- ...nchronousCallErrors(module=nodenext).types | 12 ++-- ...rmatFileAlwaysHasDefault(module=node12).js | 36 +++++++++++ ...ileAlwaysHasDefault(module=node12).symbols | 13 ++++ ...tFileAlwaysHasDefault(module=node12).types | 14 +++++ ...atFileAlwaysHasDefault(module=nodenext).js | 36 +++++++++++ ...eAlwaysHasDefault(module=nodenext).symbols | 13 ++++ ...ileAlwaysHasDefault(module=nodenext).types | 14 +++++ ...tionEmitDynamicImportWithPackageExports.js | 62 +++++++++---------- ...nEmitDynamicImportWithPackageExports.types | 32 +++++----- ...SynchronousCallErrors(module=node12).types | 12 ++-- ...nchronousCallErrors(module=nodenext).types | 12 ++-- ...odeModulesCjsFormatFileAlwaysHasDefault.ts | 19 ++++++ 14 files changed, 236 insertions(+), 81 deletions(-) create mode 100644 tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).js create mode 100644 tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).symbols create mode 100644 tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).types create mode 100644 tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).js create mode 100644 tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).symbols create mode 100644 tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).types create mode 100644 tests/cases/conformance/node/nodeModulesCjsFormatFileAlwaysHasDefault.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f07c6f0f36727..1af122e4e3b4e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2629,7 +2629,23 @@ namespace ts { return ((isExportAssignment(node) && !node.isExportEquals) || hasSyntacticModifier(node, ModifierFlags.Default) || isExportSpecifier(node)); } - function canHaveSyntheticDefault(file: SourceFile | undefined, moduleSymbol: Symbol, dontResolveAlias: boolean) { + function getUsageModeForExpression(usage: Expression) { + return isStringLiteralLike(usage) ? getModeForUsageLocation(getSourceFileOfNode(usage), usage) : undefined; + } + + function isESMFormatImportImportingCommonjsFormatFile(usageMode: SourceFile["impliedNodeFormat"], targetMode: SourceFile["impliedNodeFormat"]) { + return usageMode === ModuleKind.ESNext && targetMode === ModuleKind.CommonJS; + } + + function canHaveSyntheticDefault(file: SourceFile | undefined, moduleSymbol: Symbol, dontResolveAlias: boolean, usage: Expression) { + const usageMode = file && getUsageModeForExpression(usage); + if (file && usageMode !== undefined) { + const result = isESMFormatImportImportingCommonjsFormatFile(usageMode, file.impliedNodeFormat); + if (usageMode === ModuleKind.ESNext || result) { + return result; + } + // fallthrough on cjs usages so we imply defaults for interop'd imports, too + } if (!allowSyntheticDefaultImports) { return false; } @@ -2672,7 +2688,7 @@ namespace ts { } const file = moduleSymbol.declarations?.find(isSourceFile); - const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias); + const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, node.parent.moduleSpecifier); if (!exportDefaultSymbol && !hasSyntheticDefault) { if (hasExportAssignmentSymbol(moduleSymbol)) { const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop"; @@ -2824,7 +2840,7 @@ namespace ts { let symbolFromModule = getExportOfModule(targetSymbol, name, specifier, dontResolveAlias); if (symbolFromModule === undefined && name.escapedText === InternalSymbolName.Default) { const file = moduleSymbol.declarations?.find(isSourceFile); - if (canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias)) { + if (canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, moduleSpecifier)) { symbolFromModule = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias); } } @@ -3587,7 +3603,7 @@ namespace ts { sigs = getSignaturesOfStructuredType(type, SignatureKind.Construct); } if (sigs && sigs.length) { - const moduleType = getTypeWithSyntheticDefaultImportType(type, symbol, moduleSymbol!); + const moduleType = getTypeWithSyntheticDefaultImportType(type, symbol, moduleSymbol!, isImportCall(referenceParent) ? referenceParent.arguments[0] : referenceParent.moduleSpecifier); // Create a new symbol which has the module's type less the call and construct signatures const result = createSymbol(symbol.flags, symbol.escapedName); result.declarations = symbol.declarations ? symbol.declarations.slice() : []; @@ -30945,18 +30961,18 @@ namespace ts { if (moduleSymbol) { const esModuleSymbol = resolveESModuleSymbol(moduleSymbol, specifier, /*dontRecursivelyResolve*/ true, /*suppressUsageError*/ false); if (esModuleSymbol) { - return createPromiseReturnType(node, getTypeWithSyntheticDefaultImportType(getTypeOfSymbol(esModuleSymbol), esModuleSymbol, moduleSymbol)); + return createPromiseReturnType(node, getTypeWithSyntheticDefaultImportType(getTypeOfSymbol(esModuleSymbol), esModuleSymbol, moduleSymbol, specifier)); } } return createPromiseReturnType(node, anyType); } - function getTypeWithSyntheticDefaultImportType(type: Type, symbol: Symbol, originalSymbol: Symbol): Type { + function getTypeWithSyntheticDefaultImportType(type: Type, symbol: Symbol, originalSymbol: Symbol, moduleSpecifier: Expression): Type { if (allowSyntheticDefaultImports && type && !isErrorType(type)) { const synthType = type as SyntheticDefaultModuleType; if (!synthType.syntheticType) { const file = originalSymbol.declarations?.find(isSourceFile); - const hasSyntheticDefault = canHaveSyntheticDefault(file, originalSymbol, /*dontResolveAlias*/ false); + const hasSyntheticDefault = canHaveSyntheticDefault(file, originalSymbol, /*dontResolveAlias*/ false, moduleSpecifier); if (hasSyntheticDefault) { const memberTable = createSymbolTable(); const newSymbol = createSymbol(SymbolFlags.Alias, InternalSymbolName.Default); diff --git a/tests/baselines/reference/nodeModulesAllowJsSynchronousCallErrors(module=node12).types b/tests/baselines/reference/nodeModulesAllowJsSynchronousCallErrors(module=node12).types index 77061e385af3a..dde6770780839 100644 --- a/tests/baselines/reference/nodeModulesAllowJsSynchronousCallErrors(module=node12).types +++ b/tests/baselines/reference/nodeModulesAllowJsSynchronousCallErrors(module=node12).types @@ -23,9 +23,9 @@ export async function f() { >"../index.js" : "../index.js" const mod4 = await import ("./index.js"); ->mod4 : typeof mod2 ->await import ("./index.js") : typeof mod2 ->import ("./index.js") : Promise +>mod4 : { default: typeof mod2; f(): Promise; } +>await import ("./index.js") : { default: typeof mod2; f(): Promise; } +>import ("./index.js") : Promise<{ default: typeof mod2; f(): Promise; }> >"./index.js" : "./index.js" h(); @@ -57,9 +57,9 @@ export async function h() { >"./index.js" : "./index.js" const mod4 = await import ("./subfolder/index.js"); ->mod4 : typeof mod2 ->await import ("./subfolder/index.js") : typeof mod2 ->import ("./subfolder/index.js") : Promise +>mod4 : { default: typeof mod2; f(): Promise; } +>await import ("./subfolder/index.js") : { default: typeof mod2; f(): Promise; } +>import ("./subfolder/index.js") : Promise<{ default: typeof mod2; f(): Promise; }> >"./subfolder/index.js" : "./subfolder/index.js" f(); diff --git a/tests/baselines/reference/nodeModulesAllowJsSynchronousCallErrors(module=nodenext).types b/tests/baselines/reference/nodeModulesAllowJsSynchronousCallErrors(module=nodenext).types index 77061e385af3a..dde6770780839 100644 --- a/tests/baselines/reference/nodeModulesAllowJsSynchronousCallErrors(module=nodenext).types +++ b/tests/baselines/reference/nodeModulesAllowJsSynchronousCallErrors(module=nodenext).types @@ -23,9 +23,9 @@ export async function f() { >"../index.js" : "../index.js" const mod4 = await import ("./index.js"); ->mod4 : typeof mod2 ->await import ("./index.js") : typeof mod2 ->import ("./index.js") : Promise +>mod4 : { default: typeof mod2; f(): Promise; } +>await import ("./index.js") : { default: typeof mod2; f(): Promise; } +>import ("./index.js") : Promise<{ default: typeof mod2; f(): Promise; }> >"./index.js" : "./index.js" h(); @@ -57,9 +57,9 @@ export async function h() { >"./index.js" : "./index.js" const mod4 = await import ("./subfolder/index.js"); ->mod4 : typeof mod2 ->await import ("./subfolder/index.js") : typeof mod2 ->import ("./subfolder/index.js") : Promise +>mod4 : { default: typeof mod2; f(): Promise; } +>await import ("./subfolder/index.js") : { default: typeof mod2; f(): Promise; } +>import ("./subfolder/index.js") : Promise<{ default: typeof mod2; f(): Promise; }> >"./subfolder/index.js" : "./subfolder/index.js" f(); diff --git a/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).js b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).js new file mode 100644 index 0000000000000..f0c9198a51305 --- /dev/null +++ b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).js @@ -0,0 +1,36 @@ +//// [tests/cases/conformance/node/nodeModulesCjsFormatFileAlwaysHasDefault.ts] //// + +//// [index.ts] +// cjs format file +export const a = 1; +//// [index.ts] +// esm format file +import mod from "./subfolder/index.js"; +mod; +//// [package.json] +{ + "name": "package", + "private": true, + "type": "module" +} +//// [package.json] +{ + "type": "commonjs" +} + +//// [index.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.a = void 0; +// cjs format file +exports.a = 1; +//// [index.js] +// esm format file +import mod from "./subfolder/index.js"; +mod; + + +//// [index.d.ts] +export declare const a = 1; +//// [index.d.ts] +export {}; diff --git a/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).symbols b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).symbols new file mode 100644 index 0000000000000..0d676181e2735 --- /dev/null +++ b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).symbols @@ -0,0 +1,13 @@ +=== tests/cases/conformance/node/subfolder/index.ts === +// cjs format file +export const a = 1; +>a : Symbol(a, Decl(index.ts, 1, 12)) + +=== tests/cases/conformance/node/index.ts === +// esm format file +import mod from "./subfolder/index.js"; +>mod : Symbol(mod, Decl(index.ts, 1, 6)) + +mod; +>mod : Symbol(mod, Decl(index.ts, 1, 6)) + diff --git a/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).types b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).types new file mode 100644 index 0000000000000..abbc4f1b40ee5 --- /dev/null +++ b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=node12).types @@ -0,0 +1,14 @@ +=== tests/cases/conformance/node/subfolder/index.ts === +// cjs format file +export const a = 1; +>a : 1 +>1 : 1 + +=== tests/cases/conformance/node/index.ts === +// esm format file +import mod from "./subfolder/index.js"; +>mod : typeof mod + +mod; +>mod : typeof mod + diff --git a/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).js b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).js new file mode 100644 index 0000000000000..f0c9198a51305 --- /dev/null +++ b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).js @@ -0,0 +1,36 @@ +//// [tests/cases/conformance/node/nodeModulesCjsFormatFileAlwaysHasDefault.ts] //// + +//// [index.ts] +// cjs format file +export const a = 1; +//// [index.ts] +// esm format file +import mod from "./subfolder/index.js"; +mod; +//// [package.json] +{ + "name": "package", + "private": true, + "type": "module" +} +//// [package.json] +{ + "type": "commonjs" +} + +//// [index.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.a = void 0; +// cjs format file +exports.a = 1; +//// [index.js] +// esm format file +import mod from "./subfolder/index.js"; +mod; + + +//// [index.d.ts] +export declare const a = 1; +//// [index.d.ts] +export {}; diff --git a/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).symbols b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).symbols new file mode 100644 index 0000000000000..0d676181e2735 --- /dev/null +++ b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).symbols @@ -0,0 +1,13 @@ +=== tests/cases/conformance/node/subfolder/index.ts === +// cjs format file +export const a = 1; +>a : Symbol(a, Decl(index.ts, 1, 12)) + +=== tests/cases/conformance/node/index.ts === +// esm format file +import mod from "./subfolder/index.js"; +>mod : Symbol(mod, Decl(index.ts, 1, 6)) + +mod; +>mod : Symbol(mod, Decl(index.ts, 1, 6)) + diff --git a/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).types b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).types new file mode 100644 index 0000000000000..abbc4f1b40ee5 --- /dev/null +++ b/tests/baselines/reference/nodeModulesCjsFormatFileAlwaysHasDefault(module=nodenext).types @@ -0,0 +1,14 @@ +=== tests/cases/conformance/node/subfolder/index.ts === +// cjs format file +export const a = 1; +>a : 1 +>1 : 1 + +=== tests/cases/conformance/node/index.ts === +// esm format file +import mod from "./subfolder/index.js"; +>mod : typeof mod + +mod; +>mod : typeof mod + diff --git a/tests/baselines/reference/nodeModulesDeclarationEmitDynamicImportWithPackageExports.js b/tests/baselines/reference/nodeModulesDeclarationEmitDynamicImportWithPackageExports.js index a872cc70f5625..f8d6161af5772 100644 --- a/tests/baselines/reference/nodeModulesDeclarationEmitDynamicImportWithPackageExports.js +++ b/tests/baselines/reference/nodeModulesDeclarationEmitDynamicImportWithPackageExports.js @@ -122,7 +122,9 @@ export {}; //// [index.d.cts] export {}; //// [other.d.ts] -export declare const a: typeof import("package/cjs"); +export declare const a: { + default: typeof import("package/cjs"); +}; export declare const b: typeof import("package/mjs"); export declare const c: typeof import("package"); export declare const f: { @@ -134,12 +136,11 @@ export declare const d: { default: typeof import("inner/cjs"); cjsNonmain: true; }; -export declare const e: { - default: typeof import("inner/mjs"); - esm: true; -}; +export declare const e: typeof import("inner/mjs"); //// [other.d.mts] -export declare const a: typeof import("package/cjs"); +export declare const a: { + default: typeof import("package/cjs"); +}; export declare const b: typeof import("package/mjs"); export declare const c: typeof import("package"); export declare const f: { @@ -151,12 +152,11 @@ export declare const d: { default: typeof import("inner/cjs"); cjsNonmain: true; }; -export declare const e: { - default: typeof import("inner/mjs"); - esm: true; -}; +export declare const e: typeof import("inner/mjs"); //// [other.d.cts] -export declare const a: Promise; +export declare const a: Promise<{ + default: typeof import("package/cjs"); +}>; export declare const b: Promise; export declare const c: Promise; export declare const f: Promise<{ @@ -168,18 +168,15 @@ export declare const d: Promise<{ default: typeof import("inner/cjs"); cjsNonmain: true; }>; -export declare const e: Promise<{ - default: typeof import("inner/mjs"); - esm: true; -}>; +export declare const e: Promise; //// [DtsFileErrors] -tests/cases/conformance/node/other.d.cts(2,47): error TS1471: Module 'package/mjs' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead. -tests/cases/conformance/node/other.d.cts(3,47): error TS1471: Module 'package' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead. -tests/cases/conformance/node/other2.d.cts(6,28): error TS1471: Module 'inner/mjs' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead. +tests/cases/conformance/node/other.d.cts(4,47): error TS1471: Module 'package/mjs' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead. +tests/cases/conformance/node/other.d.cts(5,47): error TS1471: Module 'package' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead. +tests/cases/conformance/node/other2.d.cts(5,47): error TS1471: Module 'inner/mjs' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead. ==== tests/cases/conformance/node/index.d.ts (0 errors) ==== @@ -192,7 +189,9 @@ tests/cases/conformance/node/other2.d.cts(6,28): error TS1471: Module 'inner/mjs export {}; ==== tests/cases/conformance/node/other.d.ts (0 errors) ==== - export declare const a: typeof import("package/cjs"); + export declare const a: { + default: typeof import("package/cjs"); + }; export declare const b: typeof import("package/mjs"); export declare const c: typeof import("package"); export declare const f: { @@ -205,13 +204,12 @@ tests/cases/conformance/node/other2.d.cts(6,28): error TS1471: Module 'inner/mjs default: typeof import("inner/cjs"); cjsNonmain: true; }; - export declare const e: { - default: typeof import("inner/mjs"); - esm: true; - }; + export declare const e: typeof import("inner/mjs"); ==== tests/cases/conformance/node/other.d.mts (0 errors) ==== - export declare const a: typeof import("package/cjs"); + export declare const a: { + default: typeof import("package/cjs"); + }; export declare const b: typeof import("package/mjs"); export declare const c: typeof import("package"); export declare const f: { @@ -224,13 +222,12 @@ tests/cases/conformance/node/other2.d.cts(6,28): error TS1471: Module 'inner/mjs default: typeof import("inner/cjs"); cjsNonmain: true; }; - export declare const e: { - default: typeof import("inner/mjs"); - esm: true; - }; + export declare const e: typeof import("inner/mjs"); ==== tests/cases/conformance/node/other.d.cts (2 errors) ==== - export declare const a: Promise; + export declare const a: Promise<{ + default: typeof import("package/cjs"); + }>; export declare const b: Promise; ~~~~~~~~~~~~~ !!! error TS1471: Module 'package/mjs' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead. @@ -247,12 +244,9 @@ tests/cases/conformance/node/other2.d.cts(6,28): error TS1471: Module 'inner/mjs default: typeof import("inner/cjs"); cjsNonmain: true; }>; - export declare const e: Promise<{ - default: typeof import("inner/mjs"); - ~~~~~~~~~~~ + export declare const e: Promise; + ~~~~~~~~~~~ !!! error TS1471: Module 'inner/mjs' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead. - esm: true; - }>; ==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ==== // cjs format file diff --git a/tests/baselines/reference/nodeModulesDeclarationEmitDynamicImportWithPackageExports.types b/tests/baselines/reference/nodeModulesDeclarationEmitDynamicImportWithPackageExports.types index 5a8030b43bb26..54a78d6a9fc2e 100644 --- a/tests/baselines/reference/nodeModulesDeclarationEmitDynamicImportWithPackageExports.types +++ b/tests/baselines/reference/nodeModulesDeclarationEmitDynamicImportWithPackageExports.types @@ -10,9 +10,9 @@ No type information for this code.export {}; No type information for this code.=== tests/cases/conformance/node/other.ts === // esm format file export const a = await import("package/cjs"); ->a : typeof import("tests/cases/conformance/node/index") ->await import("package/cjs") : typeof import("tests/cases/conformance/node/index") ->import("package/cjs") : Promise +>a : { default: typeof import("tests/cases/conformance/node/index"); } +>await import("package/cjs") : { default: typeof import("tests/cases/conformance/node/index"); } +>import("package/cjs") : Promise<{ default: typeof import("tests/cases/conformance/node/index"); }> >"package/cjs" : "package/cjs" export const b = await import("package/mjs"); @@ -42,17 +42,17 @@ export const d = await import("inner/cjs"); >"inner/cjs" : "inner/cjs" export const e = await import("inner/mjs"); ->e : { default: typeof import("tests/cases/conformance/node/node_modules/inner/index"); esm: true; } ->await import("inner/mjs") : { default: typeof import("tests/cases/conformance/node/node_modules/inner/index"); esm: true; } ->import("inner/mjs") : Promise<{ default: typeof import("tests/cases/conformance/node/node_modules/inner/index"); esm: true; }> +>e : typeof import("tests/cases/conformance/node/node_modules/inner/index") +>await import("inner/mjs") : typeof import("tests/cases/conformance/node/node_modules/inner/index") +>import("inner/mjs") : Promise >"inner/mjs" : "inner/mjs" === tests/cases/conformance/node/other.mts === // esm format file export const a = await import("package/cjs"); ->a : typeof import("tests/cases/conformance/node/index") ->await import("package/cjs") : typeof import("tests/cases/conformance/node/index") ->import("package/cjs") : Promise +>a : { default: typeof import("tests/cases/conformance/node/index"); } +>await import("package/cjs") : { default: typeof import("tests/cases/conformance/node/index"); } +>import("package/cjs") : Promise<{ default: typeof import("tests/cases/conformance/node/index"); }> >"package/cjs" : "package/cjs" export const b = await import("package/mjs"); @@ -82,16 +82,16 @@ export const d = await import("inner/cjs"); >"inner/cjs" : "inner/cjs" export const e = await import("inner/mjs"); ->e : { default: typeof import("tests/cases/conformance/node/node_modules/inner/index"); esm: true; } ->await import("inner/mjs") : { default: typeof import("tests/cases/conformance/node/node_modules/inner/index"); esm: true; } ->import("inner/mjs") : Promise<{ default: typeof import("tests/cases/conformance/node/node_modules/inner/index"); esm: true; }> +>e : typeof import("tests/cases/conformance/node/node_modules/inner/index") +>await import("inner/mjs") : typeof import("tests/cases/conformance/node/node_modules/inner/index") +>import("inner/mjs") : Promise >"inner/mjs" : "inner/mjs" === tests/cases/conformance/node/other.cts === // cjs format file, no TLA export const a = import("package/cjs"); ->a : Promise ->import("package/cjs") : Promise +>a : Promise<{ default: typeof import("tests/cases/conformance/node/index"); }> +>import("package/cjs") : Promise<{ default: typeof import("tests/cases/conformance/node/index"); }> >"package/cjs" : "package/cjs" export const b = import("package/mjs"); @@ -117,8 +117,8 @@ export const d = import("inner/cjs"); >"inner/cjs" : "inner/cjs" export const e = import("inner/mjs"); ->e : Promise<{ default: typeof import("tests/cases/conformance/node/node_modules/inner/index"); esm: true; }> ->import("inner/mjs") : Promise<{ default: typeof import("tests/cases/conformance/node/node_modules/inner/index"); esm: true; }> +>e : Promise +>import("inner/mjs") : Promise >"inner/mjs" : "inner/mjs" === tests/cases/conformance/node/node_modules/inner/index.d.ts === diff --git a/tests/baselines/reference/nodeModulesSynchronousCallErrors(module=node12).types b/tests/baselines/reference/nodeModulesSynchronousCallErrors(module=node12).types index 8ca6d52152161..0ded6fc1b888a 100644 --- a/tests/baselines/reference/nodeModulesSynchronousCallErrors(module=node12).types +++ b/tests/baselines/reference/nodeModulesSynchronousCallErrors(module=node12).types @@ -23,9 +23,9 @@ export async function f() { >"../index.js" : "../index.js" const mod4 = await import ("./index.js"); ->mod4 : typeof mod2 ->await import ("./index.js") : typeof mod2 ->import ("./index.js") : Promise +>mod4 : { default: typeof mod2; f(): Promise; } +>await import ("./index.js") : { default: typeof mod2; f(): Promise; } +>import ("./index.js") : Promise<{ default: typeof mod2; f(): Promise; }> >"./index.js" : "./index.js" h(); @@ -57,9 +57,9 @@ export async function h() { >"./index.js" : "./index.js" const mod4 = await import ("./subfolder/index.js"); ->mod4 : typeof mod2 ->await import ("./subfolder/index.js") : typeof mod2 ->import ("./subfolder/index.js") : Promise +>mod4 : { default: typeof mod2; f(): Promise; } +>await import ("./subfolder/index.js") : { default: typeof mod2; f(): Promise; } +>import ("./subfolder/index.js") : Promise<{ default: typeof mod2; f(): Promise; }> >"./subfolder/index.js" : "./subfolder/index.js" f(); diff --git a/tests/baselines/reference/nodeModulesSynchronousCallErrors(module=nodenext).types b/tests/baselines/reference/nodeModulesSynchronousCallErrors(module=nodenext).types index 8ca6d52152161..0ded6fc1b888a 100644 --- a/tests/baselines/reference/nodeModulesSynchronousCallErrors(module=nodenext).types +++ b/tests/baselines/reference/nodeModulesSynchronousCallErrors(module=nodenext).types @@ -23,9 +23,9 @@ export async function f() { >"../index.js" : "../index.js" const mod4 = await import ("./index.js"); ->mod4 : typeof mod2 ->await import ("./index.js") : typeof mod2 ->import ("./index.js") : Promise +>mod4 : { default: typeof mod2; f(): Promise; } +>await import ("./index.js") : { default: typeof mod2; f(): Promise; } +>import ("./index.js") : Promise<{ default: typeof mod2; f(): Promise; }> >"./index.js" : "./index.js" h(); @@ -57,9 +57,9 @@ export async function h() { >"./index.js" : "./index.js" const mod4 = await import ("./subfolder/index.js"); ->mod4 : typeof mod2 ->await import ("./subfolder/index.js") : typeof mod2 ->import ("./subfolder/index.js") : Promise +>mod4 : { default: typeof mod2; f(): Promise; } +>await import ("./subfolder/index.js") : { default: typeof mod2; f(): Promise; } +>import ("./subfolder/index.js") : Promise<{ default: typeof mod2; f(): Promise; }> >"./subfolder/index.js" : "./subfolder/index.js" f(); diff --git a/tests/cases/conformance/node/nodeModulesCjsFormatFileAlwaysHasDefault.ts b/tests/cases/conformance/node/nodeModulesCjsFormatFileAlwaysHasDefault.ts new file mode 100644 index 0000000000000..06c6320d77905 --- /dev/null +++ b/tests/cases/conformance/node/nodeModulesCjsFormatFileAlwaysHasDefault.ts @@ -0,0 +1,19 @@ +// @module: node12,nodenext +// @declaration: true +// @filename: subfolder/index.ts +// cjs format file +export const a = 1; +// @filename: index.ts +// esm format file +import mod from "./subfolder/index.js"; +mod; +// @filename: package.json +{ + "name": "package", + "private": true, + "type": "module" +} +// @filename: subfolder/package.json +{ + "type": "commonjs" +} \ No newline at end of file