Skip to content

Commit 3551935

Browse files
TypeScript Botweswigham
TypeScript Bot
andauthored
Cherry-pick PR #49268 into release-4.7 (#49276)
Component commits: 5d13ab2 moduleDetection: auto makes cjs files parse as modules, module: node sets moduleDetection: force Co-authored-by: Wesley Wigham <[email protected]>
1 parent 4e91c6c commit 3551935

15 files changed

+75
-7
lines changed

Diff for: 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
}

Diff for: src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2747,7 +2747,7 @@ namespace ts {
27472747
return hasExportAssignmentSymbol(moduleSymbol);
27482748
}
27492749
// 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
2750-
return !file.externalModuleIndicator && !resolveExportByName(moduleSymbol, escapeLeadingUnderscores("__esModule"), /*sourceNode*/ undefined, dontResolveAlias);
2750+
return typeof file.externalModuleIndicator !== "object" && !resolveExportByName(moduleSymbol, escapeLeadingUnderscores("__esModule"), /*sourceNode*/ undefined, dontResolveAlias);
27512751
}
27522752

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

Diff for: src/compiler/utilities.ts

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

63196320
export function getSetExternalModuleIndicator(options: CompilerOptions): (file: SourceFile) => void {
@@ -6322,7 +6323,7 @@ namespace ts {
63226323
case ModuleDetectionKind.Force:
63236324
// All non-declaration files are modules, declaration files still do the usual isFileProbablyExternalModule
63246325
return (file: SourceFile) => {
6325-
file.externalModuleIndicator = !file.isDeclarationFile || isFileProbablyExternalModule(file);
6326+
file.externalModuleIndicator = isFileProbablyExternalModule(file) || !file.isDeclarationFile || undefined;
63266327
};
63276328
case ModuleDetectionKind.Legacy:
63286329
// Files are modules if they have imports, exports, or import.meta
@@ -6382,7 +6383,8 @@ namespace ts {
63826383
}
63836384

63846385
export function getEmitModuleDetectionKind(options: CompilerOptions) {
6385-
return options.moduleDetection || ModuleDetectionKind.Auto;
6386+
return options.moduleDetection ||
6387+
(getEmitModuleKind(options) === ModuleKind.Node16 || getEmitModuleKind(options) === ModuleKind.NodeNext ? ModuleDetectionKind.Force : ModuleDetectionKind.Auto);
63866388
}
63876389

63886390
export function hasJsonModuleEmitEnabled(options: CompilerOptions) {

Diff for: 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

Diff for: 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;

Diff for: 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.

Diff for: 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 };

Diff for: 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)