Skip to content

Commit 5d13ab2

Browse files
committed
moduleDetection: auto makes cjs files parse as modules, module: node sets moduleDetection: force
1 parent 8497483 commit 5d13ab2

15 files changed

+75
-7
lines changed

src/compiler/binder.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2834,12 +2834,14 @@ namespace ts {
28342834
}
28352835

28362836
function setCommonJsModuleIndicator(node: Node) {
2837-
if (file.externalModuleIndicator) {
2837+
if (file.externalModuleIndicator && file.externalModuleIndicator !== true) {
28382838
return false;
28392839
}
28402840
if (!file.commonJsModuleIndicator) {
28412841
file.commonJsModuleIndicator = node;
2842-
bindSourceFileAsExternalModule();
2842+
if (!file.externalModuleIndicator) {
2843+
bindSourceFileAsExternalModule();
2844+
}
28432845
}
28442846
return true;
28452847
}

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2746,7 +2746,7 @@ namespace ts {
27462746
return hasExportAssignmentSymbol(moduleSymbol);
27472747
}
27482748
// JS files have a synthetic default if they do not contain ES2015+ module syntax (export = is not valid in js) _and_ do not have an __esModule marker
2749-
return !file.externalModuleIndicator && !resolveExportByName(moduleSymbol, escapeLeadingUnderscores("__esModule"), /*sourceNode*/ undefined, dontResolveAlias);
2749+
return typeof file.externalModuleIndicator !== "object" && !resolveExportByName(moduleSymbol, escapeLeadingUnderscores("__esModule"), /*sourceNode*/ undefined, dontResolveAlias);
27502750
}
27512751

