Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle relative tsconfig.json paths #80

Merged
merged 7 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parse-tsconfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
return extendsConfig;
};

const _parseTsconfig = (

Check warning on line 79 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Arrow function has a complexity of 21. Maximum allowed is 10
tsconfigPath: string,
cache?: Cache<string>,
circularExtendsTracker = new Set<string>(),
Expand Down Expand Up @@ -211,4 +211,4 @@
export const parseTsconfig = (
tsconfigPath: string,
cache: Cache<string> = new Map(),
): TsConfigJsonResolved => _parseTsconfig(tsconfigPath, cache);
): TsConfigJsonResolved => _parseTsconfig(path.resolve(tsconfigPath), cache);
43 changes: 41 additions & 2 deletions tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
import { describe } from 'manten';
import { describe, test, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { createTsconfigJson, getTscResolution } from './utils.js';
import { getTsconfig, createPathsMatcher } from '#get-tsconfig';

describe('get-tsconfig', ({ runTestSuite }) => {
await describe('get-tsconfig', ({ runTestSuite }) => {
runTestSuite(import('./specs/get-tsconfig.js'));
runTestSuite(import('./specs/parse-tsconfig/index.js'));
runTestSuite(import('./specs/create-paths-matcher.js'));
runTestSuite(import('./specs/create-files-matcher.js'));
});

/**
* This needs to happen in isolation because it changes process.cwd
*
* TODO: either: 1) update code to not rely on cwd, or 2) update manten to handle parallel: false
*/
test('paths > prefix match > nested directory', async () => {
await using fixture = await createFixture({
'dir/tsconfig.json': createTsconfigJson({
compilerOptions: {
paths: {
'@/*': ['./*'],
},
},
}),
});

const cwd = process.cwd();

try {
process.chdir(fixture.path);

const tsconfig = getTsconfig('./dir/tsconfig.json');
expect(tsconfig).not.toBeNull();

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

const resolvedAttempts = await getTscResolution('@/file', fixture.getPath('./dir'));
expect(matcher('@/file')).toStrictEqual([
resolvedAttempts[0].filePath.slice(0, -3),
]);
} finally {
process.chdir(cwd);
}
});
2 changes: 1 addition & 1 deletion tests/specs/create-paths-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default testSuite(({ describe }) => {
});

describe('baseUrl', ({ test }) => {
test('baseUrl', async () => {
test('absolute path', async () => {
await using fixture = await createFixture({
'tsconfig.json': createTsconfigJson({
compilerOptions: {
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/parse-tsconfig/parses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default testSuite(({ describe }) => {
test('non-existent path', async () => {
expect(
() => parseTsconfig('non-existent-path'),
).toThrow('Cannot resolve tsconfig at path: non-existent-path');
).toThrow('Cannot resolve tsconfig at path: ');
});

test('empty file', async () => {
Expand Down
Loading