Skip to content

--moduleFormatInterop (all names bikesheddable) #58480

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

Closed
wants to merge 15 commits into from
Closed
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
14 changes: 12 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ import {
getLineAndCharacterOfPosition,
getMembersOfDeclaration,
getModifiers,
getModuleFormatInteropKind,
getModuleInstanceState,
getNameFromImportAttribute,
getNameFromIndexInfo,
Expand Down Expand Up @@ -859,6 +860,7 @@ import {
modifierToFlag,
ModuleBlock,
ModuleDeclaration,
ModuleFormatInteropKind,
ModuleInstanceState,
ModuleKind,
ModuleResolutionKind,
Expand Down Expand Up @@ -3634,7 +3636,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const usageMode = file && getEmitSyntaxForModuleSpecifierExpression(usage);
if (file && usageMode !== undefined) {
const targetMode = host.getImpliedNodeFormatForEmit(file);
if (usageMode === ModuleKind.ESNext && targetMode === ModuleKind.CommonJS && ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) {
const moduleFormatInterop = getModuleFormatInteropKind(compilerOptions);
const usesNodeLikeInteropRules = moduleFormatInterop === ModuleFormatInteropKind.BundlerNode
|| ModuleFormatInteropKind.Node16 <= moduleFormatInterop && moduleFormatInterop <= ModuleFormatInteropKind.NodeNext;
if (usageMode === ModuleKind.ESNext && targetMode !== ModuleKind.ESNext && usesNodeLikeInteropRules) {
// In Node.js, CommonJS modules always have a synthetic default when imported into ESM
return true;
}
Expand All @@ -3649,6 +3654,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
// Declaration files (and ambient modules)
if (!file || file.isDeclarationFile) {
// Unambiguously ESM files do not have a synthetic default
if (file && host.getImpliedNodeFormatForEmit(file) === ModuleKind.ESNext) {
return false;
}
// Definitely cannot have a synthetic default if they have a syntactic default member specified
const defaultExportSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, /*sourceNode*/ undefined, /*dontResolveAlias*/ true); // Dont resolve alias because we want the immediately exported symbol's declaration
if (defaultExportSymbol && some(defaultExportSymbol.declarations, isSyntacticDefault)) {
Expand Down Expand Up @@ -4572,6 +4581,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
? host.getModeForUsageLocation(currentSourceFile, contextSpecifier)
: host.getDefaultResolutionModeForFile(currentSourceFile);
const moduleResolutionKind = getEmitModuleResolutionKind(compilerOptions);
const moduleFormatInterop = getModuleFormatInteropKind(compilerOptions);
const resolvedModule = host.getResolvedModule(currentSourceFile, moduleReference, mode)?.resolvedModule;
const resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule, currentSourceFile);
const sourceFile = resolvedModule
Expand Down Expand Up @@ -4607,7 +4617,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (resolvedModule.isExternalLibraryImport && !resolutionExtensionIsTSOrJson(resolvedModule.extension)) {
errorOnImplicitAnyModule(/*isError*/ false, errorNode, currentSourceFile, mode, resolvedModule, moduleReference);
}
if (moduleResolutionKind === ModuleResolutionKind.Node16 || moduleResolutionKind === ModuleResolutionKind.NodeNext) {
if (ModuleFormatInteropKind.Node16 <= moduleFormatInterop && moduleFormatInterop <= ModuleFormatInteropKind.NodeNext) {
const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration);
const overrideHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l)) as ImportTypeNode | ImportDeclaration | ExportDeclaration | undefined;
// An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import {
mapIterator,
MapLike,
ModuleDetectionKind,
ModuleFormatInteropKind,
ModuleKind,
ModuleResolutionKind,
NewLineKind,
Expand Down Expand Up @@ -1196,6 +1197,20 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
category: Diagnostics.Modules,
description: Diagnostics.Conditions_to_set_in_addition_to_the_resolver_specific_defaults_when_resolving_imports,
},
{
name: "moduleFormatInterop",
type: new Map(Object.entries({
bundler: ModuleFormatInteropKind.Bundler,
bundlernode: ModuleFormatInteropKind.BundlerNode,
node16: ModuleFormatInteropKind.Node16,
nodenext: ModuleFormatInteropKind.NodeNext,
})),
affectsSemanticDiagnostics: true,
affectsBuildInfo: true,
category: Diagnostics.Modules,
description: Diagnostics.Specify_the_target_runtime_s_rules_for_ESM_CommonJS_interoperation,
defaultValueDescription: Diagnostics.node16_when_module_is_node16_nodenext_when_module_is_nodenext_bundler_otherwise,
},

// Source Maps
{
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6392,6 +6392,10 @@
"category": "Message",
"code": 6805
},
"Specify the target runtime's rules for ESM-CommonJS interoperation.": {
"category": "Message",
"code": 6806
},

"one of:": {
"category": "Message",
Expand Down Expand Up @@ -6521,6 +6525,10 @@
"category": "Error",
"code": 6931
},
"'node16' when 'module' is 'node16'; 'nodenext' when 'module' is 'nodenext'; 'bundler' otherwise.": {
"category": "Message",
"code": 6932
},

"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7290,6 +7290,7 @@ export interface CompilerOptions {
mapRoot?: string;
maxNodeModuleJsDepth?: number;
module?: ModuleKind;
moduleFormatInterop?: ModuleFormatInteropKind;
moduleResolution?: ModuleResolutionKind;
moduleSuffixes?: string[];
moduleDetection?: ModuleDetectionKind;
Expand Down Expand Up @@ -7423,6 +7424,14 @@ export enum ModuleKind {
Preserve = 200,
}

export enum ModuleFormatInteropKind {
Bundler = 1,
BundlerNode = 2,

Node16 = 100,
NodeNext = 199,
}

export const enum JsxEmit {
None = 0,
Preserve = 1,
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ import {
ModuleBlock,
ModuleDeclaration,
ModuleDetectionKind,
ModuleFormatInteropKind,
ModuleKind,
ModuleResolutionKind,
moduleResolutionOptionDeclarations,
Expand Down Expand Up @@ -8852,6 +8853,22 @@ export const computedOptions = createComputedCompilerOptions({
computedOptions.module.computeValue(compilerOptions) === ModuleKind.NodeNext ? ModuleDetectionKind.Force : ModuleDetectionKind.Auto);
},
},
moduleFormatInterop: {
dependencies: ["module", "target"],
computeValue: (compilerOptions): ModuleFormatInteropKind => {
if (compilerOptions.moduleFormatInterop !== undefined) {
return compilerOptions.moduleFormatInterop;
}
switch (computedOptions.module.computeValue(compilerOptions)) {
case ModuleKind.Node16:
return ModuleFormatInteropKind.Node16;
case ModuleKind.NodeNext:
return ModuleFormatInteropKind.NodeNext;
default:
return ModuleFormatInteropKind.Bundler;
}
},
},
isolatedModules: {
dependencies: ["verbatimModuleSyntax"],
computeValue: compilerOptions => {
Expand Down Expand Up @@ -9028,6 +9045,8 @@ export const getEmitModuleResolutionKind = computedOptions.moduleResolution.comp
/** @internal */
export const getEmitModuleDetectionKind = computedOptions.moduleDetection.computeValue;
/** @internal */
export const getModuleFormatInteropKind = computedOptions.moduleFormatInterop.computeValue;
/** @internal */
export const getIsolatedModules = computedOptions.isolatedModules.computeValue;
/** @internal */
export const getESModuleInterop = computedOptions.esModuleInterop.computeValue;
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class CompilerTest {
"jsx",
"module",
"moduleDetection",
"moduleFormatInterop",
"moduleResolution",
"noEmit",
"noImplicitAny",
Expand Down
7 changes: 7 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6942,6 +6942,7 @@ declare namespace ts {
mapRoot?: string;
maxNodeModuleJsDepth?: number;
module?: ModuleKind;
moduleFormatInterop?: ModuleFormatInteropKind;
moduleResolution?: ModuleResolutionKind;
moduleSuffixes?: string[];
moduleDetection?: ModuleDetectionKind;
Expand Down Expand Up @@ -7044,6 +7045,12 @@ declare namespace ts {
NodeNext = 199,
Preserve = 200,
}
enum ModuleFormatInteropKind {
Bundler = 1,
BundlerNode = 2,
Node16 = 100,
NodeNext = 199,
}
enum JsxEmit {
None = 0,
Preserve = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "moduleFormatInterop": "bundler", /* Specify the target runtime's rules for ESM-CommonJS interoperation. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"moduleFormatInterop": "bundler"
}
}
Loading