27522752
function getTargetOfImportClause(node: ImportClause, dontResolveAlias: boolean): Symbol | undefined {

src/compiler/utilities.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -6305,8 +6305,9 @@ namespace ts {
63056305
*/
63066306
function isFileForcedToBeModuleByFormat(file: SourceFile): true | undefined {
63076307
// Excludes declaration files - they still require an explicit `export {}` or the like
6308-
// for back compat purposes.
6309-
return file.impliedNodeFormat === ModuleKind.ESNext && !file.isDeclarationFile ? true : undefined;
6308+
// for back compat purposes. The only non-declaration files _not_ forced to be a module are `.js` files
6309+
// that aren't esm-mode (meaning not in a `type: module` scope).
6310+
return (file.impliedNodeFormat === ModuleKind.ESNext || (fileExtensionIsOneOf(file.fileName, [Extension.Cjs, Extension.Cts]))) && !file.isDeclarationFile ? true : undefined;
63106311
}
63116312

63126313
export function getSetExternalModuleIndicator(options: CompilerOptions): (file: SourceFile) => void {
@@ -6315,7 +6316,7 @@ namespace ts {
63156316
case ModuleDetectionKind.Force:
63166317
// All non-declaration files are modules, declaration files still do the usual isFileProbablyExternalModule
63176318
return (file: SourceFile) => {
6318-
file.externalModuleIndicator = !file.isDeclarationFile || isFileProbablyExternalModule(file);
6319+
file.externalModuleIndicator = isFileProbablyExternalModule(file) || !file.isDeclarationFile || undefined;
63196320
};
63206321
case ModuleDetectionKind.Legacy:
63216322
// Files are modules if they have imports, exports, or import.meta
@@ -6375,7 +6376,8 @@ namespace ts {
63756376
}
63766377

63776378
export function getEmitModuleDetectionKind(options: CompilerOptions) {
6378-
return options.moduleDetection || ModuleDetectionKind.Auto;
6379+
return options.moduleDetection ||
6380+
(getEmitModuleKind(options) === ModuleKind.Node16 || getEmitModuleKind(options) === ModuleKind.NodeNext ? ModuleDetectionKind.Force : ModuleDetectionKind.Auto);
63796381
}
63806382

63816383
export function hasJsonModuleEmitEnabled(options: CompilerOptions) {

tests/baselines/reference/moduleResolutionWithoutExtension8.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
import("./foo").then(x => x); // should error, ask for extension
44

55
//// [bar.cjs]
6+
"use strict";
7+
Object.defineProperty(exports, "__esModule", { value: true });
68
// Extensionless relative path dynamic import in a cjs module
79
import("./foo").then(x => x); // should error, ask for extension

tests/baselines/reference/nodeModulesAllowJsExportAssignment(module=node16).js

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ module.exports = a;
3434
const a = {};
3535
module.exports = a;
3636
//// [file.js]
37+
"use strict";
38+
Object.defineProperty(exports, "__esModule", { value: true });
3739
// cjs format file
3840
const a = {};
3941
module.exports = a;

tests/baselines/reference/nodeModulesAllowJsExportAssignment(module=nodenext).js

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ module.exports = a;
3434
const a = {};
3535
module.exports = a;
3636
//// [file.js]
37+
"use strict";
38+
Object.defineProperty(exports, "__esModule", { value: true });
3739
// cjs format file
3840
const a = {};
3941
module.exports = a;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/conformance/node/allowJs/nodeModulesAllowJsExportlessJsModuleDetectionAuto.ts] ////
2+
3+
//// [foo.cjs]
4+
// this file is a module despite having no imports
5+
//// [bar.js]
6+
// however this file is _not_ a module
7+
8+
//// [foo.cjs]
9+
"use strict";
10+
Object.defineProperty(exports, "__esModule", { value: true });
11+
// this file is a module despite having no imports
12+
//// [bar.js]
13+
// however this file is _not_ a module
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/node/allowJs/foo.cjs ===
2+
// this file is a module despite having no imports
3+
No type information for this code.=== tests/cases/conformance/node/allowJs/bar.js ===
4+
// however this file is _not_ a module
5+
No type information for this code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/node/allowJs/foo.cjs ===
2+
// this file is a module despite having no imports
3+
No type information for this code.=== tests/cases/conformance/node/allowJs/bar.js ===
4+
// however this file is _not_ a module
5+
No type information for this code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/conformance/node/allowJs/nodeModulesAllowJsExportlessJsModuleDetectionAuto.ts] ////
2+
3+
//// [foo.cjs]
4+
// this file is a module despite having no imports
5+
//// [bar.js]
6+
// however this file is _not_ a module
7+
8+
//// [foo.cjs]
9+
"use strict";
10+
Object.defineProperty(exports, "__esModule", { value: true });
11+
// this file is a module despite having no imports
12+
//// [bar.js]
13+
// however this file is _not_ a module
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/node/allowJs/foo.cjs ===
2+
// this file is a module despite having no imports
3+
No type information for this code.=== tests/cases/conformance/node/allowJs/bar.js ===
4+
// however this file is _not_ a module
5+
No type information for this code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/node/allowJs/foo.cjs ===
2+
// this file is a module despite having no imports
3+
No type information for this code.=== tests/cases/conformance/node/allowJs/bar.js ===
4+
// however this file is _not_ a module
5+
No type information for this code.

tests/baselines/reference/tripleSlashTypesReferenceWithMissingExports(module=node16).js

+2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ interface GlobalThing { a: number }
1414
const a: GlobalThing = { a: 0 };
1515

1616
//// [usage.js]
17+
"use strict";
1718
/// <reference types="pkg" />
19+
Object.defineProperty(exports, "__esModule", { value: true });
1820
const a = { a: 0 };

tests/baselines/reference/tripleSlashTypesReferenceWithMissingExports(module=nodenext).js

+2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ interface GlobalThing { a: number }
1414
const a: GlobalThing = { a: 0 };
1515

1616
//// [usage.js]
17+
"use strict";
1718
/// <reference types="pkg" />
19+
Object.defineProperty(exports, "__esModule", { value: true });
1820
const a = { a: 0 };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @module: node16,nodenext
2+
// @allowJs: true
3+
// @outDir: ./out
4+
// @moduleDetection: auto
5+
// @filename: foo.cjs
6+
// this file is a module despite having no imports
7+
// @filename: bar.js
8+
// however this file is _not_ a module

0 commit comments

Comments
 (0)