reading-stone 0.0.0-stage → 0.2.0
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/LICENSE +201 -0
- package/NOTICE +11 -0
- package/README.md +274 -2
- package/dist/analysis.d.ts +341 -0
- package/dist/analysis.js +392 -0
- package/dist/cognitive.d.ts +163 -0
- package/dist/cognitive.js +380 -0
- package/dist/complexity.d.ts +102 -0
- package/dist/complexity.js +163 -0
- package/dist/coverage.d.ts +78 -0
- package/dist/coverage.js +635 -0
- package/dist/detector.d.ts +41 -0
- package/dist/detector.js +2134 -0
- package/dist/discovery.d.ts +330 -0
- package/dist/discovery.js +930 -0
- package/dist/doc-coverage.d.ts +155 -0
- package/dist/doc-coverage.js +543 -0
- package/dist/extract.d.ts +209 -0
- package/dist/extract.js +498 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +54 -0
- package/dist/internal.d.ts +75 -0
- package/dist/internal.js +63 -0
- package/dist/main.d.ts +8 -0
- package/dist/main.js +39 -0
- package/dist/models.d.ts +185 -0
- package/dist/models.js +170 -0
- package/dist/program.d.ts +207 -0
- package/dist/program.js +662 -0
- package/dist/protocol.d.ts +88 -0
- package/dist/protocol.js +74 -0
- package/dist/quality/assertions.d.ts +81 -0
- package/dist/quality/assertions.js +425 -0
- package/dist/quality/collect.d.ts +66 -0
- package/dist/quality/collect.js +412 -0
- package/dist/quality/inference.d.ts +86 -0
- package/dist/quality/inference.js +603 -0
- package/dist/quality/matchers.d.ts +129 -0
- package/dist/quality/matchers.js +220 -0
- package/dist/quality/pairing.d.ts +133 -0
- package/dist/quality/pairing.js +770 -0
- package/dist/quality/pipeline.d.ts +59 -0
- package/dist/quality/pipeline.js +202 -0
- package/dist/quality/types.d.ts +127 -0
- package/dist/quality/types.js +92 -0
- package/dist/safeio.d.ts +107 -0
- package/dist/safeio.js +314 -0
- package/dist/server.d.ts +19 -0
- package/dist/server.js +451 -0
- package/dist/signals/adapter.d.ts +103 -0
- package/dist/signals/adapter.js +323 -0
- package/dist/signals/caller-index.d.ts +117 -0
- package/dist/signals/caller-index.js +147 -0
- package/dist/signals/caller.d.ts +76 -0
- package/dist/signals/caller.js +108 -0
- package/dist/signals/evidence.d.ts +233 -0
- package/dist/signals/evidence.js +578 -0
- package/dist/signals/interface.d.ts +68 -0
- package/dist/signals/interface.js +87 -0
- package/dist/signals/jsdoc.d.ts +27 -0
- package/dist/signals/jsdoc.js +233 -0
- package/dist/signals/naming.d.ts +26 -0
- package/dist/signals/naming.js +160 -0
- package/dist/signals/routing.d.ts +163 -0
- package/dist/signals/routing.js +416 -0
- package/dist/signals/types.d.ts +146 -0
- package/dist/signals/types.js +99 -0
- package/dist/signals/visibility.d.ts +23 -0
- package/dist/signals/visibility.js +82 -0
- package/dist/taxonomy.d.ts +89 -0
- package/dist/taxonomy.js +217 -0
- package/package.json +61 -4
|
@@ -0,0 +1,930 @@
|
|
|
1
|
+
import { access, readdir, realpath, stat } from "node:fs/promises";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { MAX_FILE_BYTES, readBounded } from "./safeio.js";
|
|
4
|
+
/**
|
|
5
|
+
* Deterministic tree walker for file discovery (FR-002/FR-003, research R2).
|
|
6
|
+
*
|
|
7
|
+
* Performs an async recursive descent over a project tree and returns every
|
|
8
|
+
* *walk candidate*: an entry that is neither a directory nor a symlink and
|
|
9
|
+
* whose name carries an in-scope TypeScript or JavaScript-family extension.
|
|
10
|
+
* Candidates are returned as root-relative POSIX-separator paths in
|
|
11
|
+
* deterministic depth-first order — each directory's entries are visited in
|
|
12
|
+
* UTF-16 code-unit order — so any downstream per-file diagnostic emitted in
|
|
13
|
+
* walk order is deterministic too (research R8), not just the sorted
|
|
14
|
+
* response lists.
|
|
15
|
+
*
|
|
16
|
+
* The walker decides only what is visited and what is collected.
|
|
17
|
+
* `discover` (T024) sequences the per-candidate precedence chain — safety
|
|
18
|
+
* skip, `.d.ts` exclusion, generated-marker exclusion — over the walk
|
|
19
|
+
* results and hands the survivors to `classifyFiles`, which applies
|
|
20
|
+
* test-vs-source classification and lexicographically sorts the two final
|
|
21
|
+
* lists. `detectFramework` (US4, FR-006) reports the test framework from
|
|
22
|
+
* configuration presence at the resolved root, reading `package.json`
|
|
23
|
+
* through the shared safe reader, and `hasGeneratedMarker` (US4, FR-007)
|
|
24
|
+
* is the exported single-boolean view of the leading-comment scan the
|
|
25
|
+
* chain runs. All four surfaces are exported and composable; `discover`
|
|
26
|
+
* below runs them behind root-path validation (FR-010) and assembles the
|
|
27
|
+
* wire-shaped result (FR-001).
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* File extensions considered by discovery (FR-002). Case-sensitive.
|
|
31
|
+
*
|
|
32
|
+
* Also the parse-only module-specifier resolution domain's extension
|
|
33
|
+
* enumeration (spec 004's imports pairing tier, FR-004): the pairing
|
|
34
|
+
* strategies import the SAME set so specifier resolution and file
|
|
35
|
+
* discovery can never disagree about which extensions name an in-root
|
|
36
|
+
* module — one enumeration, two consumers, no drift.
|
|
37
|
+
*/
|
|
38
|
+
export const WALKED_EXTENSIONS = new Set([
|
|
39
|
+
".ts",
|
|
40
|
+
".tsx",
|
|
41
|
+
".js",
|
|
42
|
+
".jsx",
|
|
43
|
+
".mjs",
|
|
44
|
+
".cjs",
|
|
45
|
+
]);
|
|
46
|
+
/**
|
|
47
|
+
* Directory names pruned from the walk at any depth (FR-003) — exactly these
|
|
48
|
+
* five, matched by exact case-sensitive name. Every other directory,
|
|
49
|
+
* including dot-directories such as `.github`, is walked.
|
|
50
|
+
*/
|
|
51
|
+
const PRUNED_DIRECTORY_NAMES = new Set([
|
|
52
|
+
"node_modules",
|
|
53
|
+
"dist",
|
|
54
|
+
"build",
|
|
55
|
+
"coverage",
|
|
56
|
+
".git",
|
|
57
|
+
]);
|
|
58
|
+
/**
|
|
59
|
+
* Reports whether a basename carries one of the walked extensions (FR-002).
|
|
60
|
+
* Suffix match, case-sensitive: `.TS` is not a walked extension. Every
|
|
61
|
+
* JavaScript-family name that matches is collected — never silently skipped
|
|
62
|
+
* (Constitution Principle III).
|
|
63
|
+
*/
|
|
64
|
+
function hasWalkedExtension(name) {
|
|
65
|
+
for (const extension of WALKED_EXTENSIONS) {
|
|
66
|
+
if (name.endsWith(extension)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Reports whether a directory name is one of the five pruned directories
|
|
74
|
+
* (FR-003). Exact, case-sensitive match on the entry name.
|
|
75
|
+
*/
|
|
76
|
+
function isPrunedDirectoryName(name) {
|
|
77
|
+
return PRUNED_DIRECTORY_NAMES.has(name);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Orders dirents by entry name in UTF-16 code-unit order — the deterministic
|
|
81
|
+
* visit order (research R2/R8). The default string comparison (`<` and `>`)
|
|
82
|
+
* compares UTF-16 code units; `localeCompare` is locale- and ICU-dependent
|
|
83
|
+
* and is rejected for that reason.
|
|
84
|
+
*/
|
|
85
|
+
function compareDirentNames(a, b) {
|
|
86
|
+
if (a.name < b.name) {
|
|
87
|
+
return -1;
|
|
88
|
+
}
|
|
89
|
+
return a.name > b.name ? 1 : 0;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Walks one directory level, yielding walk candidates as root-relative
|
|
93
|
+
* POSIX-separator paths in deterministic depth-first order.
|
|
94
|
+
*
|
|
95
|
+
* Each recursion level is a separate awaited async call, so no explicit
|
|
96
|
+
* depth budget is needed for walking (research R2, unlike AST visitors).
|
|
97
|
+
*
|
|
98
|
+
* @param absDir Absolute platform-native path of the directory to read.
|
|
99
|
+
* @param relDir Root-relative POSIX path of the same directory; the empty
|
|
100
|
+
* string at the walk root.
|
|
101
|
+
*/
|
|
102
|
+
async function* walkDirectory(absDir, relDir) {
|
|
103
|
+
const entries = await readdir(absDir, { withFileTypes: true });
|
|
104
|
+
entries.sort(compareDirentNames);
|
|
105
|
+
for (const entry of entries) {
|
|
106
|
+
// Symlinks are never followed (FR-018): a symlinked directory is not
|
|
107
|
+
// descended and a symlinked file is never read. Dirents report symlinks
|
|
108
|
+
// as non-directories, but the check is kept explicit for intent.
|
|
109
|
+
if (entry.isSymbolicLink()) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const relPath = relDir === "" ? entry.name : `${relDir}/${entry.name}`;
|
|
113
|
+
if (entry.isDirectory()) {
|
|
114
|
+
if (!isPrunedDirectoryName(entry.name)) {
|
|
115
|
+
yield* walkDirectory(join(absDir, entry.name), relPath);
|
|
116
|
+
}
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
// Candidate: an entry that is neither directory nor symlink and carries
|
|
120
|
+
// an in-scope extension. Deliberately NOT filtered on
|
|
121
|
+
// `dirent.isFile()`: a FIFO or device named like a source file must
|
|
122
|
+
// remain a candidate so the US3 safe reader (later phase) can diagnose
|
|
123
|
+
// it with exactly one stderr line — filtering on isFile() would
|
|
124
|
+
// silently drop it (binding spec-review advisory for this task).
|
|
125
|
+
if (hasWalkedExtension(entry.name)) {
|
|
126
|
+
yield relPath;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Walks the project tree rooted at `root` and returns all walk candidates
|
|
132
|
+
* as root-relative POSIX-separator paths in deterministic walk order.
|
|
133
|
+
*
|
|
134
|
+
* The caller (`discover`, T009) validates and realpath-resolves `root`
|
|
135
|
+
* before walking, and `resolveRoot`'s root-level readability probe (T024)
|
|
136
|
+
* has already proven the root readable by the time the walk starts; this
|
|
137
|
+
* function assumes an existing, accessible directory. A `readdir` failure
|
|
138
|
+
* on a subdirectory propagates to the caller: silently skipping an
|
|
139
|
+
* unreadable subtree would silently omit its files — a silent false
|
|
140
|
+
* negative the constitution forbids (Principle II).
|
|
141
|
+
*
|
|
142
|
+
* @param root Absolute path of an existing directory to walk.
|
|
143
|
+
* @returns Walk candidates in deterministic depth-first order. The list is
|
|
144
|
+
* NOT globally sorted — sorting the response lists is `classifyFiles`'s
|
|
145
|
+
* responsibility (FR-008).
|
|
146
|
+
*/
|
|
147
|
+
export async function walkTree(root) {
|
|
148
|
+
const candidates = [];
|
|
149
|
+
for await (const relPath of walkDirectory(root, "")) {
|
|
150
|
+
candidates.push(relPath);
|
|
151
|
+
}
|
|
152
|
+
return candidates;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Basename segments (dot-delimited, case-sensitive) that carry the
|
|
156
|
+
* test-name signal (FR-005, research R13): a segment equal to `test` or
|
|
157
|
+
* `spec` appearing strictly between the first and last segments of the
|
|
158
|
+
* basename — the precise meaning of `*.test.*` / `*.spec.*` for walked
|
|
159
|
+
* files, which always end in a known extension.
|
|
160
|
+
*/
|
|
161
|
+
const TEST_NAME_SEGMENTS = new Set(["test", "spec"]);
|
|
162
|
+
/**
|
|
163
|
+
* Directory names that make every walked file beneath them a test file
|
|
164
|
+
* (FR-005). Matched exactly and case-sensitively against any directory
|
|
165
|
+
* component of the path — `mytests` and `testUtils` do not match.
|
|
166
|
+
*/
|
|
167
|
+
const TEST_DIRECTORY_NAMES = new Set([
|
|
168
|
+
"__tests__",
|
|
169
|
+
"tests",
|
|
170
|
+
"test",
|
|
171
|
+
]);
|
|
172
|
+
/**
|
|
173
|
+
* Returns the basename — the final `/`-separated segment — of a
|
|
174
|
+
* root-relative POSIX walk path.
|
|
175
|
+
*
|
|
176
|
+
* Hand-rolled rather than `node:path`'s `basename` because walk paths are
|
|
177
|
+
* POSIX-by-construction (`walkDirectory` joins with `/` only) while
|
|
178
|
+
* `path.basename` resolves separators platform-dependently; the
|
|
179
|
+
* determinism mandate (research R8) is not delegated to the platform.
|
|
180
|
+
*
|
|
181
|
+
* @param relPath Root-relative POSIX-separator walk path.
|
|
182
|
+
* @returns The basename of `relPath`; the whole path when it contains no
|
|
183
|
+
* `/` (a root-level file).
|
|
184
|
+
*/
|
|
185
|
+
function basenameOf(relPath) {
|
|
186
|
+
return relPath.slice(relPath.lastIndexOf("/") + 1);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Reports whether a basename is a TypeScript declaration file (FR-004):
|
|
190
|
+
* a name ending with the literal `.d.ts`. Declaration files carry no
|
|
191
|
+
* function bodies to analyze, so they are excluded from both lists as a
|
|
192
|
+
* documented policy — silently, with no diagnostic (research R13, spec
|
|
193
|
+
* edge case). Case-sensitive: `.D.TS` is not a declaration name.
|
|
194
|
+
*
|
|
195
|
+
* @param name Basename of a walk candidate.
|
|
196
|
+
* @returns `true` when `name` ends with the literal `.d.ts`.
|
|
197
|
+
*/
|
|
198
|
+
function isDeclarationFileName(name) {
|
|
199
|
+
return name.endsWith(".d.ts");
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Reports whether a basename carries the test-name signal (FR-005): some
|
|
203
|
+
* dot-delimited segment equal to `test` or `spec` that is neither the
|
|
204
|
+
* first nor the last segment.
|
|
205
|
+
*
|
|
206
|
+
* `foo.test.ts` and `foo.spec.jsx` are tests by name; `test.ts` and
|
|
207
|
+
* `test.foo.ts` are not (`test` is the first segment); neither is
|
|
208
|
+
* `foo.teststep.ts` — segment equality, not substring. Case-sensitive.
|
|
209
|
+
*
|
|
210
|
+
* @param name Basename of a walk candidate.
|
|
211
|
+
* @returns `true` when a middle dot-delimited segment is `test` or `spec`.
|
|
212
|
+
*/
|
|
213
|
+
function isTestName(name) {
|
|
214
|
+
const segments = name.split(".");
|
|
215
|
+
// slice(1, -1) is exactly "the segments strictly between the first and
|
|
216
|
+
// the last" — empty for two-segment basenames such as `test.ts`, which
|
|
217
|
+
// is why a first-segment `test` never matches.
|
|
218
|
+
for (const segment of segments.slice(1, -1)) {
|
|
219
|
+
if (TEST_NAME_SEGMENTS.has(segment)) {
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Reports whether any directory component of a root-relative POSIX walk
|
|
227
|
+
* path is exactly `__tests__`, `tests`, or `test` (FR-005).
|
|
228
|
+
*
|
|
229
|
+
* Only directory components count — never the basename — so a root-level
|
|
230
|
+
* `tests.ts` is not a test by this signal. Exact, case-sensitive match.
|
|
231
|
+
*
|
|
232
|
+
* @param relPath Root-relative POSIX-separator walk path.
|
|
233
|
+
* @returns `true` when a directory component is a test-directory name.
|
|
234
|
+
*/
|
|
235
|
+
function hasTestDirectoryComponent(relPath) {
|
|
236
|
+
const lastSlash = relPath.lastIndexOf("/");
|
|
237
|
+
// A root-level file (no `/` in the path) has no directory components.
|
|
238
|
+
if (lastSlash === -1) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
const directoryPart = relPath.slice(0, lastSlash);
|
|
242
|
+
for (const component of directoryPart.split("/")) {
|
|
243
|
+
if (TEST_DIRECTORY_NAMES.has(component)) {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Classifies walk candidates into disjoint, sorted source and test file
|
|
251
|
+
* lists (FR-004, FR-005, FR-008; research R8, R13).
|
|
252
|
+
*
|
|
253
|
+
* Precedence per research R13: a trailing-`.d.ts` basename is excluded
|
|
254
|
+
* from both lists first (silent documented policy, no diagnostic); then
|
|
255
|
+
* any test signal — name or directory — classifies the file into
|
|
256
|
+
* `testFiles`, because test wins over source; every other candidate is a
|
|
257
|
+
* source file. The chain's async stages — safety skip and
|
|
258
|
+
* generated-marker exclusion (FR-007) — run earlier, in `discover`'s
|
|
259
|
+
* screen (`screenCandidates`); a candidate arriving here from `discover`
|
|
260
|
+
* has already passed them, and the `.d.ts` check below is kept so direct
|
|
261
|
+
* callers of this composable surface get the same policy.
|
|
262
|
+
*
|
|
263
|
+
* Determinism (FR-008 / research R8): both lists are sorted with the
|
|
264
|
+
* default `Array.prototype.sort()` comparator — UTF-16 code-unit
|
|
265
|
+
* lexicographic, stable, locale-independent, and Node-version-stable.
|
|
266
|
+
* `localeCompare` is locale- and ICU-dependent and is never used.
|
|
267
|
+
*
|
|
268
|
+
* @param candidates Root-relative POSIX-separator walk candidates (the
|
|
269
|
+
* output of `walkTree`), in any order.
|
|
270
|
+
* @returns The disjoint, lexicographically sorted file lists.
|
|
271
|
+
*/
|
|
272
|
+
export function classifyFiles(candidates) {
|
|
273
|
+
const sourceFiles = [];
|
|
274
|
+
const testFiles = [];
|
|
275
|
+
for (const relPath of candidates) {
|
|
276
|
+
const name = basenameOf(relPath);
|
|
277
|
+
// Precedence chain (research R13): `.d.ts` exclusion drops the file
|
|
278
|
+
// from both lists before any test signal is considered.
|
|
279
|
+
if (isDeclarationFileName(name)) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (isTestName(name) || hasTestDirectoryComponent(relPath)) {
|
|
283
|
+
testFiles.push(relPath);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
sourceFiles.push(relPath);
|
|
287
|
+
}
|
|
288
|
+
sourceFiles.sort();
|
|
289
|
+
testFiles.sort();
|
|
290
|
+
return { sourceFiles, testFiles };
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Root-level vitest configuration basenames (FR-006, research R7): any
|
|
294
|
+
* one of these present at the resolved project root reports `"vitest"`.
|
|
295
|
+
* Exactly these four, matched by exact case-sensitive name.
|
|
296
|
+
*/
|
|
297
|
+
const VITEST_CONFIG_BASENAMES = [
|
|
298
|
+
"vitest.config.ts",
|
|
299
|
+
"vitest.config.js",
|
|
300
|
+
"vitest.config.mts",
|
|
301
|
+
"vitest.config.mjs",
|
|
302
|
+
];
|
|
303
|
+
/**
|
|
304
|
+
* Root-level jest configuration basenames (FR-006, research R7): any one
|
|
305
|
+
* of these present at the resolved project root reports `"jest"`. Exactly
|
|
306
|
+
* these five, matched by exact case-sensitive name.
|
|
307
|
+
*/
|
|
308
|
+
const JEST_CONFIG_BASENAMES = [
|
|
309
|
+
"jest.config.ts",
|
|
310
|
+
"jest.config.js",
|
|
311
|
+
"jest.config.mjs",
|
|
312
|
+
"jest.config.cjs",
|
|
313
|
+
"jest.config.json",
|
|
314
|
+
];
|
|
315
|
+
/**
|
|
316
|
+
* Reports whether `name` exists directly at the analysis root (research
|
|
317
|
+
* R7: root only, never recursive).
|
|
318
|
+
*
|
|
319
|
+
* Existence only — the candidate's contents are never read, so the probe
|
|
320
|
+
* cannot block on a FIFO or any other non-regular entry named like a
|
|
321
|
+
* config. `access` with no mode argument is the canonical F_OK existence
|
|
322
|
+
* check; it follows a symlink, which is the ordinary meaning of "this
|
|
323
|
+
* file exists at the root" — FR-018's never-follow rule governs tree
|
|
324
|
+
* walking and file reading, and neither happens here. Any failure — the
|
|
325
|
+
* usual absent name (ENOENT), or an inaccessible path — reads as "not
|
|
326
|
+
* present".
|
|
327
|
+
*
|
|
328
|
+
* @param root Resolved absolute analysis root.
|
|
329
|
+
* @param name Candidate basename, joined onto the root directly.
|
|
330
|
+
* @returns `true` when `name` exists at the root.
|
|
331
|
+
*/
|
|
332
|
+
async function nameExistsAtRoot(root, name) {
|
|
333
|
+
try {
|
|
334
|
+
await access(join(root, name));
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
catch {
|
|
338
|
+
// Absent (or uncheckable) at the root: not present.
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Reports whether any of `names` exists directly at the analysis root
|
|
344
|
+
* (FR-006, research R7).
|
|
345
|
+
*
|
|
346
|
+
* The loop short-circuits at the first name found; the boolean result is
|
|
347
|
+
* independent of the declared order, which exists only to keep the probe
|
|
348
|
+
* sequence itself deterministic.
|
|
349
|
+
*
|
|
350
|
+
* @param root Resolved absolute analysis root.
|
|
351
|
+
* @param names Candidate basenames, joined onto the root directly.
|
|
352
|
+
* @returns `true` when at least one name exists at the root.
|
|
353
|
+
*/
|
|
354
|
+
async function anyConfigPresentAtRoot(root, names) {
|
|
355
|
+
for (const name of names) {
|
|
356
|
+
if (await nameExistsAtRoot(root, name)) {
|
|
357
|
+
return true;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* The read budget for the root `package.json` (FR-006, research R7).
|
|
364
|
+
*
|
|
365
|
+
* The full shared hard cap rather than a smaller prefix budget: JSON
|
|
366
|
+
* cannot be parsed from a truncated prefix, so a partial read would only
|
|
367
|
+
* manufacture a misleading parse failure. At the full cap, a
|
|
368
|
+
* `package.json` over 16 MiB flows through `readBounded`'s own size-cap
|
|
369
|
+
* skip — one honest diagnostic — instead of a phantom "malformed"
|
|
370
|
+
* verdict, and every readable `package.json` within the cap is parsed in
|
|
371
|
+
* full.
|
|
372
|
+
*/
|
|
373
|
+
const PACKAGE_JSON_BUDGET = MAX_FILE_BYTES;
|
|
374
|
+
/**
|
|
375
|
+
* Reports whether the root `package.json` carries a `jest` key (FR-006,
|
|
376
|
+
* research R7), reading the file through the shared safe reader.
|
|
377
|
+
*
|
|
378
|
+
* The read runs `readBounded`'s full guarded sequence (FR-017, FR-018):
|
|
379
|
+
* containment, `lstat` (a symlinked `package.json` is skipped, never
|
|
380
|
+
* followed), the regular-file check, the 16 MiB cap, and strict UTF-8
|
|
381
|
+
* decoding. Every skip there — unreadable, symlink, oversized,
|
|
382
|
+
* undecodable — resolves `null` after `readBounded` itself has written
|
|
383
|
+
* the one `reading-stone: skipping package.json: <reason>` line, so this
|
|
384
|
+
* function writes nothing on that path: the spec pins exactly one line
|
|
385
|
+
* per skip (FR-017). A file that reads successfully but fails
|
|
386
|
+
* `JSON.parse` is the one skip `readBounded` cannot see, so this function
|
|
387
|
+
* writes the same-shaped line itself — still exactly one — and both
|
|
388
|
+
* conditions land in the same place: no `jest` key, and the request
|
|
389
|
+
* continues.
|
|
390
|
+
*
|
|
391
|
+
* A well-formed document whose top level is not an object with an own
|
|
392
|
+
* `jest` property (a number, a string, `null`, an array) simply carries
|
|
393
|
+
* no `jest` key — valid JSON in an unexpected shape is not a
|
|
394
|
+
* diagnostic-worthy condition. Neither is absence: the guarded read is
|
|
395
|
+
* gated on presence, because a root with no `package.json` is the normal
|
|
396
|
+
* state of a project without one, and FR-017's one-diagnostic skip rules
|
|
397
|
+
* exist for files that are present but cannot be read or parsed — a
|
|
398
|
+
* missing file (or a symlink whose target is missing) is silently treated
|
|
399
|
+
* as carrying no `jest` key, so it never manufactures a diagnostic.
|
|
400
|
+
*
|
|
401
|
+
* @param resolvedRoot Realpath-resolved absolute analysis root.
|
|
402
|
+
* @param stderr Injected diagnostic sink (Testability principle); stdout
|
|
403
|
+
* is never touched.
|
|
404
|
+
* @returns `true` only when the file read, parsed, and carried an own
|
|
405
|
+
* `jest` property.
|
|
406
|
+
*/
|
|
407
|
+
async function packageJsonHasJestKey(resolvedRoot, stderr) {
|
|
408
|
+
// Gate: absence is not a skip condition — only a package.json that
|
|
409
|
+
// exists flows into readBounded's one-diagnostic guarded sequence.
|
|
410
|
+
if (!(await nameExistsAtRoot(resolvedRoot, "package.json"))) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
const content = await readBounded(resolvedRoot, "package.json", PACKAGE_JSON_BUDGET, stderr);
|
|
414
|
+
if (content === null) {
|
|
415
|
+
// readBounded's skip path has already written the one diagnostic
|
|
416
|
+
// line; a second one here would violate the FR-017 contract.
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
let parsed;
|
|
420
|
+
try {
|
|
421
|
+
parsed = JSON.parse(content);
|
|
422
|
+
}
|
|
423
|
+
catch {
|
|
424
|
+
// The malformed-JSON skip readBounded cannot see: this function
|
|
425
|
+
// writes the one line itself, in readBounded's shape.
|
|
426
|
+
stderr.write("reading-stone: skipping package.json: malformed JSON\n");
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
if (parsed === null || typeof parsed !== "object") {
|
|
430
|
+
// Valid JSON whose top level is not an object carries no jest key.
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
return Object.hasOwn(parsed, "jest");
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Detects the project's test framework from configuration presence at the
|
|
437
|
+
* resolved project root (FR-006; research R7; Constitution Principle I).
|
|
438
|
+
*
|
|
439
|
+
* The detection basis is exactly what the spec pins (Assumption 2):
|
|
440
|
+
* configuration presence at the root, non-recursive, independent of
|
|
441
|
+
* source-file contents and of the process environment — so the same tree
|
|
442
|
+
* reports the same framework on every run (US4 scenario 3):
|
|
443
|
+
*
|
|
444
|
+
* 1. `"vitest"` when any of the four vitest config basenames exists at
|
|
445
|
+
* the root;
|
|
446
|
+
* 2. otherwise `"jest"` when any of the five jest config basenames exists
|
|
447
|
+
* at the root, or the root `package.json` carries a `jest` key;
|
|
448
|
+
* 3. otherwise `"unknown"`.
|
|
449
|
+
*
|
|
450
|
+
* vitest wins when both frameworks are configured (FR-006). The
|
|
451
|
+
* `package.json` read happens only on the jest-key fallback — a vitest
|
|
452
|
+
* or jest config file has already decided the answer — and flows through
|
|
453
|
+
* the shared safe reader with its 16 MiB cap and one-diagnostic skip
|
|
454
|
+
* rules: an unreadable or malformed `package.json` writes exactly one
|
|
455
|
+
* stderr diagnostic and is treated as having no `jest` key (FR-017),
|
|
456
|
+
* while a root with no `package.json` is silent — absence is not a skip
|
|
457
|
+
* condition.
|
|
458
|
+
*
|
|
459
|
+
* Sequenced into `discover()` (T024): the call replaced the US1
|
|
460
|
+
* `"unknown"` placeholder, and the caller's stderr sink flows through —
|
|
461
|
+
* the server handler forwards the server's own `stderr` — so a skipped
|
|
462
|
+
* `package.json` diagnostic reaches the process stderr, never stdout.
|
|
463
|
+
*
|
|
464
|
+
* @param resolvedRoot Realpath-resolved absolute analysis root, the form
|
|
465
|
+
* `resolveRoot` returns; every probe and read is relative to it.
|
|
466
|
+
* @param stderr Injected diagnostic sink (Testability principle — the
|
|
467
|
+
* server passes its own `stderr`); stdout is never touched.
|
|
468
|
+
* @returns The lowercase framework string: `"vitest"`, `"jest"`, or
|
|
469
|
+
* `"unknown"` (spec Assumption 11).
|
|
470
|
+
*/
|
|
471
|
+
export async function detectFramework(resolvedRoot, stderr) {
|
|
472
|
+
if (await anyConfigPresentAtRoot(resolvedRoot, VITEST_CONFIG_BASENAMES)) {
|
|
473
|
+
return "vitest";
|
|
474
|
+
}
|
|
475
|
+
if (await anyConfigPresentAtRoot(resolvedRoot, JEST_CONFIG_BASENAMES)) {
|
|
476
|
+
return "jest";
|
|
477
|
+
}
|
|
478
|
+
const hasJestKey = await packageJsonHasJestKey(resolvedRoot, stderr);
|
|
479
|
+
return hasJestKey ? "jest" : "unknown";
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* The prefix-read budget for generated-marker scanning (FR-007; research
|
|
483
|
+
* R6): at most the first 64 KiB of a candidate file is ever read.
|
|
484
|
+
*
|
|
485
|
+
* A prefix budget, deliberately distinct from `PACKAGE_JSON_BUDGET`'s
|
|
486
|
+
* full 16 MiB cap: only the leading comment block can carry an exclusion
|
|
487
|
+
* marker, so 64 KiB — large enough for stacked license headers before a
|
|
488
|
+
* marker, small enough that per-file I/O stays trivial — is all the
|
|
489
|
+
* scanner needs. The plan's performance goals pin the value (spec
|
|
490
|
+
* Assumption 5 makes the budget a plan-level decision). Truncation at
|
|
491
|
+
* the budget is deterministic: the same bytes always produce the same
|
|
492
|
+
* verdict.
|
|
493
|
+
*/
|
|
494
|
+
const MARKER_PREFIX_BUDGET = 64 * 1024;
|
|
495
|
+
/**
|
|
496
|
+
* The case-sensitive generated-code markers (FR-007): a leading comment
|
|
497
|
+
* block whose text contains either substring marks its file as
|
|
498
|
+
* generated. Case matters — `@Generated` and `code generated` are not
|
|
499
|
+
* markers.
|
|
500
|
+
*/
|
|
501
|
+
const GENERATED_MARKERS = ["@generated", "Code generated"];
|
|
502
|
+
/**
|
|
503
|
+
* Reports whether a UTF-16 code unit is whitespace the scanner skips
|
|
504
|
+
* between leading comments.
|
|
505
|
+
*
|
|
506
|
+
* The set mirrors the trivia the TypeScript scanner itself skips between
|
|
507
|
+
* tokens: ECMAScript's `WhiteSpace` and `LineTerminator` productions,
|
|
508
|
+
* plus next line (U+0085), which the compiler's scanner also treats as
|
|
509
|
+
* single-line whitespace. Spec-defined and engine-stable, never
|
|
510
|
+
* locale-dependent (research R8) — the skip set is a fixed function of
|
|
511
|
+
* the code unit.
|
|
512
|
+
*
|
|
513
|
+
* @param codeUnit A UTF-16 code unit from the decoded prefix.
|
|
514
|
+
* @returns `true` when the unit is skippable inter-comment whitespace.
|
|
515
|
+
*/
|
|
516
|
+
function isScannerWhitespace(codeUnit) {
|
|
517
|
+
return (codeUnit === 0x20 || // space
|
|
518
|
+
// Horizontal tab through carriage return: tab, line feed, vertical
|
|
519
|
+
// tab, form feed, carriage return.
|
|
520
|
+
(codeUnit >= 0x09 && codeUnit <= 0x0d) ||
|
|
521
|
+
codeUnit === 0x85 || // next line
|
|
522
|
+
codeUnit === 0xa0 || // no-break space
|
|
523
|
+
codeUnit === 0x1680 || // ogham space mark
|
|
524
|
+
// En quad through hair space: the Unicode Zs width range.
|
|
525
|
+
(codeUnit >= 0x2000 && codeUnit <= 0x200a) ||
|
|
526
|
+
codeUnit === 0x2028 || // line separator
|
|
527
|
+
codeUnit === 0x2029 || // paragraph separator
|
|
528
|
+
codeUnit === 0x202f || // narrow no-break space
|
|
529
|
+
codeUnit === 0x205f || // medium mathematical space
|
|
530
|
+
codeUnit === 0x3000 || // ideographic space
|
|
531
|
+
codeUnit === 0xfeff // zero-width no-break space (a mid-file BOM)
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Reports whether a UTF-16 code unit terminates a line comment: exactly
|
|
536
|
+
* the ECMAScript `LineTerminator` production — line feed, carriage
|
|
537
|
+
* return, LINE SEPARATOR (U+2028), PARAGRAPH SEPARATOR (U+2029) — the
|
|
538
|
+
* same four the TypeScript scanner ends comments at. Recognizing all
|
|
539
|
+
* four matters for accuracy: a U+2028 inside a comment ends it in real
|
|
540
|
+
* JavaScript, so a scanner that kept consuming would swallow following
|
|
541
|
+
* code as comment text and could false-match a marker inside it.
|
|
542
|
+
*
|
|
543
|
+
* @param codeUnit A UTF-16 code unit from the decoded prefix.
|
|
544
|
+
* @returns `true` when the unit ends the current line.
|
|
545
|
+
*/
|
|
546
|
+
function isLineTerminator(codeUnit) {
|
|
547
|
+
return (codeUnit === 0x0a || // line feed
|
|
548
|
+
codeUnit === 0x0d || // carriage return
|
|
549
|
+
codeUnit === 0x2028 || // line separator
|
|
550
|
+
codeUnit === 0x2029 // paragraph separator
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Reports whether one consumed comment's text contains a marker. The
|
|
555
|
+
* comment delimiters cannot themselves contain a marker, so the whole
|
|
556
|
+
* slice — delimiters included — is searched; case is significant
|
|
557
|
+
* (FR-007).
|
|
558
|
+
*
|
|
559
|
+
* @param text One consumed comment, delimiters included.
|
|
560
|
+
* @returns `true` when `@generated` or `Code generated` appears in it.
|
|
561
|
+
*/
|
|
562
|
+
function commentHasGeneratedMarker(text) {
|
|
563
|
+
return GENERATED_MARKERS.some((marker) => text.includes(marker));
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Reports whether the leading comment trivia of decoded file content
|
|
567
|
+
* carries a generated-code marker (FR-007; research R6).
|
|
568
|
+
*
|
|
569
|
+
* Bounded lexical-trivia scanning — not a reimplementation of
|
|
570
|
+
* TypeScript semantics (research R6; Principle III's target is the AST
|
|
571
|
+
* work of issue #3). The grammar walked is exactly what the spec pins:
|
|
572
|
+
* an optional single shebang line at position zero, then whitespace and
|
|
573
|
+
* consecutive line comments and block comments, stopping at the first
|
|
574
|
+
* non-comment content. Markers are matched only in consumed comment
|
|
575
|
+
* text, so the same words in a string literal or in a comment after
|
|
576
|
+
* code begins never exclude a file. A leading UTF-8 BOM never reaches
|
|
577
|
+
* this function — `readBounded`'s decoder strips one.
|
|
578
|
+
*
|
|
579
|
+
* Truncation determinism (research R8): every bound is the input string
|
|
580
|
+
* itself. A line comment that reaches the boundary without a terminator
|
|
581
|
+
* is consumed to the boundary, and a block comment left unterminated at
|
|
582
|
+
* the boundary is consumed to the boundary with its text scanned — the
|
|
583
|
+
* budget admits no further bytes, so the verdict is a pure function of
|
|
584
|
+
* the prefix.
|
|
585
|
+
*
|
|
586
|
+
* @param content The decoded file prefix (at most the 64 KiB budget).
|
|
587
|
+
* @returns `true` when any leading comment's text carries a marker.
|
|
588
|
+
*/
|
|
589
|
+
function leadingCommentBlockHasMarker(content) {
|
|
590
|
+
let index = 0;
|
|
591
|
+
// A shebang is recognized only at position zero and only once: a `#!`
|
|
592
|
+
// appearing after any other content is code, where the scan has
|
|
593
|
+
// already stopped.
|
|
594
|
+
if (content.startsWith("#!")) {
|
|
595
|
+
while (index < content.length && !isLineTerminator(content.charCodeAt(index))) {
|
|
596
|
+
index++;
|
|
597
|
+
}
|
|
598
|
+
// The line break ending the shebang is skipped as whitespace below.
|
|
599
|
+
}
|
|
600
|
+
while (index < content.length) {
|
|
601
|
+
while (index < content.length && isScannerWhitespace(content.charCodeAt(index))) {
|
|
602
|
+
index++;
|
|
603
|
+
}
|
|
604
|
+
if (index >= content.length) {
|
|
605
|
+
break;
|
|
606
|
+
}
|
|
607
|
+
if (content.startsWith("//", index)) {
|
|
608
|
+
// Line comment: its text runs to the next line terminator or the
|
|
609
|
+
// boundary. The terminator itself is absorbed by the whitespace
|
|
610
|
+
// skip on the next pass.
|
|
611
|
+
const start = index;
|
|
612
|
+
index += 2;
|
|
613
|
+
while (index < content.length && !isLineTerminator(content.charCodeAt(index))) {
|
|
614
|
+
index++;
|
|
615
|
+
}
|
|
616
|
+
if (commentHasGeneratedMarker(content.slice(start, index))) {
|
|
617
|
+
return true;
|
|
618
|
+
}
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
if (content.startsWith("/*", index)) {
|
|
622
|
+
// Block comment: closed by the first closing delimiter after the
|
|
623
|
+
// opener (block comments do not nest). When the closing delimiter
|
|
624
|
+
// lies beyond the truncation boundary, the comment is consumed to
|
|
625
|
+
// the boundary — the text present is scanned and the scan stops
|
|
626
|
+
// (a documented deterministic choice; see the function comment).
|
|
627
|
+
const start = index;
|
|
628
|
+
const closeIndex = content.indexOf("*/", index + 2);
|
|
629
|
+
index = closeIndex === -1 ? content.length : closeIndex + 2;
|
|
630
|
+
if (commentHasGeneratedMarker(content.slice(start, index))) {
|
|
631
|
+
return true;
|
|
632
|
+
}
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
// First non-comment, non-whitespace content: the leading comment
|
|
636
|
+
// block is over. Markers beyond this point — trailing comments,
|
|
637
|
+
// string literals, later code — must not match; research R6 rejects
|
|
638
|
+
// a regex over the raw prefix for exactly this failure mode.
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Runs the bounded leading-comment scan for one walk candidate (T024):
|
|
645
|
+
* `readBounded`'s guarded 64 KiB prefix read, then the marker scan over
|
|
646
|
+
* the decoded prefix — one read per candidate, shared by the exported
|
|
647
|
+
* `hasGeneratedMarker` and `discover`'s precedence chain.
|
|
648
|
+
*
|
|
649
|
+
* @param resolvedRoot Realpath-resolved absolute analysis root; the read
|
|
650
|
+
* is contained within it (FR-018).
|
|
651
|
+
* @param relPath Walk-built root-relative POSIX path, echoed verbatim in
|
|
652
|
+
* `readBounded`'s diagnostics.
|
|
653
|
+
* @param stderr Injected diagnostic sink; stdout is never touched.
|
|
654
|
+
* @returns The skip fact and the marker verdict as separate fields.
|
|
655
|
+
*/
|
|
656
|
+
async function scanLeadingComments(resolvedRoot, relPath, stderr) {
|
|
657
|
+
const prefix = await readBounded(resolvedRoot, relPath, MARKER_PREFIX_BUDGET, stderr);
|
|
658
|
+
if (prefix === null) {
|
|
659
|
+
// readBounded's skip path has already written the one diagnostic
|
|
660
|
+
// line; the caller excludes the file from both lists on this fact.
|
|
661
|
+
return { skipped: true, marked: false };
|
|
662
|
+
}
|
|
663
|
+
return { skipped: false, marked: leadingCommentBlockHasMarker(prefix) };
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Reports whether a walked file's leading comment block carries a
|
|
667
|
+
* generated-code marker (FR-007; research R6) — the signal that
|
|
668
|
+
* excludes the file from both discover lists.
|
|
669
|
+
*
|
|
670
|
+
* The read runs `readBounded`'s full guarded sequence (FR-017, FR-018)
|
|
671
|
+
* over the bounded 64 KiB prefix, so containment, the regular-file
|
|
672
|
+
* check, the 16 MiB size cap, and strict UTF-8 decoding all apply. A
|
|
673
|
+
* skipped file resolves `null` after `readBounded` itself has written
|
|
674
|
+
* the one `reading-stone: skipping <relPath>: <reason>` line; this
|
|
675
|
+
* function writes nothing on that path (FR-017's one-diagnostic
|
|
676
|
+
* contract) and reads the file as not marked — the safety skip already
|
|
677
|
+
* excludes it from both lists higher in the precedence chain (safety
|
|
678
|
+
* skip → `.d.ts` → generated marker → test → source), so a `false`
|
|
679
|
+
* here can never re-include it.
|
|
680
|
+
*
|
|
681
|
+
* A file that is merely marked — the exclusion this function reports —
|
|
682
|
+
* is a silent documented policy like `.d.ts` (FR-004, FR-007): no
|
|
683
|
+
* diagnostic, because nothing is wrong with the file; it is generated
|
|
684
|
+
* code, out of scope by policy.
|
|
685
|
+
*
|
|
686
|
+
* Sequenced into `discover()` (T024): the precedence chain runs this
|
|
687
|
+
* same scan through the shared internal `scanLeadingComments`, which
|
|
688
|
+
* keeps the safety-skip fact distinguishable from the marker verdict.
|
|
689
|
+
* The exported boolean view preserves T023's single-`false` contract: a
|
|
690
|
+
* safety-skipped file reads as not marked, sound in the chain because
|
|
691
|
+
* the skip itself — not the verdict — excludes the file from both lists.
|
|
692
|
+
*
|
|
693
|
+
* @param resolvedRoot Realpath-resolved absolute analysis root; the read
|
|
694
|
+
* is contained within it (FR-018).
|
|
695
|
+
* @param relPath Walk-built root-relative POSIX path, echoed verbatim in
|
|
696
|
+
* `readBounded`'s diagnostics.
|
|
697
|
+
* @param stderr Injected diagnostic sink (Testability principle — the
|
|
698
|
+
* server passes its own `stderr`); stdout is never touched.
|
|
699
|
+
* @returns `true` when the leading comment block's text contains the
|
|
700
|
+
* case-sensitive `@generated` or `Code generated` marker; `false` when
|
|
701
|
+
* it does not, when the prefix carries no comment trivia, or when the
|
|
702
|
+
* safe reader skipped the file.
|
|
703
|
+
*/
|
|
704
|
+
export async function hasGeneratedMarker(resolvedRoot, relPath, stderr) {
|
|
705
|
+
const { marked } = await scanLeadingComments(resolvedRoot, relPath, stderr);
|
|
706
|
+
return marked;
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Applies the per-candidate precedence chain's excluding stages to the
|
|
710
|
+
* walk results (T024; FR-004, FR-007, FR-017; research R13) and returns
|
|
711
|
+
* the candidates that survive into path-only classification.
|
|
712
|
+
*
|
|
713
|
+
* The chain, in the spec's order — safety skip → `.d.ts` → generated
|
|
714
|
+
* marker — with test-vs-source classification (in `classifyFiles`) as
|
|
715
|
+
* the final stages:
|
|
716
|
+
*
|
|
717
|
+
* 1. **Safety skip** (FR-017): every walked candidate flows through
|
|
718
|
+
* `readBounded`'s guarded 64 KiB prefix read first — the scan's own
|
|
719
|
+
* read, so each file is read exactly once. A `null` return (FIFO,
|
|
720
|
+
* oversized, unreadable, decode error, symlink) excludes the file
|
|
721
|
+
* from both lists; readBounded has already written the one
|
|
722
|
+
* `reading-stone: skipping <relPath>: <reason>` line. This is why
|
|
723
|
+
* the read precedes even the `.d.ts` name check: the safety reader —
|
|
724
|
+
* not the name — decides whether a candidate named like a source
|
|
725
|
+
* file is readable (a FIFO named `types.d.ts` is diagnosed, not
|
|
726
|
+
* silently dropped).
|
|
727
|
+
* 2. **`.d.ts` exclusion** (FR-004): silent documented policy.
|
|
728
|
+
* 3. **Generated-marker exclusion** (FR-007): silent documented policy —
|
|
729
|
+
* a marked file is generated code, out of scope by policy, and the
|
|
730
|
+
* exclusion wins over test classification (spec edge case).
|
|
731
|
+
*
|
|
732
|
+
* Candidates are screened in deterministic walk order, so the skip
|
|
733
|
+
* diagnostics land in walk order too (research R8).
|
|
734
|
+
*
|
|
735
|
+
* @param resolvedRoot Realpath-resolved absolute analysis root.
|
|
736
|
+
* @param candidates Walk candidates (`walkTree`'s output), in walk order.
|
|
737
|
+
* @param stderr Injected diagnostic sink; stdout is never touched.
|
|
738
|
+
* @returns The candidates that survive all three excluding stages, in
|
|
739
|
+
* walk order, for `classifyFiles` to split into test and source lists.
|
|
740
|
+
*/
|
|
741
|
+
export async function screenCandidates(resolvedRoot, candidates, stderr) {
|
|
742
|
+
const surviving = [];
|
|
743
|
+
for (const relPath of candidates) {
|
|
744
|
+
// Stage 1 — safety skip: the guarded prefix read runs for every
|
|
745
|
+
// candidate; a null return has already written its one diagnostic.
|
|
746
|
+
const { skipped, marked } = await scanLeadingComments(resolvedRoot, relPath, stderr);
|
|
747
|
+
if (skipped) {
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
// Stage 2 — `.d.ts` exclusion: silent documented policy (FR-004).
|
|
751
|
+
if (isDeclarationFileName(basenameOf(relPath))) {
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
// Stage 3 — generated-marker exclusion: silent documented policy
|
|
755
|
+
// (FR-007); wins over the test signals classifyFiles applies later.
|
|
756
|
+
if (marked) {
|
|
757
|
+
continue;
|
|
758
|
+
}
|
|
759
|
+
surviving.push(relPath);
|
|
760
|
+
}
|
|
761
|
+
return surviving;
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* The error thrown when a discovery root fails validation (FR-010): the
|
|
765
|
+
* root does not exist, is not a directory, or cannot be accessed.
|
|
766
|
+
*
|
|
767
|
+
* All three failure modes deliberately share one message: the wire
|
|
768
|
+
* contract (contracts/discover.md error table) maps every root failure
|
|
769
|
+
* to the same `-32602` response — `Invalid params: root_path is not a
|
|
770
|
+
* directory: <path>` — so this error mirrors that unified phrasing and
|
|
771
|
+
* the server handler can produce the contract message without
|
|
772
|
+
* re-classifying the failure. The underlying fs error, when one exists,
|
|
773
|
+
* rides along as the standard `cause` for debugging and never reaches
|
|
774
|
+
* the wire.
|
|
775
|
+
*
|
|
776
|
+
* The carried path is the cwd-resolved root (the absolute form of the
|
|
777
|
+
* caller's `root_path`), not the realpath-canonicalized form:
|
|
778
|
+
* `path.resolve` never touches the filesystem, so the reported path is
|
|
779
|
+
* a deterministic, caller-predictable function of the input and never
|
|
780
|
+
* leaks platform canonicalization (macOS resolves `/var` to
|
|
781
|
+
* `/private/var`) into error messages.
|
|
782
|
+
*/
|
|
783
|
+
export class InvalidRootPathError extends Error {
|
|
784
|
+
/** The cwd-resolved root path that failed validation. */
|
|
785
|
+
rootPath;
|
|
786
|
+
/**
|
|
787
|
+
* @param rootPath The cwd-resolved root path that failed validation;
|
|
788
|
+
* embedded in the message and exposed as a field so the server
|
|
789
|
+
* handler can build the contract's `-32602` response (research R9
|
|
790
|
+
* layering: discovery never imports from server).
|
|
791
|
+
* @param options Standard error options; fs failures pass the
|
|
792
|
+
* underlying error as `cause` (`ENOENT`, `EACCES`, ...).
|
|
793
|
+
*/
|
|
794
|
+
constructor(rootPath, options) {
|
|
795
|
+
super(`root_path is not a directory: ${rootPath}`, options);
|
|
796
|
+
this.rootPath = rootPath;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Reports whether a filesystem error is a permission failure (`EACCES` or
|
|
801
|
+
* `EPERM`) — the codes whose meaning on the resolved root itself is "this
|
|
802
|
+
* root cannot be read" and therefore belongs to root validation (FR-010),
|
|
803
|
+
* not to the fail-loud mid-walk surface.
|
|
804
|
+
*
|
|
805
|
+
* @param error A caught fs error from a root-level probe.
|
|
806
|
+
* @returns `true` when the error's code is `EACCES` or `EPERM`.
|
|
807
|
+
*/
|
|
808
|
+
function isPermissionDenied(error) {
|
|
809
|
+
if (!(error instanceof Error)) {
|
|
810
|
+
return false;
|
|
811
|
+
}
|
|
812
|
+
const code = error.code;
|
|
813
|
+
return code === "EACCES" || code === "EPERM";
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Validates and resolves the discovery root (FR-010, FR-018; research
|
|
817
|
+
* R5) in a fixed, security-relevant order:
|
|
818
|
+
*
|
|
819
|
+
* 1. `path.resolve` the (possibly relative) `root_path` against the
|
|
820
|
+
* analyzer process working directory (spec Assumption 8). Pure
|
|
821
|
+
* string arithmetic: the filesystem is not touched and symlinks are
|
|
822
|
+
* not followed — canonicalization is step 2's job.
|
|
823
|
+
* 2. `fs.realpath` the resolved root exactly once: a symlinked root is
|
|
824
|
+
* followed at this boundary only, and the path is canonicalized. A
|
|
825
|
+
* nonexistent root (`ENOENT`) or an inaccessible one (`EACCES`)
|
|
826
|
+
* fails here.
|
|
827
|
+
* 3. `stat` the real path and require a directory — a file used as the
|
|
828
|
+
* root rejects.
|
|
829
|
+
* 4. `readdir` the real root once as a readability probe — `realpath`
|
|
830
|
+
* and `stat` both succeed on an unreadable (mode 000) directory, so
|
|
831
|
+
* only this probe surfaces a root-level `EACCES`/`EPERM` as an
|
|
832
|
+
* `InvalidRootPathError` (T024, the T005-flagged src fix) rather than
|
|
833
|
+
* letting the walk's root `readdir` fail as an unexpected internal
|
|
834
|
+
* error; every other probe failure rethrows, and mid-walk
|
|
835
|
+
* subdirectory `readdir` failures stay fail-loud (Principle II).
|
|
836
|
+
*
|
|
837
|
+
* Containment downstream (FR-018) is enforced against the returned
|
|
838
|
+
* resolved root: the walker only ever joins root-relative segments onto
|
|
839
|
+
* it and never follows symlinks, so no walked path can escape it
|
|
840
|
+
* (research R5's structural layer).
|
|
841
|
+
*
|
|
842
|
+
* @param rootPath Absolute or relative root path from the caller.
|
|
843
|
+
* @returns The realpath-resolved absolute root to walk.
|
|
844
|
+
* @throws InvalidRootPathError when the root does not exist, is not a
|
|
845
|
+
* directory, or cannot be accessed.
|
|
846
|
+
*/
|
|
847
|
+
export async function resolveRoot(rootPath) {
|
|
848
|
+
const resolved = resolve(rootPath);
|
|
849
|
+
let realRoot;
|
|
850
|
+
let rootStat;
|
|
851
|
+
try {
|
|
852
|
+
// Nonexistent (ENOENT from realpath) and inaccessible (EACCES from
|
|
853
|
+
// either call) roots fail here; both map to the same unified
|
|
854
|
+
// rejection (FR-010).
|
|
855
|
+
realRoot = await realpath(resolved);
|
|
856
|
+
rootStat = await stat(realRoot);
|
|
857
|
+
}
|
|
858
|
+
catch (cause) {
|
|
859
|
+
throw new InvalidRootPathError(resolved, { cause });
|
|
860
|
+
}
|
|
861
|
+
if (!rootStat.isDirectory()) {
|
|
862
|
+
// A file (or any non-directory entry) used as the root rejects. The
|
|
863
|
+
// error carries the cwd-resolved path — see InvalidRootPathError.
|
|
864
|
+
throw new InvalidRootPathError(resolved);
|
|
865
|
+
}
|
|
866
|
+
// Step 4 — root readability probe (T024, the T005-flagged src fix):
|
|
867
|
+
// a chmod-000 root passes realpath and stat — only readdir surfaces
|
|
868
|
+
// the EACCES — so without this probe the walk's root readdir failure
|
|
869
|
+
// would escape as an internal error (-32603) instead of the contract's
|
|
870
|
+
// invalid-params surface (-32602). The listing is discarded — walkTree
|
|
871
|
+
// re-reads the root — because the probe's job is the permission
|
|
872
|
+
// verdict, kept here where the cwd-resolved path is in scope for
|
|
873
|
+
// InvalidRootPathError.
|
|
874
|
+
try {
|
|
875
|
+
await readdir(realRoot);
|
|
876
|
+
}
|
|
877
|
+
catch (cause) {
|
|
878
|
+
if (isPermissionDenied(cause)) {
|
|
879
|
+
throw new InvalidRootPathError(resolved, { cause });
|
|
880
|
+
}
|
|
881
|
+
throw cause;
|
|
882
|
+
}
|
|
883
|
+
return realRoot;
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Discovers a project's source and test files (FR-001, FR-006, FR-007,
|
|
887
|
+
* FR-010; research R5) — the implementation behind the `discover`
|
|
888
|
+
* JSON-RPC method.
|
|
889
|
+
*
|
|
890
|
+
* Composes the exported building blocks: `resolveRoot` validates and
|
|
891
|
+
* realpath-resolves the root, `walkTree` collects the walk candidates in
|
|
892
|
+
* deterministic order, `screenCandidates` applies the per-candidate
|
|
893
|
+
* precedence chain's excluding stages (safety skip → `.d.ts` → generated
|
|
894
|
+
* marker), `classifyFiles` splits the survivors into the disjoint,
|
|
895
|
+
* sorted lists, and `detectFramework` reports the test framework from
|
|
896
|
+
* configuration presence at the root. The returned object is already the
|
|
897
|
+
* wire-shaped result, so the server handler returns it as the JSON-RPC
|
|
898
|
+
* result unchanged (byte-level determinism is the protocol layer's
|
|
899
|
+
* serializer).
|
|
900
|
+
*
|
|
901
|
+
* Diagnostics (FR-017) flow to the injected stderr sink — the server
|
|
902
|
+
* handler forwards the server's own `stderr`, so a skip line reaches the
|
|
903
|
+
* process stderr, never stdout; direct callers that omit the sink get
|
|
904
|
+
* `process.stderr`.
|
|
905
|
+
*
|
|
906
|
+
* @param rootPath Absolute or relative project root; a relative path
|
|
907
|
+
* resolves against the analyzer process working directory (spec
|
|
908
|
+
* Assumption 8), and a symlinked root is realpath-resolved exactly once
|
|
909
|
+
* at the boundary (FR-018 / research R5).
|
|
910
|
+
* @param stderr Diagnostic sink for safe-reader skip lines
|
|
911
|
+
* (Testability principle); defaults to `process.stderr`.
|
|
912
|
+
* @returns The discovery result: sorted, root-relative POSIX
|
|
913
|
+
* `source_files` and `test_files`, and the detected test `framework`.
|
|
914
|
+
* @throws InvalidRootPathError when the root does not exist, is not a
|
|
915
|
+
* directory, or cannot be accessed (FR-010); the server handler maps it
|
|
916
|
+
* to the contract's `-32602` response.
|
|
917
|
+
*/
|
|
918
|
+
export async function discover(rootPath, stderr = process.stderr) {
|
|
919
|
+
const realRoot = await resolveRoot(rootPath);
|
|
920
|
+
const candidates = await walkTree(realRoot);
|
|
921
|
+
const surviving = await screenCandidates(realRoot, candidates, stderr);
|
|
922
|
+
const { sourceFiles, testFiles } = classifyFiles(surviving);
|
|
923
|
+
const framework = await detectFramework(realRoot, stderr);
|
|
924
|
+
return {
|
|
925
|
+
source_files: sourceFiles,
|
|
926
|
+
test_files: testFiles,
|
|
927
|
+
framework,
|
|
928
|
+
};
|
|
929
|
+
}
|
|
930
|
+
//# sourceMappingURL=discovery.js.map
|