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

Commit

Permalink
use typescript to resolve files referenced in 'include'
Browse files Browse the repository at this point in the history
  • Loading branch information
christianscott committed May 26, 2022
1 parent 9049317 commit 2ddcc39
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
Empty file.
3 changes: 3 additions & 0 deletions __tests__/fixtures/tsconfigs copy/project/glob/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["../../includes/*.*", "./**/*.*"]
}
Empty file.
3 changes: 3 additions & 0 deletions __tests__/fixtures/tsconfigs copy/project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["../includes/a.d.ts"]
}
Empty file.
3 changes: 3 additions & 0 deletions __tests__/fixtures/tsconfigs/decls/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["./**/*.*"]
}
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions __tests__/fixtures/tsconfigs/extends/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../project/tsconfig.json",
"include": ["./**/*.*"]
}
Empty file.
3 changes: 3 additions & 0 deletions __tests__/fixtures/tsconfigs/glob/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["../decls/*.d.ts", "./**/*.*"]
}
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion __tests__/fixtures/tsconfigs/project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"include": ["../includes/a.d.ts"]
"include": ["../decls/one.d.ts", "./**/*.*"]
}
18 changes: 11 additions & 7 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,18 +508,22 @@ describe('dependencyTree', () => {
it('adds files in `include` from nearest tsconfig.json', async () => {
expect.hasAssertions();
const result = await dependencyTree.gather();
const decl1 = fixture('tsconfigs', 'decls', 'one.d.ts');
const decl2 = fixture('tsconfigs', 'decls', 'two.d.ts');
expect(result).toStrictEqual({
missing: new Map(),
resolved: new Map([
[fixture('tsconfigs', 'project', 'project.ts'), new Set([decl1])],
[
fixture('tsconfigs', 'project', 'a.ts'),
new Set([fixture('tsconfigs', 'includes', 'a.d.ts')]),
fixture('tsconfigs', 'project', 'nested', 'nested.ts'),
new Set([decl1]),
],
[
fixture('tsconfigs', 'project', 'nested', 'b.ts'),
new Set([fixture('tsconfigs', 'includes', 'a.d.ts')]),
],
[fixture('tsconfigs', 'includes', 'a.d.ts'), new Set([])],
[fixture('tsconfigs', 'glob', 'glob.ts'), new Set([decl1, decl2])],
// "includes" is overwritten in the child config
[fixture('tsconfigs', 'extends', 'extends.ts'), new Set([])],
// decls in the same dir that includes './**.*.*' depend on one another
[fixture('tsconfigs', 'decls', 'one.d.ts'), new Set([decl2])],
[fixture('tsconfigs', 'decls', 'two.d.ts'), new Set([decl1])],
]),
});
});
Expand Down
61 changes: 29 additions & 32 deletions src/processors/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,39 +190,36 @@ export class TypeScriptFileProcessor implements FileProcessor {
},
);

/**
* Finds the tsconfig.json that this file is included by. Does this by searching in the same directory,
* and then searching in each dir all the way to the root directory.
*/
private includesFromNearestTsconfigFile = memoize(
async (file: Path): Promise<string[]> => {
const maybeTsconfig = await findUp('tsconfig.json', {
cwd: path.dirname(file),
stopAt: this.rootDir,
});
if (maybeTsconfig == null) {
return [];
}

const json = JSON.parse(
await fs.promises.readFile(maybeTsconfig, 'utf8'),
);
if (!('include' in json)) {
return [];
}
private async includesFromNearestTsconfigFile(file: Path): Promise<string[]> {
const tsconfigPath = ts.findConfigFile(
path.dirname(file),
ts.sys.fileExists,
);
if (!tsconfigPath || !tsconfigPath.startsWith(this.rootDir)) {
return [];
}

const include = json['include'] as string[];
const tsconfigDir = path.dirname(maybeTsconfig);
return (
include
// filter out globbed includes
// TODO: support globbed includes
.filter((maybeGlob) => !maybeGlob.includes('*'))
// make the includes absolute
.map((include) => path.join(tsconfigDir, include))
);
},
);
debug.enabled = true;
const json = ts.parseJsonText(
tsconfigPath,
await fs.promises.readFile(tsconfigPath, 'utf-8'),
);
const parsed = ts.parseJsonSourceFileConfigFileContent(
json,
ts.sys,
path.dirname(tsconfigPath),
);
debug(path.relative(this.rootDir, tsconfigPath));
debug(parsed);

return parsed.fileNames.filter(
(fileName) =>
// only include .d.ts files. all other references should be made using `import` or `require`.
fileName.endsWith('.d.ts') &&
// files do not depend on themselves
fileName !== file,
);
}

// Finds an implicit 'import' in the entry point object literal, like:
//
Expand Down

0 comments on commit 2ddcc39

Please sign in to comment.