@ryuugg/stanza 0.3.1 → 0.3.2
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 +2 -2
- package/src/cli.ts +35 -142
- package/src/compile/darwin-arm64.ts +1 -1
- package/src/compile/darwin-x64.ts +1 -1
- package/src/compile/linux-arm64-musl.ts +1 -1
- package/src/compile/linux-arm64.ts +1 -1
- package/src/compile/linux-x64-musl.ts +1 -1
- package/src/compile/linux-x64.ts +1 -1
- package/src/depth.ts +368 -0
- package/src/directories.ts +77 -0
- package/src/engine/gaps.ts +26 -2
- package/src/files.ts +176 -22
- package/src/format.ts +159 -0
- package/src/hook.ts +32 -6
- package/src/languages/javascript/config/find.ts +7 -10
- package/src/languages/javascript/config/index.ts +1 -1
- package/src/languages/javascript/config/module.ts +3 -2
- package/src/languages/javascript/config/width.ts +32 -26
- package/src/pool.ts +312 -0
- package/src/usage.ts +5 -2
- package/src/worker.ts +4 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { existsSync, readdirSync, realpathSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join } from "node:path";
|
|
3
|
+
|
|
4
|
+
type Listing = { names: Set<string>; folded: Set<string> } | null;
|
|
5
|
+
|
|
6
|
+
const maps: Map<unknown, unknown>[] = [];
|
|
7
|
+
export function invocationMap<K, V>(): Map<K, V> {
|
|
8
|
+
const map = new Map<K, V>();
|
|
9
|
+
maps.push(map);
|
|
10
|
+
return map;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function beginInvocation(): void {
|
|
14
|
+
for (const map of maps) map.clear();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const realDirectories = invocationMap<string, string>();
|
|
18
|
+
const listings = invocationMap<string, Listing>();
|
|
19
|
+
const existence = invocationMap<string, boolean>();
|
|
20
|
+
const realPaths = invocationMap<string, string>();
|
|
21
|
+
|
|
22
|
+
export function realDirectory(dir: string): string {
|
|
23
|
+
const cached = realDirectories.get(dir);
|
|
24
|
+
if (cached !== undefined) return cached;
|
|
25
|
+
|
|
26
|
+
const parent = dirname(dir);
|
|
27
|
+
const real = existsSync(dir)
|
|
28
|
+
? realpathSync(dir)
|
|
29
|
+
: parent === dir
|
|
30
|
+
? dir
|
|
31
|
+
: join(realDirectory(parent), basename(dir));
|
|
32
|
+
|
|
33
|
+
realDirectories.set(dir, real);
|
|
34
|
+
realDirectories.set(real, real);
|
|
35
|
+
return real;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function listing(dir: string): Listing {
|
|
39
|
+
const cached = listings.get(dir);
|
|
40
|
+
if (cached !== undefined) return cached;
|
|
41
|
+
|
|
42
|
+
let result: Listing;
|
|
43
|
+
try {
|
|
44
|
+
const names = readdirSync(dir);
|
|
45
|
+
result = { names: new Set(names), folded: new Set(names.map((name) => name.toLowerCase())) };
|
|
46
|
+
} catch {
|
|
47
|
+
result = null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
listings.set(dir, result);
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function holds(dir: string, name: string): boolean {
|
|
55
|
+
const path = join(dir, name);
|
|
56
|
+
const cached = existence.get(path);
|
|
57
|
+
if (cached !== undefined) return cached;
|
|
58
|
+
|
|
59
|
+
const entries = listing(dir);
|
|
60
|
+
const exists = (entries === null || entries.folded.has(name.toLowerCase())) && existsSync(path);
|
|
61
|
+
|
|
62
|
+
existence.set(path, exists);
|
|
63
|
+
return exists;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function listed(dir: string, name: string): boolean {
|
|
67
|
+
return listing(dir)?.names.has(name) ?? false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function realPath(path: string): string {
|
|
71
|
+
const cached = realPaths.get(path);
|
|
72
|
+
if (cached !== undefined) return cached;
|
|
73
|
+
|
|
74
|
+
const real = realpathSync(path);
|
|
75
|
+
realPaths.set(path, real);
|
|
76
|
+
return real;
|
|
77
|
+
}
|
package/src/engine/gaps.ts
CHANGED
|
@@ -261,6 +261,30 @@ function walls(
|
|
|
261
261
|
const findings: Finding[] = [];
|
|
262
262
|
if (list.kind === "switch") return findings;
|
|
263
263
|
|
|
264
|
+
const excluded = new Set<number>();
|
|
265
|
+
|
|
266
|
+
let groupStart = 0;
|
|
267
|
+
for (let index = 0; index <= list.stmts.length; index++) {
|
|
268
|
+
const stmt = list.stmts[index];
|
|
269
|
+
const gap = gaps[index - 1];
|
|
270
|
+
const counted = stmt && !stmt.multiline && stmt.kind !== "import";
|
|
271
|
+
const forcedJoined =
|
|
272
|
+
gap &&
|
|
273
|
+
gap.decision.want === "none" &&
|
|
274
|
+
!gap.next.detached &&
|
|
275
|
+
(gap.blank === 0 || touches(gap.prev.startLine, gap.next.endLine));
|
|
276
|
+
|
|
277
|
+
if (!counted || !forcedJoined) {
|
|
278
|
+
if (index - groupStart >= 6)
|
|
279
|
+
for (let forcedIndex = groupStart; forcedIndex < index; forcedIndex++)
|
|
280
|
+
excluded.add(forcedIndex);
|
|
281
|
+
|
|
282
|
+
groupStart = index;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (!counted) groupStart = index + 1;
|
|
286
|
+
}
|
|
287
|
+
|
|
264
288
|
let runStart: Stmt | undefined;
|
|
265
289
|
let length = 0;
|
|
266
290
|
for (let index = 0; index <= list.stmts.length; index++) {
|
|
@@ -268,13 +292,13 @@ function walls(
|
|
|
268
292
|
const gap = gaps[index - 1];
|
|
269
293
|
const separated =
|
|
270
294
|
gap &&
|
|
271
|
-
(touches(gap.prev.startLine, gap.next.endLine)
|
|
295
|
+
(!gap.next.detached && touches(gap.prev.startLine, gap.next.endLine)
|
|
272
296
|
? gap.decision.want === "frozen" ||
|
|
273
297
|
gap.decision.want === "at-least-one" ||
|
|
274
298
|
(gap.decision.want === "keep" && gap.blank > 0)
|
|
275
299
|
: gap.decision.want === "frozen" || gap.blank > 0);
|
|
276
300
|
|
|
277
|
-
const counted = stmt && !stmt.multiline && stmt.kind !== "import";
|
|
301
|
+
const counted = stmt && !stmt.multiline && stmt.kind !== "import" && !excluded.has(index);
|
|
278
302
|
if (!counted || separated) {
|
|
279
303
|
if (runStart && length >= 6 && touches(runStart.startLine, list.stmts[index - 1]!.endLine))
|
|
280
304
|
findings.push(finding(doc, runStart.start, "wall"));
|
package/src/files.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
accessSync,
|
|
3
|
+
closeSync,
|
|
4
|
+
constants,
|
|
5
|
+
existsSync,
|
|
6
|
+
lstatSync,
|
|
7
|
+
openSync,
|
|
8
|
+
readdirSync,
|
|
9
|
+
readFileSync,
|
|
10
|
+
readSync,
|
|
11
|
+
realpathSync,
|
|
12
|
+
statSync,
|
|
13
|
+
type Stats,
|
|
14
|
+
} from "node:fs";
|
|
2
15
|
import { basename, delimiter, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
16
|
+
import { invocationMap, listed, realDirectory } from "./directories.ts";
|
|
3
17
|
import { entryFor, isCandidate, prunes } from "./languages/index.ts";
|
|
4
18
|
import { ignoredByRules, parseIgnore, type Rule } from "./gitignore.ts";
|
|
5
19
|
import { diffChanges } from "./engine/hunks.ts";
|
|
@@ -275,9 +289,105 @@ function hasGitMarker(dir: string): boolean {
|
|
|
275
289
|
}
|
|
276
290
|
}
|
|
277
291
|
|
|
278
|
-
|
|
292
|
+
const locations = invocationMap<string, Location>();
|
|
293
|
+
const repositoryConfigs = invocationMap<string, boolean>();
|
|
294
|
+
|
|
295
|
+
const discoveryEnvironment = [
|
|
296
|
+
"GIT_DIR",
|
|
297
|
+
"GIT_WORK_TREE",
|
|
298
|
+
"GIT_COMMON_DIR",
|
|
299
|
+
"GIT_OBJECT_DIRECTORY",
|
|
300
|
+
"GIT_CEILING_DIRECTORIES",
|
|
301
|
+
"GIT_DISCOVERY_ACROSS_FILESYSTEM",
|
|
302
|
+
"GIT_TEST_ASSUME_DIFFERENT_OWNER",
|
|
303
|
+
];
|
|
304
|
+
|
|
305
|
+
function plainConfig(root: string): boolean {
|
|
306
|
+
const cached = repositoryConfigs.get(root);
|
|
307
|
+
if (cached !== undefined) return cached;
|
|
308
|
+
|
|
309
|
+
const result = Bun.spawnSync(["git", "-C", root, "var", "-l"], {
|
|
310
|
+
env: process.env,
|
|
311
|
+
stderr: "pipe",
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const config = new TextDecoder().decode(result.stdout);
|
|
315
|
+
const plain =
|
|
316
|
+
result.exitCode === 0 &&
|
|
317
|
+
result.stderr.length === 0 &&
|
|
318
|
+
!/^(?:core\.worktree|extensions\.[^=]*|include(?:if\.[^=]*)?\.path)(?:=|$)/im.test(config) &&
|
|
319
|
+
!/^core\.bare(?:=(?!(?:false|no|off|0)$)|$)/im.test(config) &&
|
|
320
|
+
!/^core\.repositoryformatversion(?:=(?![01]$)|$)/im.test(config);
|
|
321
|
+
|
|
322
|
+
repositoryConfigs.set(root, plain);
|
|
323
|
+
return plain;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function plainRepository(worktree: string, marker: string, markerStat: Stats): boolean {
|
|
327
|
+
if (lstatSync(worktree).uid !== process.geteuid!() || markerStat.uid !== process.geteuid!())
|
|
328
|
+
return false;
|
|
329
|
+
|
|
330
|
+
const head = join(marker, "HEAD");
|
|
331
|
+
if (!lstatSync(head).isFile()) return false;
|
|
332
|
+
|
|
333
|
+
const descriptor = openSync(head, "r");
|
|
334
|
+
const bytes = Buffer.alloc(255);
|
|
335
|
+
|
|
336
|
+
let text: string;
|
|
337
|
+
try {
|
|
338
|
+
text = bytes.subarray(0, readSync(descriptor, bytes, 0, bytes.length, 0)).toString("latin1");
|
|
339
|
+
} finally {
|
|
340
|
+
closeSync(descriptor);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (!/^(?:ref: refs\/|[0-9a-f]{40})/.test(text)) return false;
|
|
344
|
+
|
|
345
|
+
accessSync(join(marker, "objects"), constants.X_OK);
|
|
346
|
+
accessSync(join(marker, "refs"), constants.X_OK);
|
|
347
|
+
if (lstatSync(join(marker, "commondir"), { throwIfNoEntry: false }) !== undefined) return false;
|
|
348
|
+
|
|
349
|
+
return plainConfig(worktree);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function knownRoot(dir: string): string | undefined {
|
|
353
|
+
try {
|
|
354
|
+
if (discoveryEnvironment.some((name) => process.env[name] !== undefined)) return undefined;
|
|
355
|
+
if (Object.keys(process.env).some((name) => name.startsWith("GIT_CONFIG"))) return undefined;
|
|
356
|
+
if (typeof process.geteuid !== "function") return undefined;
|
|
357
|
+
|
|
358
|
+
const start = realpathSync(dir);
|
|
359
|
+
const startStat = statSync(start);
|
|
360
|
+
if (!startStat.isDirectory() || /[^\x20-\x7e]/.test(start) || start.split(sep).includes(".git"))
|
|
361
|
+
return undefined;
|
|
362
|
+
|
|
363
|
+
const device = startStat.dev;
|
|
364
|
+
|
|
365
|
+
let current = start;
|
|
366
|
+
while (true) {
|
|
367
|
+
const marker = join(current, ".git");
|
|
368
|
+
const entry = lstatSync(marker, { throwIfNoEntry: false });
|
|
369
|
+
if (entry !== undefined)
|
|
370
|
+
return entry.isDirectory() && plainRepository(current, marker, entry) ? current : undefined;
|
|
371
|
+
|
|
372
|
+
if (lstatSync(join(current, "HEAD"), { throwIfNoEntry: false }) !== undefined)
|
|
373
|
+
return undefined;
|
|
374
|
+
|
|
375
|
+
const parent = dirname(current);
|
|
376
|
+
if (parent === current || statSync(parent).dev !== device) return undefined;
|
|
377
|
+
|
|
378
|
+
current = parent;
|
|
379
|
+
}
|
|
380
|
+
} catch {
|
|
381
|
+
return undefined;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function discoverLocation(dir: string): Location {
|
|
279
386
|
if (!hasGit()) return { kind: "outside" };
|
|
280
387
|
|
|
388
|
+
const known = knownRoot(dir);
|
|
389
|
+
if (known !== undefined) return { kind: "repository", root: known.trim() };
|
|
390
|
+
|
|
281
391
|
const result = runGit(dir, ["rev-parse", "--show-toplevel"]);
|
|
282
392
|
const root = result.ok ? result.output.trim() : "";
|
|
283
393
|
if (root) return { kind: "repository", root };
|
|
@@ -285,6 +395,15 @@ export function locate(dir: string): Location {
|
|
|
285
395
|
return { kind: "outside" };
|
|
286
396
|
}
|
|
287
397
|
|
|
398
|
+
export function locate(dir: string): Location {
|
|
399
|
+
const known = locations.get(dir);
|
|
400
|
+
if (known !== undefined) return known;
|
|
401
|
+
|
|
402
|
+
const location = discoverLocation(dir);
|
|
403
|
+
locations.set(dir, location);
|
|
404
|
+
return location;
|
|
405
|
+
}
|
|
406
|
+
|
|
288
407
|
function repositoryPath(root: string, file: string): string {
|
|
289
408
|
return relative(root, file).split(sep).join("/");
|
|
290
409
|
}
|
|
@@ -381,12 +500,25 @@ function directoryFiles(dir: string, root: string, keepGenerated: boolean): Sele
|
|
|
381
500
|
const files = nulItems(result.output)
|
|
382
501
|
.filter((path) => isCandidate(path, keepGenerated))
|
|
383
502
|
.map((path) => resolve(dir, path))
|
|
384
|
-
.filter((path) =>
|
|
503
|
+
.filter((path) => {
|
|
504
|
+
try {
|
|
505
|
+
return lstatSync(path).isFile();
|
|
506
|
+
} catch {
|
|
507
|
+
return false;
|
|
508
|
+
}
|
|
509
|
+
});
|
|
385
510
|
|
|
386
|
-
const inside = files.map((path) =>
|
|
511
|
+
const inside = files.map((path) => listedFileRealpath(path)).filter((path) => within(root, path));
|
|
387
512
|
return { ok: true, files: inside };
|
|
388
513
|
}
|
|
389
514
|
|
|
515
|
+
// realpathSync returns the name in its on disk case and normalization, so only an exact listing match may join.
|
|
516
|
+
function listedFileRealpath(path: string): string {
|
|
517
|
+
const dir = dirname(path);
|
|
518
|
+
const name = basename(path);
|
|
519
|
+
return listed(dir, name) ? join(realDirectory(dir), name) : fileRealpath(path);
|
|
520
|
+
}
|
|
521
|
+
|
|
390
522
|
function fileRealpath(path: string): string {
|
|
391
523
|
try {
|
|
392
524
|
return realpathSync(path);
|
|
@@ -401,7 +533,6 @@ function sorted(files: string[]): string[] {
|
|
|
401
533
|
}
|
|
402
534
|
|
|
403
535
|
export function collectFiles(paths: string[], cwd: string, keepGenerated = false): Collected {
|
|
404
|
-
const locations = new Map<string, Location>();
|
|
405
536
|
const byRoot = new Map<string, string[]>();
|
|
406
537
|
const outside: string[] = [];
|
|
407
538
|
const errors: string[] = [];
|
|
@@ -409,15 +540,6 @@ export function collectFiles(paths: string[], cwd: string, keepGenerated = false
|
|
|
409
540
|
? []
|
|
410
541
|
: ["git not found on PATH, file selection fell back to the directory walk"];
|
|
411
542
|
|
|
412
|
-
function locationOf(dir: string): Location {
|
|
413
|
-
const known = locations.get(dir);
|
|
414
|
-
if (known) return known;
|
|
415
|
-
|
|
416
|
-
const location = locate(dir);
|
|
417
|
-
locations.set(dir, location);
|
|
418
|
-
return location;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
543
|
function add(root: string, files: string[]): void {
|
|
422
544
|
const listed = byRoot.get(root);
|
|
423
545
|
if (listed) listed.push(...files);
|
|
@@ -437,7 +559,7 @@ export function collectFiles(paths: string[], cwd: string, keepGenerated = false
|
|
|
437
559
|
} catch {}
|
|
438
560
|
|
|
439
561
|
if (directory) {
|
|
440
|
-
const location =
|
|
562
|
+
const location = locate(path);
|
|
441
563
|
if (location.kind === "failed") {
|
|
442
564
|
errors.push(location.error);
|
|
443
565
|
continue;
|
|
@@ -463,7 +585,7 @@ export function collectFiles(paths: string[], cwd: string, keepGenerated = false
|
|
|
463
585
|
}
|
|
464
586
|
|
|
465
587
|
const file = fileRealpath(path);
|
|
466
|
-
const location =
|
|
588
|
+
const location = locate(dirname(file));
|
|
467
589
|
if (location.kind === "failed") {
|
|
468
590
|
errors.push(location.error);
|
|
469
591
|
continue;
|
|
@@ -596,6 +718,7 @@ function diffLines(
|
|
|
596
718
|
return { ok: true, lines: sections.map(diffChanges) };
|
|
597
719
|
}
|
|
598
720
|
|
|
721
|
+
const pathspecBudget = 64 * 1024;
|
|
599
722
|
export function collectChanged(
|
|
600
723
|
cwd: string,
|
|
601
724
|
location: Location = locate(cwd),
|
|
@@ -618,24 +741,55 @@ export function collectChanged(
|
|
|
618
741
|
return { files: [], errors: ["not inside a git repository"], warnings: [], changedLines };
|
|
619
742
|
|
|
620
743
|
const root = location.root;
|
|
744
|
+
const realRoot = realpathSync(root);
|
|
745
|
+
const scope = [...(written ?? [])]
|
|
746
|
+
.filter((path) => within(realRoot, path))
|
|
747
|
+
.map((path) => repositoryPath(realRoot, path));
|
|
621
748
|
|
|
622
|
-
const
|
|
749
|
+
const pathspecs = Buffer.byteLength(scope.join("")) <= pathspecBudget ? scope : [];
|
|
750
|
+
|
|
751
|
+
let changed = runGit(root, [
|
|
752
|
+
"--literal-pathspecs",
|
|
753
|
+
"diff",
|
|
754
|
+
"--name-only",
|
|
755
|
+
"-z",
|
|
756
|
+
"--no-renames",
|
|
757
|
+
"--submodule=short",
|
|
758
|
+
"HEAD",
|
|
759
|
+
"--",
|
|
760
|
+
...pathspecs,
|
|
761
|
+
]);
|
|
762
|
+
|
|
763
|
+
const born = changed.ok || runGit(root, ["rev-parse", "--verify", "-q", "HEAD"]).ok;
|
|
623
764
|
if (!born) {
|
|
624
765
|
const branch = runGit(root, ["symbolic-ref", "-q", "HEAD"]);
|
|
625
766
|
if (!branch.ok) return { files: [], errors: [branch.error], warnings: [], changedLines };
|
|
767
|
+
|
|
768
|
+
changed = runGit(root, [
|
|
769
|
+
"--literal-pathspecs",
|
|
770
|
+
"ls-files",
|
|
771
|
+
"-z",
|
|
772
|
+
"--cached",
|
|
773
|
+
"--",
|
|
774
|
+
...pathspecs,
|
|
775
|
+
]);
|
|
626
776
|
}
|
|
627
777
|
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
778
|
+
const untracked = runGit(root, [
|
|
779
|
+
"--literal-pathspecs",
|
|
780
|
+
"ls-files",
|
|
781
|
+
"-z",
|
|
782
|
+
"--others",
|
|
783
|
+
"--exclude-standard",
|
|
784
|
+
"--",
|
|
785
|
+
...pathspecs,
|
|
786
|
+
]);
|
|
631
787
|
|
|
632
|
-
const untracked = runGit(root, ["ls-files", "-z", "--others", "--exclude-standard"]);
|
|
633
788
|
if (!changed.ok || !untracked.ok) {
|
|
634
789
|
const errors = [changed, untracked].flatMap((result) => (result.ok ? [] : [result.error]));
|
|
635
790
|
return { files: [], errors: [...new Set(errors)], warnings: [], changedLines };
|
|
636
791
|
}
|
|
637
792
|
|
|
638
|
-
const realRoot = realpathSync(root);
|
|
639
793
|
const kept = (name: string) => written === undefined || written.has(resolve(realRoot, name));
|
|
640
794
|
const names = nulItems(changed.output).filter(kept);
|
|
641
795
|
const candidates = names.filter((name) => isCandidate(name));
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
accessSync,
|
|
4
|
+
chmodSync,
|
|
5
|
+
constants,
|
|
6
|
+
readFileSync,
|
|
7
|
+
realpathSync,
|
|
8
|
+
renameSync,
|
|
9
|
+
type Stats,
|
|
10
|
+
statSync,
|
|
11
|
+
unlinkSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
} from "node:fs";
|
|
14
|
+
import { dirname, join } from "node:path";
|
|
15
|
+
import { errorCode } from "./files.ts";
|
|
16
|
+
import type { Changed } from "./engine/model.ts";
|
|
17
|
+
import { compareFindings, type Braces, type Finding, type Mode } from "./engine/types.ts";
|
|
18
|
+
import { decode, type Decoded, formatText } from "./step.ts";
|
|
19
|
+
|
|
20
|
+
export interface Input {
|
|
21
|
+
path: string;
|
|
22
|
+
read: () => Decoded;
|
|
23
|
+
changedLines?: Changed | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface FormatRun {
|
|
27
|
+
mode: Mode;
|
|
28
|
+
braces: Braces | undefined;
|
|
29
|
+
write: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface Warnings {
|
|
33
|
+
unread: Set<string>;
|
|
34
|
+
unreadWidth: Set<string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface FileOutcome {
|
|
38
|
+
path: string;
|
|
39
|
+
findings: Finding[];
|
|
40
|
+
failed: boolean;
|
|
41
|
+
rewritten?: string;
|
|
42
|
+
kept?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Formatted extends Warnings {
|
|
46
|
+
findings: Finding[];
|
|
47
|
+
failed: boolean;
|
|
48
|
+
rewritten: string[];
|
|
49
|
+
kept: Map<string, string>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function readText(path: string): Decoded {
|
|
53
|
+
try {
|
|
54
|
+
return decode(readFileSync(path));
|
|
55
|
+
} catch (error: unknown) {
|
|
56
|
+
return { message: String(error) };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function removeQuietly(path: string): void {
|
|
61
|
+
try {
|
|
62
|
+
unlinkSync(path);
|
|
63
|
+
} catch {}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function replaceFile(real: string, text: string, target: Stats): boolean {
|
|
67
|
+
const temp = join(dirname(real), `.stanza-${randomUUID()}.tmp`);
|
|
68
|
+
try {
|
|
69
|
+
writeFileSync(temp, text, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
70
|
+
} catch (error: unknown) {
|
|
71
|
+
removeQuietly(temp);
|
|
72
|
+
if (["EACCES", "EPERM"].includes(errorCode(error))) return false;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const created = statSync(temp);
|
|
78
|
+
if (created.uid !== target.uid || created.gid !== target.gid) {
|
|
79
|
+
removeQuietly(temp);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
chmodSync(temp, target.mode & 0o7777);
|
|
84
|
+
renameSync(temp, real);
|
|
85
|
+
return true;
|
|
86
|
+
} catch (error: unknown) {
|
|
87
|
+
removeQuietly(temp);
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function writeInPlace(real: string, text: string): void {
|
|
93
|
+
const original = readFileSync(real);
|
|
94
|
+
try {
|
|
95
|
+
writeFileSync(real, text, "utf8");
|
|
96
|
+
} catch (error: unknown) {
|
|
97
|
+
try {
|
|
98
|
+
writeFileSync(real, original);
|
|
99
|
+
} catch {}
|
|
100
|
+
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function writeFixed(path: string, text: string, output: string): Finding | undefined {
|
|
106
|
+
try {
|
|
107
|
+
const real = realpathSync(path);
|
|
108
|
+
const target = statSync(real);
|
|
109
|
+
accessSync(real, constants.W_OK);
|
|
110
|
+
|
|
111
|
+
if (target.nlink > 1 || !replaceFile(real, text, target)) writeInPlace(real, text);
|
|
112
|
+
} catch (error: unknown) {
|
|
113
|
+
return { path: output, line: 1, col: 1, rule: "write", message: String(error), fixable: false };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function formatFile(
|
|
118
|
+
input: Input,
|
|
119
|
+
run: FormatRun,
|
|
120
|
+
output: string,
|
|
121
|
+
warnings: Warnings,
|
|
122
|
+
): FileOutcome {
|
|
123
|
+
const result = formatText(input.path, input.read(), {
|
|
124
|
+
mode: run.mode,
|
|
125
|
+
braces: run.braces,
|
|
126
|
+
changedLines: input.changedLines,
|
|
127
|
+
...warnings,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const outcome: FileOutcome = {
|
|
131
|
+
path: input.path,
|
|
132
|
+
findings: result.findings.map((finding) => ({ ...finding, path: output })),
|
|
133
|
+
failed: result.parseError,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
if (result.fixed === undefined) return outcome;
|
|
137
|
+
if (!run.write) return { ...outcome, kept: result.fixed };
|
|
138
|
+
|
|
139
|
+
const unwritten = writeFixed(input.path, result.fixed, output);
|
|
140
|
+
if (unwritten) return { ...outcome, findings: [...outcome.findings, unwritten], failed: true };
|
|
141
|
+
|
|
142
|
+
return { ...outcome, rewritten: output };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function mergeOutcomes(outcomes: FileOutcome[], warnings: Warnings): Formatted {
|
|
146
|
+
const findings: Finding[] = [];
|
|
147
|
+
const rewritten: string[] = [];
|
|
148
|
+
const kept = new Map<string, string>();
|
|
149
|
+
|
|
150
|
+
let failed = false;
|
|
151
|
+
for (const outcome of outcomes) {
|
|
152
|
+
findings.push(...outcome.findings);
|
|
153
|
+
failed ||= outcome.failed;
|
|
154
|
+
if (outcome.rewritten !== undefined) rewritten.push(outcome.rewritten);
|
|
155
|
+
if (outcome.kept !== undefined) kept.set(outcome.path, outcome.kept);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return { findings: findings.sort(compareFindings), failed, rewritten, kept, ...warnings };
|
|
159
|
+
}
|
package/src/hook.ts
CHANGED
|
@@ -52,6 +52,22 @@ export function hookCall(args: string[], io: Io): HookCall | number {
|
|
|
52
52
|
if (input.event === "edit") return input.paths.some(isCandidateFile) ? input : 0;
|
|
53
53
|
if (input.stopHookActive) return 0;
|
|
54
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
|
+
|
|
55
71
|
const written =
|
|
56
72
|
input.transcriptPath === undefined ? undefined : writtenFiles(input.transcriptPath);
|
|
57
73
|
|
|
@@ -67,7 +83,11 @@ export function hookInput(
|
|
|
67
83
|
text: string,
|
|
68
84
|
fallbackCwd: string,
|
|
69
85
|
):
|
|
70
|
-
| {
|
|
86
|
+
| ({
|
|
87
|
+
event: "stop";
|
|
88
|
+
cwd: string;
|
|
89
|
+
stopHookActive: boolean;
|
|
90
|
+
} & ({ transcriptPath: string | undefined } | { agentTranscriptPath: string | undefined }))
|
|
71
91
|
| {
|
|
72
92
|
event: "write";
|
|
73
93
|
cwd: string;
|
|
@@ -118,20 +138,26 @@ export function hookInput(
|
|
|
118
138
|
};
|
|
119
139
|
}
|
|
120
140
|
|
|
121
|
-
const
|
|
141
|
+
const transcriptField = event === "SubagentStop" ? "agent_transcript_path" : "transcript_path";
|
|
142
|
+
|
|
143
|
+
const transcriptPath = transcriptField in input ? input[transcriptField] : undefined;
|
|
122
144
|
if (transcriptPath != null && typeof transcriptPath !== "string")
|
|
123
|
-
return { error:
|
|
145
|
+
return { error: `${transcriptField} must be a string` };
|
|
124
146
|
|
|
125
147
|
const stopHookActive = "stop_hook_active" in input ? input.stop_hook_active : undefined;
|
|
126
148
|
if (stopHookActive != null && typeof stopHookActive !== "boolean")
|
|
127
149
|
return { error: "stop_hook_active must be a boolean" };
|
|
128
150
|
|
|
129
|
-
|
|
130
|
-
event: "stop",
|
|
151
|
+
const stop = {
|
|
152
|
+
event: "stop" as const,
|
|
131
153
|
cwd: dir,
|
|
132
154
|
stopHookActive: stopHookActive ?? false,
|
|
133
|
-
transcriptPath: transcriptPath == null ? undefined : resolve(fallbackCwd, transcriptPath),
|
|
134
155
|
};
|
|
156
|
+
|
|
157
|
+
const path = transcriptPath == null ? undefined : resolve(fallbackCwd, transcriptPath);
|
|
158
|
+
return event === "SubagentStop"
|
|
159
|
+
? { ...stop, agentTranscriptPath: path }
|
|
160
|
+
: { ...stop, transcriptPath: path };
|
|
135
161
|
}
|
|
136
162
|
|
|
137
163
|
const events = new Set(["Stop", "SubagentStop", "PreToolUse", "PostToolUse"]);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { readFileSync, realpathSync, statSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { holds } from "../../../directories.ts";
|
|
3
4
|
|
|
4
5
|
const suffixes = ["", ".json", ".jsonc", ".js", ".cjs", ".mjs", ".ts", ".mts", ".cts", "/index.js"];
|
|
5
6
|
|
|
@@ -76,12 +77,6 @@ export function resolveModule(specifier: string, from: string, loader: Loader):
|
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
export function realDirectory(dir: string): string {
|
|
80
|
-
if (existsSync(dir)) return realpathSync(dir);
|
|
81
|
-
const parent = dirname(dir);
|
|
82
|
-
return parent === dir ? dir : join(realDirectory(parent), basename(dir));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
80
|
export function* configDirectories(
|
|
86
81
|
dir: string,
|
|
87
82
|
reader: { family: string; files: readonly string[]; candidate?(file: string): boolean },
|
|
@@ -91,9 +86,11 @@ export function* configDirectories(
|
|
|
91
86
|
|
|
92
87
|
let files = directoryFiles.get(key);
|
|
93
88
|
if (!files) {
|
|
89
|
+
const current = dir;
|
|
94
90
|
files = reader.files
|
|
95
|
-
.
|
|
96
|
-
.
|
|
91
|
+
.filter((name) => holds(current, name))
|
|
92
|
+
.map((name) => join(current, name))
|
|
93
|
+
.filter((file) => reader.candidate?.(file) ?? true);
|
|
97
94
|
|
|
98
95
|
directoryFiles.set(key, files);
|
|
99
96
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
2
|
import { biome } from "./biome.ts";
|
|
3
3
|
import { flat, legacy } from "./eslint.ts";
|
|
4
|
-
import { realDirectory } from "
|
|
4
|
+
import { realDirectory } from "../../../directories.ts";
|
|
5
5
|
import type { Decision, Reader } from "./layers.ts";
|
|
6
6
|
import { oxlint } from "./oxlint.ts";
|
|
7
7
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { readFileSync
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { realPath } from "../../../directories.ts";
|
|
2
3
|
import type { Node, Program } from "oxc-parser";
|
|
3
4
|
import { boundNames, children, declared, referenceChild, walk } from "../ast.ts";
|
|
4
5
|
import { parse } from "../parse.ts";
|
|
@@ -294,7 +295,7 @@ function indexModule(module: ConfigModule): void {
|
|
|
294
295
|
|
|
295
296
|
export function moduleAt(file: string): ConfigModule | null {
|
|
296
297
|
try {
|
|
297
|
-
file =
|
|
298
|
+
file = realPath(file);
|
|
298
299
|
if (modules.has(file)) return modules.get(file) ?? null;
|
|
299
300
|
|
|
300
301
|
const text = readFileSync(file, "utf8");
|