Skip to content

Commit

Permalink
check without extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Jul 2, 2024
1 parent b782365 commit be6a202
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,26 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
throw err;
}

const stats = internalFsBinding.internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
StringPrototypeSlice(path, -1) : path));
let stats;
path = StringPrototypeEndsWith(path, '/') ? StringPrototypeSlice(path, 0, -1) : path;
if (getOptionValue('--experimental-typescript')) {

stats = internalFsBinding.internalModuleStat(toNamespacedPath(path));
switch (stats) {
case 0:
// Found the file
break;
case 1:
throw new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base), String(resolved));
default:
// Try with .ts extension
path += '.ts';
stats = internalFsBinding.internalModuleStat(path);
break;
}
} else {
stats = internalFsBinding.internalModuleStat(path);
}

// Check for stats.isDirectory()
if (stats === 1) {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/typescript/test-import-no-extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { a } from './a';
console.log(a);
14 changes: 14 additions & 0 deletions test/parallel/test-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ test('execute a typescript with node_modules', async () => {
stdout: 'Hello, TypeScript!\n',
});
});

test('execute a typescript file with imports with no extensions', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-typescript',
fixtures.path('typescript/test-import-no-extension.ts'),
]);

deepStrictEqual(result, {
code: 0,
signal: null,
stderr: '',
stdout: 'Hello, TypeScript!\n',
});
});

0 comments on commit be6a202

Please sign in to comment.