From 1263debdf36a9cfbe8c5db8be2d751df204ac225 Mon Sep 17 00:00:00 2001 From: dominikg Date: Mon, 4 Sep 2023 10:32:58 +0200 Subject: [PATCH] feat(find): ignore tsconfig.json files inside node_modules --- .changeset/tricky-trees-tell.md | 5 +++++ packages/tsconfck/src/find.js | 4 +++- packages/tsconfck/src/util.js | 13 +++++++++++++ packages/tsconfck/tests/find-all.js | 4 ++-- packages/tsconfck/tests/find-native.js | 11 +++++++++++ packages/tsconfck/tests/find.js | 11 +++++++++++ packages/tsconfck/tests/fixtures/find/a/.gitignore | 1 + .../tests/fixtures/find/a/node_modules/src/foo.ts | 0 .../fixtures/find/a/node_modules/tsconfig.json | 0 packages/tsconfck/types/index.d.ts.map | 2 +- 10 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 .changeset/tricky-trees-tell.md create mode 100644 packages/tsconfck/tests/fixtures/find/a/.gitignore create mode 100644 packages/tsconfck/tests/fixtures/find/a/node_modules/src/foo.ts create mode 100644 packages/tsconfck/tests/fixtures/find/a/node_modules/tsconfig.json diff --git a/.changeset/tricky-trees-tell.md b/.changeset/tricky-trees-tell.md new file mode 100644 index 0000000..3ccb8b7 --- /dev/null +++ b/.changeset/tricky-trees-tell.md @@ -0,0 +1,5 @@ +--- +'tsconfck': major +--- + +breaking(find): ignore tsconfig files inside node_modules diff --git a/packages/tsconfck/src/find.js b/packages/tsconfck/src/find.js index fff603c..3137164 100644 --- a/packages/tsconfck/src/find.js +++ b/packages/tsconfck/src/find.js @@ -1,5 +1,6 @@ import path from 'node:path'; import fs from 'node:fs'; +import { stripNodeModules } from './util.js'; /** * find the closest tsconfig.json file * @@ -9,7 +10,8 @@ import fs from 'node:fs'; */ export async function find(filename, options) { const cache = options?.cache; - let dir = path.dirname(path.resolve(filename)); + let dir = stripNodeModules(path.dirname(path.resolve(filename))); + if (cache?.hasTSConfigPath(dir)) { return cache.getTSConfigPath(dir); } diff --git a/packages/tsconfck/src/util.js b/packages/tsconfck/src/util.js index 55d2967..01af844 100644 --- a/packages/tsconfck/src/util.js +++ b/packages/tsconfck/src/util.js @@ -50,6 +50,19 @@ export async function resolveTSConfigJson(filename, cache) { }); } +/** + * remove path segments inside node_modules + * + * @param {string} dir an absolute directory path + * @returns {string} parent dir of node_modules if inside of node_modules, dir if not + */ +export const stripNodeModules = IS_POSIX + ? (dir) => { + const i = dir.indexOf('/node_modules/'); + return i > -1 ? dir.slice(0, i) : dir; + } + : (dir) => dir.replace(/[/\\]node_modules[/\\].*$/, ''); + /** * convert posix separator to native separator * diff --git a/packages/tsconfck/tests/find-all.js b/packages/tsconfck/tests/find-all.js index 8e9987a..55f4138 100644 --- a/packages/tsconfck/tests/find-all.js +++ b/packages/tsconfck/tests/find-all.js @@ -23,14 +23,14 @@ describe('find-all', () => { it('should find tsconfig in same directory', async () => { const fixtureDir = 'find/a'; const expected = absFixture(`${fixtureDir}/tsconfig.json`); - const found = await findAll(absFixture(fixtureDir)); + const found = await findAll(absFixture(fixtureDir), { skip: (dir) => dir === 'node_modules' }); expect(found).toEqual([expected]); }); it('should find tsconfig in child directory', async () => { const fixtureDir = 'find'; const expected = absFixture(`${fixtureDir}/a/tsconfig.json`); - const found = await findAll(absFixture(fixtureDir)); + const found = await findAll(absFixture(fixtureDir), { skip: (dir) => dir === 'node_modules' }); expect(found).toEqual([expected]); }); diff --git a/packages/tsconfck/tests/find-native.js b/packages/tsconfck/tests/find-native.js index 2418a9d..df4bf7e 100644 --- a/packages/tsconfck/tests/find-native.js +++ b/packages/tsconfck/tests/find-native.js @@ -47,6 +47,17 @@ describe('find-native', () => { } }); + it('should ignore tsconfig in node_modules directory', async () => { + const fixtureDir = 'find/a'; + const expected = native2posix(absFixture(`${fixtureDir}/tsconfig.json`)); + const relativeTS = relFixture(`${fixtureDir}/node_modules/lib/src/foo.ts`); + const absoluteTS = absFixture(`${fixtureDir}/node_modules/lib/src/foo.ts`); + const inputs = [relativeTS, `./${relativeTS}`, absoluteTS]; + for (const input of inputs) { + expect(await findNative(input), `input: ${input}`).toBe(expected); + } + }); + it('should stop searching at root', async () => { const fixtureDir = 'find-root/a'; const relativeTS = relFixture(`${fixtureDir}/b/foo.ts`); diff --git a/packages/tsconfck/tests/find.js b/packages/tsconfck/tests/find.js index 9f3c52c..d24ae70 100644 --- a/packages/tsconfck/tests/find.js +++ b/packages/tsconfck/tests/find.js @@ -44,6 +44,17 @@ describe('find', () => { } }); + it('should ignore tsconfig in node_modules directory', async () => { + const fixtureDir = 'find/a'; + const expected = absFixture(`${fixtureDir}/tsconfig.json`); + const relativeTS = relFixture(`${fixtureDir}/node_modules/lib/src/foo.ts`); + const absoluteTS = absFixture(`${fixtureDir}/node_modules/lib/src/foo.ts`); + const inputs = [relativeTS, `./${relativeTS}`, absoluteTS]; + for (const input of inputs) { + expect(await find(input), `input: ${input}`).toBe(expected); + } + }); + it('should stop searching at root', async () => { const fixtureDir = 'find-root/a'; const relativeTS = relFixture(`${fixtureDir}/b/foo.ts`); diff --git a/packages/tsconfck/tests/fixtures/find/a/.gitignore b/packages/tsconfck/tests/fixtures/find/a/.gitignore new file mode 100644 index 0000000..cf4bab9 --- /dev/null +++ b/packages/tsconfck/tests/fixtures/find/a/.gitignore @@ -0,0 +1 @@ +!node_modules diff --git a/packages/tsconfck/tests/fixtures/find/a/node_modules/src/foo.ts b/packages/tsconfck/tests/fixtures/find/a/node_modules/src/foo.ts new file mode 100644 index 0000000..e69de29 diff --git a/packages/tsconfck/tests/fixtures/find/a/node_modules/tsconfig.json b/packages/tsconfck/tests/fixtures/find/a/node_modules/tsconfig.json new file mode 100644 index 0000000..e69de29 diff --git a/packages/tsconfck/types/index.d.ts.map b/packages/tsconfck/types/index.d.ts.map index fbfa219..1450a4f 100644 --- a/packages/tsconfck/types/index.d.ts.map +++ b/packages/tsconfck/types/index.d.ts.map @@ -38,5 +38,5 @@ null, null ], - "mappings": ";;;;;;;;iBASsBA,IAAIA;;;;;;;;iBCYJC,OAAOA;;;;;;;iBCRbC,MAAMA;;;;;;;;;;iBCDAC,UAAUA;cCXnBC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCyBJC,KAAKA;cAqTdC,kBAAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCrTTC,WAAWA;cAyNpBC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;WCjPpBC,mBAAmBA;;;;;;;;;;;;;;;;WAgBnBC,oBAAoBA;;;;WAIpBC,sBAAsBA;;;;;;;;;WAStBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA6BnBC,0BAA0BA;;;;;;;;;;;;WAY1BC,yBAAyBA" + "mappings": ";;;;;;;;iBAUsBA,IAAIA;;;;;;;;iBCWJC,OAAOA;;;;;;;iBCRbC,MAAMA;;;;;;;;;;iBCDAC,UAAUA;cCXnBC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCyBJC,KAAKA;cAqTdC,kBAAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCrTTC,WAAWA;cAyNpBC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;WCjPpBC,mBAAmBA;;;;;;;;;;;;;;;;WAgBnBC,oBAAoBA;;;;WAIpBC,sBAAsBA;;;;;;;;;WAStBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA6BnBC,0BAA0BA;;;;;;;;;;;;WAY1BC,yBAAyBA" } \ No newline at end of file