mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 07:50:41 +00:00
module: fix extensionless typescript in cjs loader
PR-URL: https://github.com/nodejs/node/pull/54062 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
c40c41c285
commit
cf9a8140aa
@ -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),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -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',
|
||||
@ -30,13 +28,26 @@ 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',
|
||||
@ -44,9 +55,9 @@ test('require a .ts file with implicit extension fails', async () => {
|
||||
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 () => {
|
||||
|
||||
5
test/fixtures/typescript/ts/test-cts-typescript.cts
vendored
Normal file
5
test/fixtures/typescript/ts/test-cts-typescript.cts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
const str: string = "Hello, TypeScript!";
|
||||
interface Foo {
|
||||
bar: string;
|
||||
}
|
||||
console.log(str);
|
||||
Loading…
Reference in New Issue
Block a user