Skip to content

Commit

Permalink
module: fix extensionless typescript in cjs loader
Browse files Browse the repository at this point in the history
PR-URL: #54062
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
marco-ippolito authored Jul 29, 2024
1 parent c40c41c commit cf9a814
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
8 changes: 6 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,13 @@ function getDefaultExtensions() {
let extensions = ObjectKeys(Module._extensions);
const tsEnabled = getOptionValue('--experimental-strip-types');
if (tsEnabled) {
// remove .ts and .cts from the default extensions
// to avoid extensionless require of .ts and .cts files.
// it behaves similarly to how .mjs is handled when --experimental-require-module
// is enabled.
extensions = ArrayPrototypeFilter(extensions, (ext) =>
ext !== '.ts' || Module._extensions['.ts'] !== loadTS ||
ext !== '.cts' || Module._extensions['.ts'] !== loadCTS,
(ext !== '.ts' || Module._extensions['.ts'] !== loadTS) &&
(ext !== '.cts' || Module._extensions['.cts'] !== loadCTS),
);
}

Expand Down
31 changes: 21 additions & 10 deletions test/es-module/test-typescript-commonjs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ test('require a .ts file with explicit extension succeeds', async () => {
strictEqual(result.code, 0);
});

// TODO(marco-ippolito) This test should fail because extensionless require
// but it's behaving like a .js file
test('eval require a .ts file with implicit extension fails', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
Expand All @@ -30,23 +28,36 @@ test('eval require a .ts file with implicit extension fails', async () => {
cwd: fixtures.path('typescript/ts'),
});

strictEqual(result.stderr, '');
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.code, 0);
strictEqual(result.stdout, '');
match(result.stderr, /Error: Cannot find module/);
strictEqual(result.code, 1);
});

test('eval require a .cts file with implicit extension fails', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
'require("./test-cts-typescript")',
'--no-warnings',
], {
cwd: fixtures.path('typescript/ts'),
});

strictEqual(result.stdout, '');
match(result.stderr, /Error: Cannot find module/);
strictEqual(result.code, 1);
});

// TODO(marco-ippolito) This test should fail because extensionless require
// but it's behaving like a .js file
test('require a .ts file with implicit extension fails', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--no-warnings',
fixtures.path('typescript/cts/test-extensionless-require.ts'),
]);

strictEqual(result.stderr, '');
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.code, 0);
strictEqual(result.stdout, '');
match(result.stderr, /Error: Cannot find module/);
strictEqual(result.code, 1);
});

test('expect failure of an .mts file with CommonJS syntax', async () => {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/typescript/ts/test-cts-typescript.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const str: string = "Hello, TypeScript!";
interface Foo {
bar: string;
}
console.log(str);

0 comments on commit cf9a814

Please sign in to comment.