Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit ced656f

Browse files
supports .ts files in nested dirs
1 parent ddc2dba commit ced656f

File tree

6 files changed

+52
-30
lines changed

6 files changed

+52
-30
lines changed

__tests__/fixtures/tsconfigs/project/nested/b.ts

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"include": ["../includes/a.d.ts"]
3+
}

__tests__/fixtures/tsconfigs/simple/tsconfig.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

__tests__/index.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,21 +502,26 @@ describe('dependencyTree', () => {
502502

503503
describe('tsconfig', () => {
504504
beforeEach(() => {
505-
dependencyTree = new DependencyTree([fixture('tsconfig')]);
505+
dependencyTree = new DependencyTree([fixture('tsconfigs')]);
506506
});
507507

508-
it('lists includes/a.d.ts as a dependency of simple/a.ts', async () => {
508+
it('adds files in `include` from nearest tsconfig.json', async () => {
509509
expect.hasAssertions();
510510
const result = await dependencyTree.gather();
511511
expect(result).toStrictEqual({
512-
missing: expect.any(Map),
512+
missing: new Map(),
513513
resolved: new Map([
514514
[
515-
fixture('simple', 'a.ts'),
516-
new Set([fixture('includes', 'a.d.ts')]),
515+
fixture('tsconfigs', 'project', 'a.ts'),
516+
new Set([fixture('tsconfigs', 'includes', 'a.d.ts')]),
517517
],
518+
[
519+
fixture('tsconfigs', 'project', 'nested', 'b.ts'),
520+
new Set([fixture('tsconfigs', 'includes', 'a.d.ts')]),
521+
],
522+
[fixture('tsconfigs', 'includes', 'a.d.ts'), new Set([])],
518523
]),
519524
});
520525
});
521-
})
526+
});
522527
});

src/processors/typescript.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,7 @@ export class TypeScriptFileProcessor implements FileProcessor {
110110
if (TypeScriptFileProcessor.isEntryPointFileName(file)) {
111111
filesList.push(TypeScriptFileProcessor.getEntryPointImport(file));
112112
}
113-
114-
const includes = this.includesFromNearestTsconfigFile(file);
115-
if (includes.length) {
116-
// only include non-globbed includes
117-
// TODO: support globbed includes
118-
const nonGlobIncludes = includes.filter(maybeGlob => !maybeGlob.includes('*'));
119-
filesList.push(...nonGlobIncludes);
120-
}
113+
filesList.push(...(await this.includesFromNearestTsconfigFile(file)));
121114

122115
for (const fileName of filesList) {
123116
const referencedFile = dependencyTree.transformReference(fileName, file);
@@ -201,20 +194,44 @@ export class TypeScriptFileProcessor implements FileProcessor {
201194
* Finds the tsconfig.json that this file is included by. Does this by searching in the same directory,
202195
* and then searching in each dir all the way to the root directory.
203196
*/
204-
private includesFromNearestTsconfigFile = memoize(async (file: Path): Promise<string[]> => {
205-
const segments = path.dirname(path.relative(this.rootDir, file)).split(path.sep);
206-
while (segments.length > 0) {
207-
const maybeTsconfig = path.join(this.rootDir, ...segments, 'tsconfig.json');
208-
try {
209-
const contents = JSON.parse(await fs.promises.readFile(file, 'utf8'));
210-
return 'includes' in contents ? (contents['includes'] as string[]) : [];
211-
} catch (err) {
212-
segments.pop(); // go up one dir
213-
continue;
197+
private includesFromNearestTsconfigFile = memoize(
198+
async (file: Path): Promise<string[]> => {
199+
const segments = path
200+
.dirname(path.relative(this.rootDir, file))
201+
.split(path.sep);
202+
while (segments.length > 0) {
203+
const maybeTsconfig = path.join(
204+
this.rootDir,
205+
...segments,
206+
'tsconfig.json',
207+
);
208+
let contents: string;
209+
try {
210+
contents = await fs.promises.readFile(maybeTsconfig, 'utf8');
211+
} catch (err) {
212+
segments.pop(); // go up one dir
213+
continue;
214+
}
215+
216+
const json = JSON.parse(contents);
217+
if (!('include' in json)) {
218+
return [];
219+
}
220+
221+
const include = json['include'] as string[];
222+
const tsconfigDir = path.dirname(maybeTsconfig);
223+
return (
224+
include
225+
// filter out globbed includes
226+
// TODO: support globbed includes
227+
.filter((maybeGlob) => !maybeGlob.includes('*'))
228+
// make the includes absolute
229+
.map((include) => path.join(tsconfigDir, include))
230+
);
214231
}
215-
}
216-
throw new Error(`could not find tsconfig.json for ${file}`);
217-
});
232+
return [];
233+
},
234+
);
218235

219236
// Finds an implicit 'import' in the entry point object literal, like:
220237
//

0 commit comments

Comments
 (0)