Skip to content

Commit

Permalink
Remove dependence on module compiler option to consider mts/cts fil…
Browse files Browse the repository at this point in the history
…es always modules (#49815)
  • Loading branch information
weswigham committed Jul 6, 2022
1 parent eb430f2 commit 9872184
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/compiler/utilities.ts
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
@@ -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 {};
@@ -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))

@@ -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

@@ -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;

0 comments on commit 9872184

Please sign in to comment.