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 831ef09
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,34 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
throw err;
}

const stats = internalFsBinding.internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
StringPrototypeSlice(path, -1) : path));
let stats;
if (getOptionValue('--experimental-typescript')) {
if (StringPrototypeEndsWith(path, '/')) {
path = StringPrototypeSlice(path, 0, -1);
}
// Try to add .ts extension
let attemptTsPath = path;
if (!StringPrototypeEndsWith(attemptTsPath, '.ts')) {
attemptTsPath += '.ts';
}

stats = internalFsBinding.internalModuleStat(toNamespacedPath(attemptTsPath));
switch (stats) {
case 0:
// Found the .ts file
path = attemptTsPath;
break;
case 1:
throw new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base), String(resolved));
default:
// In case we cannot find the .ts file, we fill fall back to js
stats = internalFsBinding.internalModuleStat(path);
break;
}
} else {
stats = internalFsBinding.internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
StringPrototypeSlice(path, -1) : 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 831ef09

Please sign in to comment.