Skip to content

Commit

Permalink
fix(parseTsconfig): handle absolute baseUrl (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Jun 27, 2023
1 parent ea1b356 commit 136acf4
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 51 deletions.
7 changes: 6 additions & 1 deletion src/parse-tsconfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ export const parseTsconfig = (
const { compilerOptions } = config;

if (compilerOptions.baseUrl) {
compilerOptions.baseUrl = normalizePath(compilerOptions.baseUrl);
const resolvedBaseUrl = path.resolve(directoryPath, compilerOptions.baseUrl);
const relativeBaseUrl = normalizePath(path.relative(
directoryPath,
resolvedBaseUrl,
));
compilerOptions.baseUrl = relativeBaseUrl;
}

if (compilerOptions.outDir) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/is-relative-path-pattern.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Only works on POSIX paths. Apply `slash` first.
export const isRelativePathPattern = /^\.{1,2}(\/.*)?$/;
11 changes: 6 additions & 5 deletions src/utils/normalize-path.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import slash from 'slash';
import { isRelativePathPattern } from './is-relative-path-pattern.js';

export const normalizePath = (filePath: string) => slash(
isRelativePathPattern.test(filePath)
? filePath
: `./${filePath}`,
);
export const normalizePath = (filePath: string) => {
const normalizedPath = slash(filePath);
return isRelativePathPattern.test(normalizedPath)
? normalizedPath
: `./${normalizedPath}`;
};
117 changes: 72 additions & 45 deletions tests/specs/create-paths-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,62 +102,89 @@ export default testSuite(({ describe }) => {
});
});

test('baseUrl', async () => {
const fixture = await createFixture({
'tsconfig.json': createTsconfigJson({
compilerOptions: {
baseUrl: '.',
},
}),
});
describe('baseUrl', ({ test }) => {
test('baseUrl', async () => {
const fixture = await createFixture({
'tsconfig.json': createTsconfigJson({
compilerOptions: {
baseUrl: '.',
},
}),
});

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

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

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

await fixture.rm();
});
await fixture.rm();
});

test('baseUrl from extends', async () => {
const fixture = await createFixture({
'src/lib/file': '',
'some-dir/tsconfig.json': createTsconfigJson({
compilerOptions: {
baseUrl: '..',
paths: {
$lib: [
'src/lib',
],
'$lib/*': [
'src/lib/*',
],
test('inherited from extends', async () => {
const fixture = await createFixture({
'src/lib/file': '',
'some-dir/tsconfig.json': createTsconfigJson({
compilerOptions: {
baseUrl: '..',
paths: {
$lib: [
'src/lib',
],
'$lib/*': [
'src/lib/*',
],
},
},
},
}),
'tsconfig.json': createTsconfigJson({
extends: './some-dir/tsconfig.json',
}),
}),
'tsconfig.json': createTsconfigJson({
extends: './some-dir/tsconfig.json',
}),
});

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

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

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

await fixture.rm();
});

const tsconfig = getTsconfig(fixture.path);
expect(tsconfig).not.toBeNull();
test('absolute path', async () => {
const fixture = await createFixture();
await fixture.writeFile(
'tsconfig.json',
createTsconfigJson({
compilerOptions: {
baseUrl: fixture.path,
},
}),
);

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

const resolvedAttempts = await getTscResolution('$lib', fixture.path);
expect(matcher('$lib')).toStrictEqual([
resolvedAttempts[0].filePath.slice(0, -3),
]);
const matcher = createPathsMatcher(tsconfig!)!;
expect(matcher).not.toBeNull();

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

await fixture.rm();
});
});

test('exact match', async () => {
Expand Down
42 changes: 42 additions & 0 deletions tests/specs/parse-tsconfig/parses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,47 @@ export default testSuite(({ describe }) => {

await fixture.rm();
});

describe('baseUrl', ({ test }) => {
test('relative path', async () => {
const fixture = await createFixture({
'file.ts': '',
'tsconfig.json': createTsconfigJson({
compilerOptions: {
baseUrl: '.',
},
}),
});

const parsedTsconfig = parseTsconfig(path.join(fixture.path, 'tsconfig.json'));

const expectedTsconfig = await getTscTsconfig(fixture.path);
delete expectedTsconfig.files;

expect(parsedTsconfig).toStrictEqual(expectedTsconfig);

await fixture.rm();
});

test('absolute path', async () => {
const fixture = await createFixture({
'file.ts': '',
'tsconfig.json': createTsconfigJson({
compilerOptions: {
baseUrl: process.platform === 'win32' ? 'C:\\' : '/',
},
}),
});

const parsedTsconfig = parseTsconfig(path.join(fixture.path, 'tsconfig.json'));

const expectedTsconfig = await getTscTsconfig(fixture.path);
delete expectedTsconfig.files;

expect(parsedTsconfig).toStrictEqual(expectedTsconfig);

await fixture.rm();
});
});
});
});

0 comments on commit 136acf4

Please sign in to comment.