Skip to content
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

module: fix extensionless typescript in cjs loader #54062

Merged
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
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) &&
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
(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);
Loading