Skip to content

Commit 3b60d38

Browse files
imjuniclaude
andcommitted
feat(compilers): add getInheritedFileScope to resolve include/exclude via extends chain
Introduce getInheritedFileScope which traverses the tsconfig extends chain to find include and exclude patterns independently, respecting TypeScript's overwrite (not merge) semantics. This replaces the previous approach of reading only the raw JSON of the immediate tsconfig file, which missed patterns inherited from ancestor configs (e.g. tsconfig.vue.json → tsconfig.json → tsconfig.build.json). Update getTsIncludeFiles and getTsExcludeFiles to use getInheritedFileScope, removing the fileNames absolute-path fallback which was semantically inconsistent with the glob-pattern contract expected by IncludeContainer. Fix createBuildOptions to pass projectFilePath (tsconfig file path) and the correct projectDirPath (directory, not file path) to both functions. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 028460c commit 3b60d38

13 files changed

+247
-24
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["./tsconfig.inherit.base.json"]
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true
4+
},
5+
"include": ["src/**/*.ts", "src/**/*.vue"],
6+
"exclude": ["dist/**", "node_modules/**"]
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": [123],
3+
"include": ["src/**/*.ts"]
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfig.inherit.mid.json"
3+
}

examples/tsconfig.inherit.mid.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.inherit.base.json",
3+
"exclude": ["dist/**", "node_modules/**", "**/*.test.ts"]
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfig.inherit.base"
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true
4+
},
5+
"exclude": ["dist/**"]
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.inherit.base.json",
3+
"include": ["src/**/*.ts"]
4+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { getInheritedFileScope } from '#/compilers/getInheritedFileScope';
2+
import { posixJoin } from '#/modules/path/modules/posixJoin';
3+
import { describe, expect, it } from 'vitest';
4+
5+
const examplesDir = posixJoin(process.cwd(), 'examples');
6+
7+
describe('getInheritedFileScope', () => {
8+
it('returns include and exclude directly declared in the file', () => {
9+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.base.json');
10+
const result = getInheritedFileScope(tsconfigPath);
11+
12+
expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
13+
expect(result.exclude).toEqual(['dist/**', 'node_modules/**']);
14+
});
15+
16+
it('inherits include from base when child has no include', () => {
17+
// tsconfig.inherit.mid.json extends base and has only exclude
18+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.mid.json');
19+
const result = getInheritedFileScope(tsconfigPath);
20+
21+
expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
22+
expect(result.exclude).toEqual(['dist/**', 'node_modules/**', '**/*.test.ts']);
23+
});
24+
25+
it('traverses two levels to find include', () => {
26+
// tsconfig.inherit.leaf.json → mid (no include) → base (has include)
27+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.leaf.json');
28+
const result = getInheritedFileScope(tsconfigPath);
29+
30+
expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
31+
expect(result.exclude).toEqual(['dist/**', 'node_modules/**', '**/*.test.ts']);
32+
});
33+
34+
it('child include overrides base include', () => {
35+
// tsconfig.inherit.override.json extends base but declares its own include
36+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.override.json');
37+
const result = getInheritedFileScope(tsconfigPath);
38+
39+
expect(result.include).toEqual(['src/**/*.ts']);
40+
expect(result.exclude).toEqual(['dist/**', 'node_modules/**']);
41+
});
42+
43+
it('returns empty arrays when no include or exclude found in chain', () => {
44+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.empty.json');
45+
const result = getInheritedFileScope(tsconfigPath);
46+
47+
expect(result.include).toEqual([]);
48+
expect(result.exclude).toEqual([]);
49+
});
50+
51+
it('returns only exclude when chain has no include', () => {
52+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.no-include.json');
53+
const result = getInheritedFileScope(tsconfigPath);
54+
55+
expect(result.include).toEqual([]);
56+
expect(result.exclude).toEqual(['dist/**']);
57+
});
58+
59+
it('does not loop infinitely on a non-existent extends target', () => {
60+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.base.json');
61+
// base has no extends, so traversal stops after one file
62+
const result = getInheritedFileScope(tsconfigPath);
63+
64+
expect(result).toBeDefined();
65+
});
66+
67+
it('handles array extends (TypeScript 5.0+) and inherits include from the first entry', () => {
68+
// tsconfig.inherit.array-extends.json: { "extends": ["./tsconfig.inherit.base.json"] }
69+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.array-extends.json');
70+
const result = getInheritedFileScope(tsconfigPath);
71+
72+
expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
73+
expect(result.exclude).toEqual(['dist/**', 'node_modules/**']);
74+
});
75+
76+
it('resolves extends path that omits the .json extension', () => {
77+
// tsconfig.inherit.no-ext.json: { "extends": "./tsconfig.inherit.base" }
78+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.no-ext.json');
79+
const result = getInheritedFileScope(tsconfigPath);
80+
81+
expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
82+
expect(result.exclude).toEqual(['dist/**', 'node_modules/**']);
83+
});
84+
85+
it('stops traversal when extends array contains a non-string entry', () => {
86+
// tsconfig.inherit.invalid-extends.json: { "extends": [123], "include": [...] }
87+
// has its own include, but extends[0] is a number → traversal stops before following parent
88+
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.invalid-extends.json');
89+
const result = getInheritedFileScope(tsconfigPath);
90+
91+
expect(result.include).toEqual(['src/**/*.ts']);
92+
expect(result.exclude).toEqual([]);
93+
});
94+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { getFileScope } from '#/compilers/getFileScope';
2+
import path from 'node:path';
3+
import * as tsm from 'ts-morph';
4+
5+
interface IFileScope {
6+
include: string[];
7+
exclude: string[];
8+
}
9+
10+
/**
11+
* Traverse the tsconfig extends chain and collect include/exclude patterns.
12+
*
13+
* TypeScript's extends is an overwrite (not merge): the most-derived config wins.
14+
* include and exclude are resolved independently — each comes from the first file
15+
* in the chain that explicitly declares it.
16+
*
17+
* @param tsconfigPath - absolute path to the tsconfig file
18+
*/
19+
export function getInheritedFileScope(tsconfigPath: string): IFileScope {
20+
const visited = new Set<string>();
21+
let currentPath = path.resolve(tsconfigPath);
22+
let foundInclude: string[] | null = null;
23+
let foundExclude: string[] | null = null;
24+
25+
while (!visited.has(currentPath)) {
26+
visited.add(currentPath);
27+
28+
const configFile = tsm.ts.readConfigFile(currentPath, tsm.ts.sys.readFile.bind(tsm.ts));
29+
if (configFile.error != null) break;
30+
31+
const { include, exclude } = getFileScope(configFile.config);
32+
33+
if (foundInclude == null && include.length > 0) {
34+
foundInclude = include;
35+
}
36+
37+
if (foundExclude == null && exclude.length > 0) {
38+
foundExclude = exclude;
39+
}
40+
41+
if (foundInclude != null && foundExclude != null) break;
42+
43+
const raw = configFile.config as { extends?: unknown };
44+
const extendsValue: unknown = raw.extends;
45+
if (extendsValue == null) break;
46+
47+
// TypeScript 5.0+ supports array extends; earlier versions use a string
48+
const extendsEntries: unknown[] = Array.isArray(extendsValue) ? extendsValue : [extendsValue];
49+
const firstExtends: unknown = extendsEntries[0];
50+
if (typeof firstExtends !== 'string') break;
51+
52+
const resolved = path.resolve(path.dirname(currentPath), firstExtends);
53+
currentPath = resolved.endsWith('.json') ? resolved : `${resolved}.json`;
54+
}
55+
56+
return {
57+
include: foundInclude ?? [],
58+
exclude: foundExclude ?? [],
59+
};
60+
}

0 commit comments

Comments
 (0)