@ryuugg/stanza 0.3.2 → 0.4.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/README.md +7 -5
- package/package.json +19 -11
- package/src/launcher +102 -8
- package/src/cli.ts +0 -659
- package/src/compile/darwin-arm64.ts +0 -3
- package/src/compile/darwin-x64.ts +0 -3
- package/src/compile/linux-arm64-musl.ts +0 -3
- package/src/compile/linux-arm64.ts +0 -3
- package/src/compile/linux-x64-musl.ts +0 -3
- package/src/compile/linux-x64.ts +0 -3
- package/src/compiled.ts +0 -9
- package/src/depth.ts +0 -368
- package/src/directories.ts +0 -77
- package/src/engine/attach.ts +0 -62
- package/src/engine/directives.ts +0 -157
- package/src/engine/doc.ts +0 -93
- package/src/engine/edits.ts +0 -79
- package/src/engine/explain.ts +0 -332
- package/src/engine/gaps.ts +0 -357
- package/src/engine/hunks.ts +0 -319
- package/src/engine/index.ts +0 -273
- package/src/engine/model.ts +0 -103
- package/src/engine/rules.ts +0 -103
- package/src/engine/types.ts +0 -38
- package/src/files.ts +0 -975
- package/src/format.ts +0 -159
- package/src/gitignore.ts +0 -280
- package/src/hook.ts +0 -365
- package/src/languages/index.ts +0 -34
- package/src/languages/javascript/ast.ts +0 -261
- package/src/languages/javascript/braces.ts +0 -374
- package/src/languages/javascript/config/biome.ts +0 -112
- package/src/languages/javascript/config/eslint.ts +0 -128
- package/src/languages/javascript/config/evaluate.ts +0 -278
- package/src/languages/javascript/config/find.ts +0 -105
- package/src/languages/javascript/config/glob.ts +0 -149
- package/src/languages/javascript/config/index.ts +0 -48
- package/src/languages/javascript/config/layers.ts +0 -468
- package/src/languages/javascript/config/module.ts +0 -327
- package/src/languages/javascript/config/oxlint.ts +0 -170
- package/src/languages/javascript/config/width.ts +0 -723
- package/src/languages/javascript/deserializers.ts +0 -17
- package/src/languages/javascript/embedded.ts +0 -7
- package/src/languages/javascript/entry.ts +0 -42
- package/src/languages/javascript/facts.ts +0 -294
- package/src/languages/javascript/index.ts +0 -20
- package/src/languages/javascript/lists.ts +0 -64
- package/src/languages/javascript/oracle.ts +0 -191
- package/src/languages/javascript/oxc-raw.d.ts +0 -31
- package/src/languages/javascript/parse.ts +0 -131
- package/src/languages/javascript/raw.ts +0 -95
- package/src/languages/javascript/scan.ts +0 -68
- package/src/languages/language.ts +0 -109
- package/src/native-file.d.ts +0 -4
- package/src/pool.ts +0 -312
- package/src/start.ts +0 -49
- package/src/step.ts +0 -113
- package/src/usage.ts +0 -35
- package/src/worker.ts +0 -4
package/src/hook.ts
DELETED
|
@@ -1,365 +0,0 @@
|
|
|
1
|
-
import { readFileSync, readdirSync, realpathSync } from "node:fs";
|
|
2
|
-
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
-
import type { Finding } from "./engine/types.ts";
|
|
4
|
-
import { isCandidate } from "./languages/index.ts";
|
|
5
|
-
import type { Io } from "./start.ts";
|
|
6
|
-
import { bothBraces, usage } from "./usage.ts";
|
|
7
|
-
|
|
8
|
-
export type HookCall =
|
|
9
|
-
| {
|
|
10
|
-
event: "write";
|
|
11
|
-
cwd: string;
|
|
12
|
-
toolInput: { file_path: string; content: string } & Record<string, unknown>;
|
|
13
|
-
}
|
|
14
|
-
| { event: "edit"; cwd: string; paths: string[] }
|
|
15
|
-
| { event: "stop"; cwd: string; written: Set<string> | undefined };
|
|
16
|
-
|
|
17
|
-
const hookSwitches = new Set(["--braces", "--hunks", "--no-braces"]);
|
|
18
|
-
|
|
19
|
-
function isCandidateFile(path: string): boolean {
|
|
20
|
-
try {
|
|
21
|
-
return isCandidate(basename(realpathSync(path)));
|
|
22
|
-
} catch {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function hookCall(args: string[], io: Io): HookCall | number {
|
|
28
|
-
if (io.env.AGENT_HOOKS === "0") return 0;
|
|
29
|
-
|
|
30
|
-
const unexpected = args.find(
|
|
31
|
-
(arg, index) => !hookSwitches.has(arg) || args.indexOf(arg) !== index,
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
if (unexpected !== undefined) {
|
|
35
|
-
io.stderr(`stanza hook: unexpected argument ${unexpected}\n${usage}\n`);
|
|
36
|
-
return 1;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (args.includes("--braces") && args.includes("--no-braces")) {
|
|
40
|
-
io.stderr(`stanza hook: ${bothBraces}\n${usage}\n`);
|
|
41
|
-
return 1;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const input = hookInput(Buffer.from(io.stdin()).toString("utf8"), io.cwd);
|
|
45
|
-
if ("error" in input) {
|
|
46
|
-
io.stderr(`stanza hook: ${input.error}\n`);
|
|
47
|
-
return 1;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (input.event === "ignored") return 0;
|
|
51
|
-
if (input.event === "write") return isCandidate(basename(input.toolInput.file_path)) ? input : 0;
|
|
52
|
-
if (input.event === "edit") return input.paths.some(isCandidateFile) ? input : 0;
|
|
53
|
-
if (input.stopHookActive) return 0;
|
|
54
|
-
|
|
55
|
-
if ("agentTranscriptPath" in input) {
|
|
56
|
-
if (input.agentTranscriptPath === undefined) return 0;
|
|
57
|
-
|
|
58
|
-
let text: string;
|
|
59
|
-
try {
|
|
60
|
-
text = readFileSync(input.agentTranscriptPath, "utf8");
|
|
61
|
-
} catch {
|
|
62
|
-
return 0;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const written = writtenFilesInTranscript(text);
|
|
66
|
-
if (written === undefined || ![...written].some(isCandidateFile)) return 0;
|
|
67
|
-
|
|
68
|
-
return { event: "stop", cwd: input.cwd, written };
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const written =
|
|
72
|
-
input.transcriptPath === undefined ? undefined : writtenFiles(input.transcriptPath);
|
|
73
|
-
|
|
74
|
-
if (written !== undefined && ![...written].some(isCandidateFile)) return 0;
|
|
75
|
-
|
|
76
|
-
return { event: "stop", cwd: input.cwd, written };
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export const claudeCodeHooks =
|
|
80
|
-
'{ "hooks": { "PreToolUse": [{ "matcher": "Write", "hooks": [{ "type": "command", "command": "stanza hook" }] }], "PostToolUse": [{ "matcher": "Edit|MultiEdit", "hooks": [{ "type": "command", "command": "stanza hook" }] }], "Stop": [{ "hooks": [{ "type": "command", "command": "stanza hook" }] }] } }';
|
|
81
|
-
|
|
82
|
-
export function hookInput(
|
|
83
|
-
text: string,
|
|
84
|
-
fallbackCwd: string,
|
|
85
|
-
):
|
|
86
|
-
| ({
|
|
87
|
-
event: "stop";
|
|
88
|
-
cwd: string;
|
|
89
|
-
stopHookActive: boolean;
|
|
90
|
-
} & ({ transcriptPath: string | undefined } | { agentTranscriptPath: string | undefined }))
|
|
91
|
-
| {
|
|
92
|
-
event: "write";
|
|
93
|
-
cwd: string;
|
|
94
|
-
toolInput: { file_path: string; content: string } & Record<string, unknown>;
|
|
95
|
-
}
|
|
96
|
-
| { event: "edit"; cwd: string; paths: string[] }
|
|
97
|
-
| { event: "ignored" }
|
|
98
|
-
| { error: string } {
|
|
99
|
-
let input: unknown;
|
|
100
|
-
try {
|
|
101
|
-
input = JSON.parse(text);
|
|
102
|
-
} catch {
|
|
103
|
-
return { error: "input must be valid JSON" };
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (!isRecord(input)) return { error: "input must be a JSON object" };
|
|
107
|
-
|
|
108
|
-
const event = "hook_event_name" in input ? input.hook_event_name : undefined;
|
|
109
|
-
if (event != null && typeof event !== "string")
|
|
110
|
-
return { error: "hook_event_name must be a string" };
|
|
111
|
-
|
|
112
|
-
if (event != null && !events.has(event)) return { event: "ignored" };
|
|
113
|
-
|
|
114
|
-
const cwd = "cwd" in input ? input.cwd : undefined;
|
|
115
|
-
if (cwd != null && typeof cwd !== "string") return { error: "cwd must be a string" };
|
|
116
|
-
|
|
117
|
-
const dir = cwd == null ? fallbackCwd : resolve(fallbackCwd, cwd);
|
|
118
|
-
if (event === "PostToolUse") {
|
|
119
|
-
const paths = editedPaths(input);
|
|
120
|
-
if (paths.length === 0) return { event: "ignored" };
|
|
121
|
-
return { event: "edit", cwd: dir, paths: paths.map((path) => resolve(dir, path)) };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (event === "PreToolUse") {
|
|
125
|
-
const toolInput = "tool_input" in input ? input.tool_input : undefined;
|
|
126
|
-
if (
|
|
127
|
-
!("tool_name" in input && input.tool_name === "Write") ||
|
|
128
|
-
!isRecord(toolInput) ||
|
|
129
|
-
typeof toolInput.file_path !== "string" ||
|
|
130
|
-
typeof toolInput.content !== "string"
|
|
131
|
-
)
|
|
132
|
-
return { event: "ignored" };
|
|
133
|
-
|
|
134
|
-
return {
|
|
135
|
-
event: "write",
|
|
136
|
-
cwd: dir,
|
|
137
|
-
toolInput: { ...toolInput, file_path: toolInput.file_path, content: toolInput.content },
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const transcriptField = event === "SubagentStop" ? "agent_transcript_path" : "transcript_path";
|
|
142
|
-
|
|
143
|
-
const transcriptPath = transcriptField in input ? input[transcriptField] : undefined;
|
|
144
|
-
if (transcriptPath != null && typeof transcriptPath !== "string")
|
|
145
|
-
return { error: `${transcriptField} must be a string` };
|
|
146
|
-
|
|
147
|
-
const stopHookActive = "stop_hook_active" in input ? input.stop_hook_active : undefined;
|
|
148
|
-
if (stopHookActive != null && typeof stopHookActive !== "boolean")
|
|
149
|
-
return { error: "stop_hook_active must be a boolean" };
|
|
150
|
-
|
|
151
|
-
const stop = {
|
|
152
|
-
event: "stop" as const,
|
|
153
|
-
cwd: dir,
|
|
154
|
-
stopHookActive: stopHookActive ?? false,
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const path = transcriptPath == null ? undefined : resolve(fallbackCwd, transcriptPath);
|
|
158
|
-
return event === "SubagentStop"
|
|
159
|
-
? { ...stop, agentTranscriptPath: path }
|
|
160
|
-
: { ...stop, transcriptPath: path };
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const events = new Set(["Stop", "SubagentStop", "PreToolUse", "PostToolUse"]);
|
|
164
|
-
const patchHeader = /^\*\*\* (Add File|Update File|Move to): (.+)$/;
|
|
165
|
-
const patchApplied = /^Success\. Updated the following files:$/m;
|
|
166
|
-
function editedPaths(input: Record<string, unknown>): string[] {
|
|
167
|
-
const toolInput = input.tool_input;
|
|
168
|
-
if (!isRecord(toolInput)) return [];
|
|
169
|
-
if (input.tool_name === "Edit" || input.tool_name === "MultiEdit")
|
|
170
|
-
return typeof toolInput.file_path === "string" ? [toolInput.file_path] : [];
|
|
171
|
-
|
|
172
|
-
if (input.tool_name !== "apply_patch" || typeof toolInput.command !== "string") return [];
|
|
173
|
-
if (typeof input.tool_response !== "string" || !patchApplied.test(input.tool_response)) return [];
|
|
174
|
-
|
|
175
|
-
const paths: string[] = [];
|
|
176
|
-
for (const line of toolInput.command.split(/\r?\n/)) {
|
|
177
|
-
const [, kind, path] = patchHeader.exec(line) ?? [];
|
|
178
|
-
if (path === undefined) continue;
|
|
179
|
-
if (kind === "Move to") paths.pop();
|
|
180
|
-
paths.push(path);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return paths;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
187
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const toolUseLine = /"type"\s*:\s*"tool_use"/;
|
|
191
|
-
const failedResultLine = /"is_error"\s*:\s*true/;
|
|
192
|
-
const itemCompletedLine = /"item_completed"/;
|
|
193
|
-
|
|
194
|
-
export function writtenFiles(transcriptPath: string): Set<string> | undefined {
|
|
195
|
-
let text: string;
|
|
196
|
-
try {
|
|
197
|
-
text = readFileSync(transcriptPath, "utf8");
|
|
198
|
-
} catch {
|
|
199
|
-
return undefined;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const codex = writtenFilesInCodexRollout(text);
|
|
203
|
-
if (codex !== undefined) return codex.size > 0 ? codex : undefined;
|
|
204
|
-
|
|
205
|
-
const main = writtenFilesInTranscript(text);
|
|
206
|
-
if (main === undefined) return undefined;
|
|
207
|
-
|
|
208
|
-
const written = new Set(main);
|
|
209
|
-
const subagents = join(dirname(transcriptPath), basename(transcriptPath, ".jsonl"), "subagents");
|
|
210
|
-
|
|
211
|
-
let names: string[];
|
|
212
|
-
try {
|
|
213
|
-
names = readdirSync(subagents);
|
|
214
|
-
} catch {
|
|
215
|
-
return written;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
for (const name of names) {
|
|
219
|
-
if (!name.endsWith(".jsonl")) continue;
|
|
220
|
-
|
|
221
|
-
let text: string;
|
|
222
|
-
try {
|
|
223
|
-
text = readFileSync(join(subagents, name), "utf8");
|
|
224
|
-
} catch {
|
|
225
|
-
continue;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const subagent = writtenFilesInTranscript(text);
|
|
229
|
-
if (subagent === undefined) continue;
|
|
230
|
-
|
|
231
|
-
for (const path of subagent) written.add(path);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return written;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function writtenFilesInCodexRollout(text: string): Set<string> | undefined {
|
|
238
|
-
const written = new Set<string>();
|
|
239
|
-
|
|
240
|
-
let recognized = false;
|
|
241
|
-
for (const line of text.split("\n")) {
|
|
242
|
-
if (!itemCompletedLine.test(line)) continue;
|
|
243
|
-
|
|
244
|
-
let record: unknown;
|
|
245
|
-
try {
|
|
246
|
-
record = JSON.parse(line);
|
|
247
|
-
} catch {
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
if (!isRecord(record) || record.type !== "event_msg" || !isRecord(record.payload)) continue;
|
|
252
|
-
if (record.payload.type !== "item_completed") continue;
|
|
253
|
-
|
|
254
|
-
recognized = true;
|
|
255
|
-
|
|
256
|
-
const item = record.payload.item;
|
|
257
|
-
if (!isRecord(item) || item.type !== "FileChange" || item.status !== "completed") continue;
|
|
258
|
-
if (!isRecord(item.changes)) continue;
|
|
259
|
-
|
|
260
|
-
for (const [source, change] of Object.entries(item.changes)) {
|
|
261
|
-
if (!isRecord(change) || change.type === "delete") continue;
|
|
262
|
-
|
|
263
|
-
const path = typeof change.move_path === "string" ? change.move_path : source;
|
|
264
|
-
if (!isAbsolute(path)) continue;
|
|
265
|
-
|
|
266
|
-
written.add(path);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return recognized ? written : undefined;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function writtenFilesInTranscript(text: string): Set<string> | undefined {
|
|
274
|
-
const attempted = new Map<unknown, string>();
|
|
275
|
-
const failed = new Set<unknown>();
|
|
276
|
-
|
|
277
|
-
let recognized = false;
|
|
278
|
-
for (const line of text.split("\n")) {
|
|
279
|
-
if (recognized && !toolUseLine.test(line) && !failedResultLine.test(line)) continue;
|
|
280
|
-
|
|
281
|
-
let record: unknown;
|
|
282
|
-
try {
|
|
283
|
-
record = JSON.parse(line);
|
|
284
|
-
} catch {
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
if (!isRecord(record) || !isRecord(record.message)) continue;
|
|
289
|
-
|
|
290
|
-
const content = record.message.content;
|
|
291
|
-
if (!Array.isArray(content)) continue;
|
|
292
|
-
if (record.type === "assistant") recognized = true;
|
|
293
|
-
|
|
294
|
-
for (const item of content) {
|
|
295
|
-
if (!isRecord(item)) continue;
|
|
296
|
-
|
|
297
|
-
const path = editedPath(item);
|
|
298
|
-
if (record.type === "assistant" && path !== undefined) attempted.set(item.id, path);
|
|
299
|
-
if (record.type === "user" && item.type === "tool_result" && item.is_error === true)
|
|
300
|
-
failed.add(item.tool_use_id);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
if (!recognized) return undefined;
|
|
305
|
-
|
|
306
|
-
const written = new Set<string>();
|
|
307
|
-
for (const [id, path] of attempted) if (!failed.has(id)) written.add(path);
|
|
308
|
-
|
|
309
|
-
return written;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
const editTools = new Set(["Write", "Edit", "MultiEdit"]);
|
|
313
|
-
function editedPath(item: Record<string, unknown>): string | undefined {
|
|
314
|
-
if (item.type !== "tool_use" || !editTools.has(String(item.name))) return undefined;
|
|
315
|
-
if (!isRecord(item.input) || typeof item.input.file_path !== "string") return undefined;
|
|
316
|
-
return isAbsolute(item.input.file_path) ? item.input.file_path : undefined;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
const failures: Partial<Record<Finding["rule"], string>> = {
|
|
320
|
-
parse: "could not read or parse this file",
|
|
321
|
-
error: "stanza failed on this file",
|
|
322
|
-
write: "could not write the fixes to this file",
|
|
323
|
-
};
|
|
324
|
-
|
|
325
|
-
export function blockReason(findings: Finding[], rewritten: string[]): string | undefined {
|
|
326
|
-
if (findings.length === 0) return undefined;
|
|
327
|
-
|
|
328
|
-
const { RULES } = require("./engine/rules.ts") as typeof import("./engine/rules.ts");
|
|
329
|
-
|
|
330
|
-
const shown = findings.slice(0, 12);
|
|
331
|
-
const lines = shown.map((finding) => {
|
|
332
|
-
const failure = failures[finding.rule];
|
|
333
|
-
const message = failure
|
|
334
|
-
? `${failure}: ${finding.message}`
|
|
335
|
-
: `${finding.rule} ${finding.message}`;
|
|
336
|
-
|
|
337
|
-
return ` ${finding.path}:${finding.line}:${finding.col} ${message}`;
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
if (findings.length > shown.length)
|
|
341
|
-
lines.push(` ... and ${findings.length - shown.length} more`);
|
|
342
|
-
|
|
343
|
-
const present = new Set<string>(findings.map((finding) => finding.rule));
|
|
344
|
-
const rules = Object.entries(RULES)
|
|
345
|
-
.filter(([id]) => present.has(id))
|
|
346
|
-
.map(([id, rule]) => `${id}: ${rule.summary}`);
|
|
347
|
-
|
|
348
|
-
const sections = [["stanza could not fix these in the files you changed:", ...lines].join("\n")];
|
|
349
|
-
if (rules.length > 0) sections.push(rules.join("\n"));
|
|
350
|
-
|
|
351
|
-
sections.push("Fix those findings, then reply again.");
|
|
352
|
-
|
|
353
|
-
const paths = new Set(findings.map((finding) => finding.path));
|
|
354
|
-
const reread = rewritten.filter((path) => paths.has(path));
|
|
355
|
-
if (reread.length > 0) sections.push(rereadLine("rewrote", reread));
|
|
356
|
-
|
|
357
|
-
return sections.join("\n\n");
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
export function rereadLine(verb: string, paths: string[]): string {
|
|
361
|
-
const names =
|
|
362
|
-
paths.length === 1 ? paths[0] : `${paths.slice(0, -1).join(", ")} and ${paths.at(-1)}`;
|
|
363
|
-
|
|
364
|
-
return `stanza ${verb} ${names}, so read ${paths.length === 1 ? "it" : "them"} again before editing.`;
|
|
365
|
-
}
|
package/src/languages/index.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { basename, extname } from "node:path";
|
|
2
|
-
import { entry as javascript } from "./javascript/entry.ts";
|
|
3
|
-
import type { Entry, Language } from "./language.ts";
|
|
4
|
-
|
|
5
|
-
const entries: Entry[] = [javascript];
|
|
6
|
-
const byExtension = new Map(
|
|
7
|
-
entries.flatMap((entry) => entry.extensions.map((extension) => [extension, entry] as const)),
|
|
8
|
-
);
|
|
9
|
-
|
|
10
|
-
export const extensions = [...byExtension.keys()];
|
|
11
|
-
|
|
12
|
-
export function entryFor(path: string): Entry | undefined {
|
|
13
|
-
return byExtension.get(extname(path));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function languageOf(path: string): Language {
|
|
17
|
-
const entry = entryFor(path);
|
|
18
|
-
if (!entry) throw new Error(`no language is registered for ${path}`);
|
|
19
|
-
return entry.load();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function isCandidate(relativePath: string, keepGenerated = false): boolean {
|
|
23
|
-
const entry = entryFor(relativePath);
|
|
24
|
-
return (
|
|
25
|
-
entry !== undefined &&
|
|
26
|
-
!entry.skipsName(basename(relativePath), keepGenerated) &&
|
|
27
|
-
!relativePath.split(/[\\/]+/).some((segment) => entry.directories.has(segment))
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function prunes(relativeDirectory: string): boolean {
|
|
32
|
-
const segments = relativeDirectory.split(/[\\/]+/);
|
|
33
|
-
return entries.every((entry) => segments.some((segment) => entry.directories.has(segment)));
|
|
34
|
-
}
|
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
import { visitorKeys, type Node } from "oxc-parser";
|
|
2
|
-
import type { Path } from "../../engine/model.ts";
|
|
3
|
-
|
|
4
|
-
function isNode(value: unknown): value is Node {
|
|
5
|
-
return (
|
|
6
|
-
typeof value === "object" &&
|
|
7
|
-
value !== null &&
|
|
8
|
-
"type" in value &&
|
|
9
|
-
typeof value.type === "string" &&
|
|
10
|
-
"start" in value &&
|
|
11
|
-
typeof value.start === "number" &&
|
|
12
|
-
"end" in value &&
|
|
13
|
-
typeof value.end === "number"
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function eachChild(node: Node, each: (key: string, child: Node) => void): void {
|
|
18
|
-
const keys = visitorKeys[node.type];
|
|
19
|
-
if (keys) {
|
|
20
|
-
const fields = node as unknown as Record<string, unknown>;
|
|
21
|
-
for (const key of keys) visitValue(key, fields[key], each);
|
|
22
|
-
} else
|
|
23
|
-
for (const [key, value] of Object.entries(node))
|
|
24
|
-
if (key !== "parent") visitValue(key, value, each);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function visitValue(key: string, value: unknown, each: (key: string, child: Node) => void): void {
|
|
28
|
-
if (Array.isArray(value)) {
|
|
29
|
-
for (const child of value) if (isNode(child)) each(key, child);
|
|
30
|
-
} else if (isNode(value)) each(key, value);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function children(node: Node): [string, Node][] {
|
|
34
|
-
const result: [string, Node][] = [];
|
|
35
|
-
eachChild(node, (key, child) => result.push([key, child]));
|
|
36
|
-
return result;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function pushValueReverse(
|
|
40
|
-
value: unknown,
|
|
41
|
-
parent: Node,
|
|
42
|
-
nodes: Node[],
|
|
43
|
-
parents: (Node | null)[],
|
|
44
|
-
): void {
|
|
45
|
-
if (Array.isArray(value)) {
|
|
46
|
-
for (let index = value.length - 1; index >= 0; index--) {
|
|
47
|
-
const child: unknown = value[index];
|
|
48
|
-
if (!isNode(child)) continue;
|
|
49
|
-
|
|
50
|
-
nodes.push(child);
|
|
51
|
-
parents.push(parent);
|
|
52
|
-
}
|
|
53
|
-
} else if (isNode(value)) {
|
|
54
|
-
nodes.push(value);
|
|
55
|
-
parents.push(parent);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function pushChildrenReverse(node: Node, nodes: Node[], parents: (Node | null)[]): void {
|
|
60
|
-
const keys = visitorKeys[node.type];
|
|
61
|
-
if (keys) {
|
|
62
|
-
const fields = node as unknown as Record<string, unknown>;
|
|
63
|
-
for (let index = keys.length - 1; index >= 0; index--)
|
|
64
|
-
pushValueReverse(fields[keys[index]!], node, nodes, parents);
|
|
65
|
-
} else {
|
|
66
|
-
const entries = Object.entries(node);
|
|
67
|
-
for (let index = entries.length - 1; index >= 0; index--) {
|
|
68
|
-
const [key, value] = entries[index]!;
|
|
69
|
-
if (key === "parent") continue;
|
|
70
|
-
pushValueReverse(value, node, nodes, parents);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function walk(
|
|
76
|
-
root: Node,
|
|
77
|
-
enter: (node: Node, parent: Node | null) => void,
|
|
78
|
-
leave?: (node: Node, parent: Node | null) => void,
|
|
79
|
-
): void {
|
|
80
|
-
const nodes: Node[] = [root];
|
|
81
|
-
const parents: (Node | null)[] = [null];
|
|
82
|
-
if (!leave) {
|
|
83
|
-
while (nodes.length > 0) {
|
|
84
|
-
const node = nodes.pop()!;
|
|
85
|
-
const parent = parents.pop()!;
|
|
86
|
-
enter(node, parent);
|
|
87
|
-
pushChildrenReverse(node, nodes, parents);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const leaveIndexes: number[] = [];
|
|
94
|
-
while (nodes.length > 0) {
|
|
95
|
-
const node = nodes.pop()!;
|
|
96
|
-
const parent = parents.pop()!;
|
|
97
|
-
if (leaveIndexes.at(-1) === nodes.length) {
|
|
98
|
-
leaveIndexes.pop();
|
|
99
|
-
leave(node, parent);
|
|
100
|
-
continue;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
enter(node, parent);
|
|
104
|
-
leaveIndexes.push(nodes.length);
|
|
105
|
-
nodes.push(node);
|
|
106
|
-
parents.push(parent);
|
|
107
|
-
|
|
108
|
-
pushChildrenReverse(node, nodes, parents);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function boundNames(root: Node): Set<string> {
|
|
113
|
-
const names = new Set<string>();
|
|
114
|
-
const stack = [root];
|
|
115
|
-
while (stack.length > 0) {
|
|
116
|
-
const node = stack.pop()!;
|
|
117
|
-
switch (node.type) {
|
|
118
|
-
case "Identifier":
|
|
119
|
-
names.add(node.name);
|
|
120
|
-
break;
|
|
121
|
-
|
|
122
|
-
case "VariableDeclaration":
|
|
123
|
-
for (const declaration of node.declarations.toReversed()) stack.push(declaration.id);
|
|
124
|
-
break;
|
|
125
|
-
|
|
126
|
-
case "ObjectPattern":
|
|
127
|
-
for (const property of node.properties.toReversed()) stack.push(property);
|
|
128
|
-
break;
|
|
129
|
-
|
|
130
|
-
case "ArrayPattern":
|
|
131
|
-
for (const element of node.elements.toReversed()) if (element) stack.push(element);
|
|
132
|
-
break;
|
|
133
|
-
|
|
134
|
-
case "Property":
|
|
135
|
-
stack.push(node.value);
|
|
136
|
-
break;
|
|
137
|
-
|
|
138
|
-
case "AssignmentPattern":
|
|
139
|
-
stack.push(node.left);
|
|
140
|
-
break;
|
|
141
|
-
|
|
142
|
-
case "RestElement":
|
|
143
|
-
stack.push(node.argument);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return names;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function referenceChild(node: Node, key: string): boolean {
|
|
151
|
-
if (["id", "label", "typeAnnotation", "typeParameters", "returnType"].includes(key)) return false;
|
|
152
|
-
if (node.type.startsWith("TS")) return key === "expression";
|
|
153
|
-
if (key === "key" || (node.type === "MemberExpression" && key === "property"))
|
|
154
|
-
return "computed" in node && node.computed === true;
|
|
155
|
-
|
|
156
|
-
return true;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function lexicalNames(node: Node): string[] {
|
|
160
|
-
if (node.type === "VariableDeclaration") return node.kind === "var" ? [] : [...boundNames(node)];
|
|
161
|
-
if ((node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") && node.id)
|
|
162
|
-
return [node.id.name];
|
|
163
|
-
|
|
164
|
-
return [];
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export function declared(node: Node): string[] {
|
|
168
|
-
switch (node.type) {
|
|
169
|
-
case "ForStatement":
|
|
170
|
-
return node.init ? lexicalNames(node.init) : [];
|
|
171
|
-
|
|
172
|
-
case "ForInStatement":
|
|
173
|
-
case "ForOfStatement":
|
|
174
|
-
return lexicalNames(node.left);
|
|
175
|
-
|
|
176
|
-
case "BlockStatement":
|
|
177
|
-
case "StaticBlock":
|
|
178
|
-
return node.body.flatMap(lexicalNames);
|
|
179
|
-
|
|
180
|
-
case "SwitchStatement":
|
|
181
|
-
return node.cases.flatMap((clause) => clause.consequent.flatMap(lexicalNames));
|
|
182
|
-
|
|
183
|
-
case "CatchClause":
|
|
184
|
-
return node.param ? [...boundNames(node.param)] : [];
|
|
185
|
-
|
|
186
|
-
case "FunctionDeclaration":
|
|
187
|
-
return node.params.flatMap((param) => [...boundNames(param)]);
|
|
188
|
-
|
|
189
|
-
default:
|
|
190
|
-
return [];
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export function jsxElementPath(node: Node): Path | undefined {
|
|
195
|
-
if (node.type === "JSXIdentifier") return /^[a-z]|-/.test(node.name) ? undefined : [node.name];
|
|
196
|
-
if (node.type !== "JSXMemberExpression") return undefined;
|
|
197
|
-
|
|
198
|
-
const segments = [node.property.name];
|
|
199
|
-
|
|
200
|
-
let head = node.object;
|
|
201
|
-
while (head.type === "JSXMemberExpression") {
|
|
202
|
-
segments.push(head.property.name);
|
|
203
|
-
head = head.object;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return [head.name, ...segments.reverse()];
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export function* references(root: Node, names: Set<string>): Generator<string> {
|
|
210
|
-
const stack = [{ node: root, names, binding: false }];
|
|
211
|
-
while (stack.length > 0) {
|
|
212
|
-
const { node, names, binding } = stack.pop()!;
|
|
213
|
-
if (node.type === "Identifier") {
|
|
214
|
-
if (!binding && names.has(node.name)) yield node.name;
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") continue;
|
|
219
|
-
if (node.type === "FunctionDeclaration" && node !== root) continue;
|
|
220
|
-
|
|
221
|
-
const shadowed = declared(node);
|
|
222
|
-
const visible = shadowed.some((name) => names.has(name))
|
|
223
|
-
? names.difference(new Set(shadowed))
|
|
224
|
-
: names;
|
|
225
|
-
|
|
226
|
-
if (visible.size === 0) continue;
|
|
227
|
-
|
|
228
|
-
if (node.type === "JSXOpeningElement") {
|
|
229
|
-
const path = jsxElementPath(node.name);
|
|
230
|
-
if (!binding && path && visible.has(path[0])) yield path[0];
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const next = children(node);
|
|
234
|
-
for (let index = next.length - 1; index >= 0; index--) {
|
|
235
|
-
const [key, child] = next[index]!;
|
|
236
|
-
if (!referenceChild(node, key)) continue;
|
|
237
|
-
|
|
238
|
-
const pattern =
|
|
239
|
-
key === "params" ||
|
|
240
|
-
(binding && key !== "right" && key !== "key") ||
|
|
241
|
-
(node.type === "CatchClause" && key === "param");
|
|
242
|
-
|
|
243
|
-
stack.push({ node: child, names: visible, binding: pattern });
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
export const LOOP_TYPES = new Set([
|
|
249
|
-
"ForStatement",
|
|
250
|
-
"ForInStatement",
|
|
251
|
-
"ForOfStatement",
|
|
252
|
-
"WhileStatement",
|
|
253
|
-
"DoWhileStatement",
|
|
254
|
-
]);
|
|
255
|
-
|
|
256
|
-
export const JUMP_TYPES = new Set([
|
|
257
|
-
"ReturnStatement",
|
|
258
|
-
"ThrowStatement",
|
|
259
|
-
"ContinueStatement",
|
|
260
|
-
"BreakStatement",
|
|
261
|
-
]);
|