Skip to content

Remove dependence on module compiler option to consider mts/cts files always modules #49815

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

Merged
merged 1 commit into from
Jul 6, 2022
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
7 changes: 2 additions & 5 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6319,7 +6319,7 @@ namespace ts {
// Excludes declaration files - they still require an explicit `export {}` or the like
// for back compat purposes. The only non-declaration files _not_ forced to be a module are `.js` files
// that aren't esm-mode (meaning not in a `type: module` scope).
return (file.impliedNodeFormat === ModuleKind.ESNext || (fileExtensionIsOneOf(file.fileName, [Extension.Cjs, Extension.Cts]))) && !file.isDeclarationFile ? true : undefined;
return (file.impliedNodeFormat === ModuleKind.ESNext || (fileExtensionIsOneOf(file.fileName, [Extension.Cjs, Extension.Cts, Extension.Mjs, Extension.Mts]))) && !file.isDeclarationFile ? true : undefined;
}

export function getSetExternalModuleIndicator(options: CompilerOptions): (file: SourceFile) => void {
Expand All @@ -6343,10 +6343,7 @@ namespace ts {
if (options.jsx === JsxEmit.ReactJSX || options.jsx === JsxEmit.ReactJSXDev) {
checks.push(isFileModuleFromUsingJSXTag);
}
const moduleKind = getEmitModuleKind(options);
if (moduleKind === ModuleKind.Node16 || moduleKind === ModuleKind.NodeNext) {
checks.push(isFileForcedToBeModuleByFormat);
}
checks.push(isFileForcedToBeModuleByFormat);
const combined = or(...checks);
const callback = (file: SourceFile) => void (file.externalModuleIndicator = combined(file));
return callback;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [tests/cases/compiler/moduleDetectionIsolatedModulesCjsFileScope.ts] ////

//// [filename.cts]
const a = 2;
//// [filename.mts]
const a = 2;

//// [filename.cjs]
const a = 2;
export {};
//// [filename.mjs]
const a = 2;
export {};


//// [filename.d.cts]
export {};
//// [filename.d.mts]
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/filename.cts ===
const a = 2;
>a : Symbol(a, Decl(filename.cts, 0, 5))

=== tests/cases/compiler/filename.mts ===
const a = 2;
>a : Symbol(a, Decl(filename.mts, 0, 5))

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/filename.cts ===
const a = 2;
>a : 2
>2 : 2

=== tests/cases/compiler/filename.mts ===
const a = 2;
>a : 2
>2 : 2

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @target: esnext
// @module: esnext
// @declaration: true
// @moduleResolution: node
// @isolatedModules: true
// @filename: filename.cts
const a = 2;
// @filename: filename.mts
const a = 2;