Skip to content

Commit

Permalink
fix(createPathsMatcher): resolve absolute target paths
Browse files Browse the repository at this point in the history
fixes #50
  • Loading branch information
privatenumber committed Jun 27, 2023
1 parent 136acf4 commit 8acfb83
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/paths-matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const parsePaths = (
throw new Error('Non-relative paths are not allowed when \'baseUrl\' is not set. Did you forget a leading \'./\'?');
}

return path.join(absoluteBaseUrl, substitution);
return path.resolve(absoluteBaseUrl, substitution);
}),
} as PathEntry<string | StarPattern>;
});
Expand All @@ -54,7 +54,11 @@ export const createPathsMatcher = (
baseUrl || '.',
);

const pathEntries = paths ? parsePaths(paths, baseUrl, resolvedBaseUrl) : [];
const pathEntries = (
paths
? parsePaths(paths, baseUrl, resolvedBaseUrl)
: []
);

return (specifier: string) => {
if (isRelativePathPattern.test(specifier)) {
Expand Down
30 changes: 30 additions & 0 deletions tests/specs/create-paths-matcher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { createTsconfigJson, getTscResolution } from '../utils.js';
Expand Down Expand Up @@ -407,6 +408,35 @@ export default testSuite(({ describe }) => {
await fixture.rm();
});

test('matches absolute target paths', async () => {
const fixture = await createFixture();

await fixture.writeFile(
'tsconfig.json',
createTsconfigJson({
compilerOptions: {
baseUrl: fixture.path,
paths: {
dir: [path.join(fixture.path, 'dir')],
},
},
}),
);

const tsconfig = getTsconfig(fixture.path);
expect(tsconfig).not.toBeNull();

const matcher = createPathsMatcher(tsconfig!)!;
expect(tsconfig).not.toBeNull();

const resolvedAttempts = await getTscResolution('dir', fixture.path);
expect(matcher('dir')).toStrictEqual([
resolvedAttempts[0].filePath.slice(0, -3),
]);

await fixture.rm();
});

test('matches path that starts with .', async () => {
const fixture = await createFixture({
'tsconfig.json': createTsconfigJson({
Expand Down

0 comments on commit 8acfb83

Please sign in to comment.