@webpieces/rules-config 0.4.819 → 0.4.820
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/api-lib-spelling-configs.d.ts +3 -3
- package/src/api-lib-spelling-configs.js +3 -3
- package/src/api-lib-spelling-configs.js.map +1 -1
- package/src/diff-scope.d.ts +15 -32
- package/src/diff-scope.js +27 -70
- package/src/diff-scope.js.map +1 -1
- package/src/index.d.ts +3 -5
- package/src/index.js +7 -13
- package/src/index.js.map +1 -1
- package/src/no-client-creation-config.js +1 -1
- package/src/no-client-creation-config.js.map +1 -1
- package/src/no-utility-types-config.d.ts +1 -1
- package/src/no-utility-types-config.js +1 -1
- package/src/no-utility-types-config.js.map +1 -1
- package/src/rule-configs.d.ts +9 -13
- package/src/rule-configs.js +23 -22
- package/src/rule-configs.js.map +1 -1
- package/src/rule-schemas.d.ts +0 -6
- package/src/rule-schemas.js +0 -10
- package/src/rule-schemas.js.map +1 -1
- package/src/seed-entry.js +5 -4
- package/src/seed-entry.js.map +1 -1
- package/templates/webpieces.noexitinmain.md +1 -1
- package/src/file-scope.d.ts +0 -79
- package/src/file-scope.js +0 -221
- package/src/file-scope.js.map +0 -1
package/src/file-scope.js
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* The FILE SET a diff-scoped rule judges — the one central place the whole-scope modes of #1027
|
|
4
|
-
* (`MODIFIED_PROJECTS`, `RUN_EVERY_TIME`) and the debug run's `--projects` filter are implemented.
|
|
5
|
-
*
|
|
6
|
-
* Every diff-scoped validator asks `DiffScope.getChangedFiles` which files to look at and
|
|
7
|
-
* `DiffScope.getFileDiff` which lines changed. Both consult the {@link FileScope} the caller is
|
|
8
|
-
* running inside (`diffScope.within(scope, work)`), so the widening happens ONCE, here, and no rule
|
|
9
|
-
* carries its own copy of it:
|
|
10
|
-
*
|
|
11
|
-
* DIFF — today's behaviour: the files the diff changed.
|
|
12
|
-
* MODIFIED_PROJECTS — those files PLUS every file of every project any changed file belongs to.
|
|
13
|
-
* RUN_EVERY_TIME — every file in the repo (tracked + untracked, honouring .gitignore).
|
|
14
|
-
*
|
|
15
|
-
* Under both whole-scope kinds every file is judged WHOLE: `getFileDiff` reports every line as added,
|
|
16
|
-
* so a rule's line- or method-scoped logic sees the entire file as new. A widening can therefore never
|
|
17
|
-
* report LESS than the diff-scoped mode it replaces.
|
|
18
|
-
*
|
|
19
|
-
* `projectRoots`, when set, keeps only files whose owning project (nearest `project.json`) is one of
|
|
20
|
-
* those roots. It is what `--projects` on the debug run compiles to; the gate never sets it.
|
|
21
|
-
*/
|
|
22
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.ScopedFileSelector = exports.RepoFileLister = exports.ProjectIndex = exports.WholeScopeModes = exports.FileScope = void 0;
|
|
24
|
-
const tslib_1 = require("tslib");
|
|
25
|
-
const child_process_1 = require("child_process");
|
|
26
|
-
const fs = tslib_1.__importStar(require("fs"));
|
|
27
|
-
const path = tslib_1.__importStar(require("path"));
|
|
28
|
-
const inform_ai_error_1 = require("./inform-ai-error");
|
|
29
|
-
const rule_configs_1 = require("./rule-configs");
|
|
30
|
-
const to_error_1 = require("./to-error");
|
|
31
|
-
/**
|
|
32
|
-
* The per-file judging mode a whole-scope run substitutes, most-whole first. Since a whole-scope run
|
|
33
|
-
* also reports every line as changed, any of these judges the entire file; the preference only picks
|
|
34
|
-
* the rule's most natural spelling of "the whole file".
|
|
35
|
-
*/
|
|
36
|
-
const WHOLE_FILE_JUDGE_PREFERENCE = [
|
|
37
|
-
'NEW_AND_MODIFIED_FILES',
|
|
38
|
-
'MODIFIED_CLASS',
|
|
39
|
-
'NEW_AND_MODIFIED_METHODS',
|
|
40
|
-
'NEW_AND_MODIFIED_CODE',
|
|
41
|
-
'NEW_METHODS',
|
|
42
|
-
];
|
|
43
|
-
/** Which files a rule run judges. Data-only; see the file comment. */
|
|
44
|
-
class FileScope {
|
|
45
|
-
kind;
|
|
46
|
-
projectRoots;
|
|
47
|
-
static DIFF = new FileScope('DIFF', null);
|
|
48
|
-
constructor(kind,
|
|
49
|
-
/** Repo-relative project roots to keep ('.' = a project at the repo root), or null = all. */
|
|
50
|
-
projectRoots) {
|
|
51
|
-
this.kind = kind;
|
|
52
|
-
this.projectRoots = projectRoots;
|
|
53
|
-
}
|
|
54
|
-
/** True when this is exactly today's diff scope — no widening, no project filter. */
|
|
55
|
-
isPlainDiff() {
|
|
56
|
-
return this.kind === 'DIFF' && this.projectRoots === null;
|
|
57
|
-
}
|
|
58
|
-
/** True for MODIFIED_PROJECTS / RUN_EVERY_TIME: every selected file is judged whole. */
|
|
59
|
-
isWhole() {
|
|
60
|
-
return this.kind !== 'DIFF';
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
exports.FileScope = FileScope;
|
|
64
|
-
/** The two questions every consumer of the whole-scope modes asks about a mode string. */
|
|
65
|
-
class WholeScopeModes {
|
|
66
|
-
/** True when `mode` is one of the two whole-scope modes. */
|
|
67
|
-
isWholeScopeMode(mode) {
|
|
68
|
-
return rule_configs_1.WHOLE_SCOPE_MODES.includes(mode ?? '');
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* The per-file mode a rule runs with while its FILE SET is widened, or null when the rule's mode
|
|
72
|
-
* set has no diff-scoped mode at all (a PROJECT_MODES / STRUCTURAL_MODES rule, whose own
|
|
73
|
-
* `MODIFIED_PROJECTS` / `RUN_EVERY_TIME` it implements itself and which must not be rewritten).
|
|
74
|
-
*/
|
|
75
|
-
wholeFileJudgeMode(modeSet) {
|
|
76
|
-
return WHOLE_FILE_JUDGE_PREFERENCE.find((m) => modeSet.includes(m)) ?? null;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
exports.WholeScopeModes = WholeScopeModes;
|
|
80
|
-
/**
|
|
81
|
-
* Maps files to the nx project that owns them (the nearest ancestor holding a `project.json`) and
|
|
82
|
-
* project NAMES to their roots. Parser-free and cached per instance: one run builds one index.
|
|
83
|
-
*/
|
|
84
|
-
class ProjectIndex {
|
|
85
|
-
workspaceRoot;
|
|
86
|
-
rootByDir = new Map();
|
|
87
|
-
namesToRoots = null;
|
|
88
|
-
constructor(workspaceRoot) {
|
|
89
|
-
this.workspaceRoot = workspaceRoot;
|
|
90
|
-
}
|
|
91
|
-
/** Repo-relative root of the project owning `relFile` ('.' for a repo-root project), or null. */
|
|
92
|
-
rootOf(relFile) {
|
|
93
|
-
return this.rootOfDir(path.posix.dirname(relFile.split(path.sep).join('/')));
|
|
94
|
-
}
|
|
95
|
-
/** The roots of the named projects. Throws naming every unknown name and the known ones. */
|
|
96
|
-
rootsFor(names) {
|
|
97
|
-
const index = this.names();
|
|
98
|
-
const unknown = names.filter((n) => !index.has(n));
|
|
99
|
-
if (unknown.length > 0) {
|
|
100
|
-
const known = Array.from(index.keys()).sort();
|
|
101
|
-
throw new inform_ai_error_1.InformAiError(`--projects names ${unknown.map((n) => `'${n}'`).join(', ')}, which no project.json in ` +
|
|
102
|
-
`${this.workspaceRoot} declares. Known projects: ${known.join(', ')}`);
|
|
103
|
-
}
|
|
104
|
-
return names.map((n) => index.get(n));
|
|
105
|
-
}
|
|
106
|
-
/** The project name declared for `root`, for reports (the root itself when no project.json names it). */
|
|
107
|
-
nameOf(root) {
|
|
108
|
-
for (const entry of this.names()) {
|
|
109
|
-
if (entry[1] === root)
|
|
110
|
-
return entry[0];
|
|
111
|
-
}
|
|
112
|
-
return root;
|
|
113
|
-
}
|
|
114
|
-
rootOfDir(dir) {
|
|
115
|
-
const cached = this.rootByDir.get(dir);
|
|
116
|
-
if (cached !== undefined)
|
|
117
|
-
return cached;
|
|
118
|
-
let found;
|
|
119
|
-
if (fs.existsSync(path.join(this.workspaceRoot, dir, 'project.json'))) {
|
|
120
|
-
found = dir === '' ? '.' : dir;
|
|
121
|
-
}
|
|
122
|
-
else if (dir === '.' || dir === '' || dir === '/') {
|
|
123
|
-
found = null;
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
found = this.rootOfDir(path.posix.dirname(dir));
|
|
127
|
-
}
|
|
128
|
-
this.rootByDir.set(dir, found);
|
|
129
|
-
return found;
|
|
130
|
-
}
|
|
131
|
-
names() {
|
|
132
|
-
if (this.namesToRoots !== null)
|
|
133
|
-
return this.namesToRoots;
|
|
134
|
-
const map = new Map();
|
|
135
|
-
for (const projectJson of new RepoFileLister().list(this.workspaceRoot, ['project.json', '*/project.json'])) {
|
|
136
|
-
const root = path.posix.dirname(projectJson);
|
|
137
|
-
map.set(this.readName(root), root === '' ? '.' : root);
|
|
138
|
-
}
|
|
139
|
-
this.namesToRoots = map;
|
|
140
|
-
return map;
|
|
141
|
-
}
|
|
142
|
-
// nx's own precedence: project.json `name`, else the sibling package.json `name`, else the dir.
|
|
143
|
-
readName(root) {
|
|
144
|
-
for (const file of ['project.json', 'package.json']) {
|
|
145
|
-
const name = this.readJsonName(path.join(this.workspaceRoot, root, file));
|
|
146
|
-
if (name !== null)
|
|
147
|
-
return name;
|
|
148
|
-
}
|
|
149
|
-
return path.posix.basename(root);
|
|
150
|
-
}
|
|
151
|
-
readJsonName(fullPath) {
|
|
152
|
-
if (!fs.existsSync(fullPath))
|
|
153
|
-
return null;
|
|
154
|
-
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
155
|
-
try {
|
|
156
|
-
const parsed = JSON.parse(fs.readFileSync(fullPath, 'utf-8'));
|
|
157
|
-
return typeof parsed.name === 'string' && parsed.name.length > 0 ? parsed.name : null;
|
|
158
|
-
}
|
|
159
|
-
catch (err) {
|
|
160
|
-
const error = (0, to_error_1.toError)(err);
|
|
161
|
-
void error; // swallow — a malformed project.json falls back to the directory name
|
|
162
|
-
return null;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
exports.ProjectIndex = ProjectIndex;
|
|
167
|
-
/** Lists repo files — tracked plus untracked-not-ignored — that still exist on disk. */
|
|
168
|
-
class RepoFileLister {
|
|
169
|
-
list(workspaceRoot, pathspecs) {
|
|
170
|
-
const spec = pathspecs.length === 0 ? '' : ` -- ${pathspecs.map((p) => `'${p}'`).join(' ')}`;
|
|
171
|
-
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
172
|
-
try {
|
|
173
|
-
const output = (0, child_process_1.execSync)(`git ls-files --cached --others --exclude-standard${spec}`, {
|
|
174
|
-
cwd: workspaceRoot,
|
|
175
|
-
encoding: 'utf-8',
|
|
176
|
-
maxBuffer: 256 * 1024 * 1024,
|
|
177
|
-
});
|
|
178
|
-
const files = output.split('\n').filter((f) => f.length > 0);
|
|
179
|
-
return Array.from(new Set(files)).filter((f) => fs.existsSync(path.join(workspaceRoot, f)));
|
|
180
|
-
}
|
|
181
|
-
catch (err) {
|
|
182
|
-
const error = (0, to_error_1.toError)(err);
|
|
183
|
-
// NOT swallowed: an empty list here would make a whole-scope run judge zero files and PASS,
|
|
184
|
-
// and a debug run report "TOTAL 0" — a false "nothing left" that is worse than a failure.
|
|
185
|
-
throw new inform_ai_error_1.InformAiError(`A whole-scope rule run (MODIFIED_PROJECTS / RUN_EVERY_TIME) could not list the repo's files: ` +
|
|
186
|
-
`\`git ls-files\` failed in ${workspaceRoot}: ${error.message}`, { cause: error });
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
exports.RepoFileLister = RepoFileLister;
|
|
191
|
-
/**
|
|
192
|
-
* Turns the diff's changed files into the files a {@link FileScope} selects. `keep` is the caller's
|
|
193
|
-
* own filter (ts-only, no tests) so a widened set obeys exactly the same in-scope test as the diff.
|
|
194
|
-
*/
|
|
195
|
-
class ScopedFileSelector {
|
|
196
|
-
select(workspaceRoot, scope, diffFiles, touchedFiles, pathspecs, keep) {
|
|
197
|
-
const projects = new ProjectIndex(workspaceRoot);
|
|
198
|
-
const selected = this.widen(workspaceRoot, scope, diffFiles, touchedFiles, pathspecs, keep, projects);
|
|
199
|
-
if (scope.projectRoots === null)
|
|
200
|
-
return selected;
|
|
201
|
-
const allowed = new Set(scope.projectRoots);
|
|
202
|
-
return selected.filter((f) => allowed.has(projects.rootOf(f) ?? ''));
|
|
203
|
-
}
|
|
204
|
-
widen(workspaceRoot, scope, diffFiles, touchedFiles, pathspecs, keep, projects) {
|
|
205
|
-
if (scope.kind === 'DIFF')
|
|
206
|
-
return [...diffFiles];
|
|
207
|
-
const repoFiles = new RepoFileLister().list(workspaceRoot, pathspecs).filter(keep);
|
|
208
|
-
if (scope.kind === 'RUN_EVERY_TIME')
|
|
209
|
-
return repoFiles;
|
|
210
|
-
const touched = new Set();
|
|
211
|
-
for (const file of touchedFiles()) {
|
|
212
|
-
const root = projects.rootOf(file);
|
|
213
|
-
if (root !== null)
|
|
214
|
-
touched.add(root);
|
|
215
|
-
}
|
|
216
|
-
const inTouched = repoFiles.filter((f) => touched.has(projects.rootOf(f) ?? ''));
|
|
217
|
-
return Array.from(new Set([...diffFiles, ...inTouched]));
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
exports.ScopedFileSelector = ScopedFileSelector;
|
|
221
|
-
//# sourceMappingURL=file-scope.js.map
|
package/src/file-scope.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"file-scope.js","sourceRoot":"","sources":["../../../../../packages/tooling/rules-config/src/file-scope.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;GAmBG;;;;AAEH,iDAAyC;AACzC,+CAAyB;AACzB,mDAA6B;AAE7B,uDAAkD;AAClD,iDAAmE;AACnE,yCAAqC;AAOrC;;;;GAIG;AACH,MAAM,2BAA2B,GAAG;IAChC,wBAAwB;IACxB,gBAAgB;IAChB,0BAA0B;IAC1B,uBAAuB;IACvB,aAAa;CACP,CAAC;AAEX,sEAAsE;AACtE,MAAa,SAAS;IAIL;IAEA;IALb,MAAM,CAAU,IAAI,GAAG,IAAI,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAEnD,YAC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`, `RUN_EVERY_TIME`) and the debug run's `--projects` filter are implemented.\n *\n * Every diff-scoped validator asks `DiffScope.getChangedFiles` which files to look at and\n * `DiffScope.getFileDiff` which lines changed. Both consult the {@link FileScope} the caller is\n * running inside (`diffScope.within(scope, work)`), so the widening happens ONCE, here, and no rule\n * carries its own copy of it:\n *\n * DIFF — today's behaviour: the files the diff changed.\n * MODIFIED_PROJECTS — those files PLUS every file of every project any changed file belongs to.\n * RUN_EVERY_TIME — every file in the repo (tracked + untracked, honouring .gitignore).\n *\n * Under both whole-scope kinds every file is judged WHOLE: `getFileDiff` reports every line as added,\n * so a rule's line- or method-scoped logic sees the entire file as new. A widening can therefore never\n * report LESS than the diff-scoped mode it replaces.\n *\n * `projectRoots`, when set, keeps only files whose owning project (nearest `project.json`) is one of\n * those roots. It is what `--projects` on the debug run compiles to; the gate never sets it.\n */\n\nimport { execSync } from 'child_process';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nimport { InformAiError } from './inform-ai-error';\nimport { WholeScopeMode, WHOLE_SCOPE_MODES } from './rule-configs';\nimport { toError } from './to-error';\n\n/** The one field of a project.json / package.json this module reads. */\ntype RawNamedJson = { name?: string };\n\nexport type FileScopeKind = 'DIFF' | WholeScopeMode;\n\n/**\n * The per-file judging mode a whole-scope run substitutes, most-whole first. Since a whole-scope run\n * also reports every line as changed, any of these judges the entire file; the preference only picks\n * the rule's most natural spelling of \"the whole file\".\n */\nconst WHOLE_FILE_JUDGE_PREFERENCE = [\n 'NEW_AND_MODIFIED_FILES',\n 'MODIFIED_CLASS',\n 'NEW_AND_MODIFIED_METHODS',\n 'NEW_AND_MODIFIED_CODE',\n 'NEW_METHODS',\n] as const;\n\n/** Which files a rule run judges. Data-only; see the file comment. */\nexport class FileScope {\n static readonly DIFF = new FileScope('DIFF', null);\n\n constructor(\n readonly kind: FileScopeKind,\n /** Repo-relative project roots to keep ('.' = a project at the repo root), or null = all. */\n readonly projectRoots: readonly string[] | null,\n ) {}\n\n /** True when this is exactly today's diff scope — no widening, no project filter. */\n isPlainDiff(): boolean {\n return this.kind === 'DIFF' && this.projectRoots === null;\n }\n\n /** True for MODIFIED_PROJECTS / RUN_EVERY_TIME: every selected file is judged whole. */\n isWhole(): boolean {\n return this.kind !== 'DIFF';\n }\n}\n\n/** The two questions every consumer of the whole-scope modes asks about a mode string. */\nexport class WholeScopeModes {\n /** True when `mode` is one of the two whole-scope modes. */\n isWholeScopeMode(mode: string | undefined): mode is WholeScopeMode {\n return (WHOLE_SCOPE_MODES as readonly string[]).includes(mode ?? '');\n }\n\n /**\n * The per-file mode a rule runs with while its FILE SET is widened, or null when the rule's mode\n * set has no diff-scoped mode at all (a PROJECT_MODES / STRUCTURAL_MODES rule, whose own\n * `MODIFIED_PROJECTS` / `RUN_EVERY_TIME` it implements itself and which must not be rewritten).\n */\n wholeFileJudgeMode(modeSet: readonly string[]): string | null {\n return WHOLE_FILE_JUDGE_PREFERENCE.find((m: string) => modeSet.includes(m)) ?? null;\n }\n}\n\n/**\n * Maps files to the nx project that owns them (the nearest ancestor holding a `project.json`) and\n * project NAMES to their roots. Parser-free and cached per instance: one run builds one index.\n */\nexport class ProjectIndex {\n private readonly rootByDir = new Map<string, string | null>();\n private namesToRoots: Map<string, string> | null = null;\n\n constructor(private readonly workspaceRoot: string) {}\n\n /** Repo-relative root of the project owning `relFile` ('.' for a repo-root project), or null. */\n rootOf(relFile: string): string | null {\n return this.rootOfDir(path.posix.dirname(relFile.split(path.sep).join('/')));\n }\n\n /** The roots of the named projects. Throws naming every unknown name and the known ones. */\n rootsFor(names: readonly string[]): string[] {\n const index = this.names();\n const unknown = names.filter((n: string) => !index.has(n));\n if (unknown.length > 0) {\n const known = Array.from(index.keys()).sort();\n throw new InformAiError(\n `--projects names ${unknown.map((n: string) => `'${n}'`).join(', ')}, which no project.json in ` +\n `${this.workspaceRoot} declares. Known projects: ${known.join(', ')}`,\n );\n }\n return names.map((n: string) => index.get(n) as string);\n }\n\n /** The project name declared for `root`, for reports (the root itself when no project.json names it). */\n nameOf(root: string): string {\n for (const entry of this.names()) {\n if (entry[1] === root) return entry[0];\n }\n return root;\n }\n\n private rootOfDir(dir: string): string | null {\n const cached = this.rootByDir.get(dir);\n if (cached !== undefined) return cached;\n let found: string | null;\n if (fs.existsSync(path.join(this.workspaceRoot, dir, 'project.json'))) {\n found = dir === '' ? '.' : dir;\n } else if (dir === '.' || dir === '' || dir === '/') {\n found = null;\n } else {\n found = this.rootOfDir(path.posix.dirname(dir));\n }\n this.rootByDir.set(dir, found);\n return found;\n }\n\n private names(): Map<string, string> {\n if (this.namesToRoots !== null) return this.namesToRoots;\n const map = new Map<string, string>();\n for (const projectJson of new RepoFileLister().list(this.workspaceRoot, ['project.json', '*/project.json'])) {\n const root = path.posix.dirname(projectJson);\n map.set(this.readName(root), root === '' ? '.' : root);\n }\n this.namesToRoots = map;\n return map;\n }\n\n // nx's own precedence: project.json `name`, else the sibling package.json `name`, else the dir.\n private readName(root: string): string {\n for (const file of ['project.json', 'package.json']) {\n const name = this.readJsonName(path.join(this.workspaceRoot, root, file));\n if (name !== null) return name;\n }\n return path.posix.basename(root);\n }\n\n private readJsonName(fullPath: string): string | null {\n if (!fs.existsSync(fullPath)) return null;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const parsed = JSON.parse(fs.readFileSync(fullPath, 'utf-8')) as RawNamedJson;\n return typeof parsed.name === 'string' && parsed.name.length > 0 ? parsed.name : null;\n } catch (err: unknown) {\n const error = toError(err);\n void error; // swallow — a malformed project.json falls back to the directory name\n return null;\n }\n }\n}\n\n/** Lists repo files — tracked plus untracked-not-ignored — that still exist on disk. */\nexport class RepoFileLister {\n list(workspaceRoot: string, pathspecs: readonly string[]): string[] {\n const spec = pathspecs.length === 0 ? '' : ` -- ${pathspecs.map((p: string) => `'${p}'`).join(' ')}`;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const output = execSync(`git ls-files --cached --others --exclude-standard${spec}`, {\n cwd: workspaceRoot,\n encoding: 'utf-8',\n maxBuffer: 256 * 1024 * 1024,\n });\n const files = output.split('\\n').filter((f: string) => f.length > 0);\n return Array.from(new Set(files)).filter((f: string) => fs.existsSync(path.join(workspaceRoot, f)));\n } catch (err: unknown) {\n const error = toError(err);\n // NOT swallowed: an empty list here would make a whole-scope run judge zero files and PASS,\n // and a debug run report \"TOTAL 0\" — a false \"nothing left\" that is worse than a failure.\n throw new InformAiError(\n `A whole-scope rule run (MODIFIED_PROJECTS / RUN_EVERY_TIME) could not list the repo's files: ` +\n `\\`git ls-files\\` failed in ${workspaceRoot}: ${error.message}`,\n { cause: error },\n );\n }\n }\n}\n\n/**\n * Turns the diff's changed files into the files a {@link FileScope} selects. `keep` is the caller's\n * own filter (ts-only, no tests) so a widened set obeys exactly the same in-scope test as the diff.\n */\nexport class ScopedFileSelector {\n select(\n workspaceRoot: string,\n scope: FileScope,\n diffFiles: readonly string[],\n touchedFiles: () => readonly string[],\n pathspecs: readonly string[],\n keep: (file: string) => boolean,\n ): string[] {\n const projects = new ProjectIndex(workspaceRoot);\n const selected = this.widen(workspaceRoot, scope, diffFiles, touchedFiles, pathspecs, keep, projects);\n if (scope.projectRoots === null) return selected;\n const allowed = new Set(scope.projectRoots);\n return selected.filter((f: string) => allowed.has(projects.rootOf(f) ?? ''));\n }\n\n private widen(\n workspaceRoot: string,\n scope: FileScope,\n diffFiles: readonly string[],\n touchedFiles: () => readonly string[],\n pathspecs: readonly string[],\n keep: (file: string) => boolean,\n projects: ProjectIndex,\n ): string[] {\n if (scope.kind === 'DIFF') return [...diffFiles];\n const repoFiles = new RepoFileLister().list(workspaceRoot, pathspecs).filter(keep);\n if (scope.kind === 'RUN_EVERY_TIME') return repoFiles;\n const touched = new Set<string>();\n for (const file of touchedFiles()) {\n const root = projects.rootOf(file);\n if (root !== null) touched.add(root);\n }\n const inTouched = repoFiles.filter((f: string) => touched.has(projects.rootOf(f) ?? ''));\n return Array.from(new Set([...diffFiles, ...inTouched]));\n }\n}\n"]}
|