Skip to content

Commit

Permalink
feat(find): ignore tsconfig.json files inside node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikg committed Sep 4, 2023
1 parent fec5da8 commit 1263deb
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-trees-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tsconfck': major
---

breaking(find): ignore tsconfig files inside node_modules
4 changes: 3 additions & 1 deletion packages/tsconfck/src/find.js
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/tsconfck/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions packages/tsconfck/tests/find-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});

Expand Down
11 changes: 11 additions & 0 deletions packages/tsconfck/tests/find-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
11 changes: 11 additions & 0 deletions packages/tsconfck/tests/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
1 change: 1 addition & 0 deletions packages/tsconfck/tests/fixtures/find/a/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion packages/tsconfck/types/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 1263deb

Please sign in to comment.