From 9e78ec52d450d58743439358dd88e2066109743f Mon Sep 17 00:00:00 2001 From: stefanprobst Date: Fri, 19 Jul 2024 04:16:32 +0200 Subject: [PATCH] fix: handle relative tsconfig.json paths (#80) Co-authored-by: Hiroki Osame --- src/parse-tsconfig/index.ts | 2 +- tests/index.ts | 43 +++++++++++++++++++++-- tests/specs/create-paths-matcher.ts | 2 +- tests/specs/parse-tsconfig/parses.spec.ts | 2 +- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/parse-tsconfig/index.ts b/src/parse-tsconfig/index.ts index 014b5ee..bcc015e 100644 --- a/src/parse-tsconfig/index.ts +++ b/src/parse-tsconfig/index.ts @@ -211,4 +211,4 @@ const _parseTsconfig = ( export const parseTsconfig = ( tsconfigPath: string, cache: Cache = new Map(), -): TsConfigJsonResolved => _parseTsconfig(tsconfigPath, cache); +): TsConfigJsonResolved => _parseTsconfig(path.resolve(tsconfigPath), cache); diff --git a/tests/index.ts b/tests/index.ts index d37fc45..cdcc396 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -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); + } +}); diff --git a/tests/specs/create-paths-matcher.ts b/tests/specs/create-paths-matcher.ts index 0a80804..366a43a 100644 --- a/tests/specs/create-paths-matcher.ts +++ b/tests/specs/create-paths-matcher.ts @@ -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: { diff --git a/tests/specs/parse-tsconfig/parses.spec.ts b/tests/specs/parse-tsconfig/parses.spec.ts index ebc3c7e..6ca944d 100644 --- a/tests/specs/parse-tsconfig/parses.spec.ts +++ b/tests/specs/parse-tsconfig/parses.spec.ts @@ -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 () => {