Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify synthetic default generation code for dual-mode module resolution #46156

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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() : [];
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof mod2>
>mod4 : { default: typeof mod2; f(): Promise<void>; }
>await import ("./index.js") : { default: typeof mod2; f(): Promise<void>; }
>import ("./index.js") : Promise<{ default: typeof mod2; f(): Promise<void>; }>
>"./index.js" : "./index.js"

h();
Expand Down Expand Up @@ -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<typeof mod2>
>mod4 : { default: typeof mod2; f(): Promise<void>; }
>await import ("./subfolder/index.js") : { default: typeof mod2; f(): Promise<void>; }
>import ("./subfolder/index.js") : Promise<{ default: typeof mod2; f(): Promise<void>; }>
>"./subfolder/index.js" : "./subfolder/index.js"

f();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof mod2>
>mod4 : { default: typeof mod2; f(): Promise<void>; }
>await import ("./index.js") : { default: typeof mod2; f(): Promise<void>; }
>import ("./index.js") : Promise<{ default: typeof mod2; f(): Promise<void>; }>
>"./index.js" : "./index.js"

h();
Expand Down Expand Up @@ -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<typeof mod2>
>mod4 : { default: typeof mod2; f(): Promise<void>; }
>await import ("./subfolder/index.js") : { default: typeof mod2; f(): Promise<void>; }
>import ("./subfolder/index.js") : Promise<{ default: typeof mod2; f(): Promise<void>; }>
>"./subfolder/index.js" : "./subfolder/index.js"

f();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {};
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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 {};
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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<typeof import("package/cjs")>;
export declare const a: Promise<{
default: typeof import("package/cjs");
}>;
export declare const b: Promise<typeof import("package/mjs")>;
export declare const c: Promise<typeof import("package")>;
export declare const f: Promise<{
Expand All @@ -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<typeof import("inner/mjs")>;


//// [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) ====
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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<typeof import("package/cjs")>;
export declare const a: Promise<{
default: typeof import("package/cjs");
}>;
export declare const b: Promise<typeof import("package/mjs")>;
~~~~~~~~~~~~~
!!! 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.
Expand All @@ -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<typeof import("inner/mjs")>;
~~~~~~~~~~~
!!! 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
Expand Down
Loading