@ryuugg/stanza 0.3.3 → 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.
Files changed (59) hide show
  1. package/README.md +7 -5
  2. package/package.json +19 -11
  3. package/src/launcher +102 -8
  4. package/src/cli.ts +0 -684
  5. package/src/compile/darwin-arm64.ts +0 -3
  6. package/src/compile/darwin-x64.ts +0 -3
  7. package/src/compile/linux-arm64-musl.ts +0 -3
  8. package/src/compile/linux-arm64.ts +0 -3
  9. package/src/compile/linux-x64-musl.ts +0 -3
  10. package/src/compile/linux-x64.ts +0 -3
  11. package/src/compiled.ts +0 -9
  12. package/src/depth.ts +0 -372
  13. package/src/directories.ts +0 -77
  14. package/src/engine/attach.ts +0 -62
  15. package/src/engine/directives.ts +0 -157
  16. package/src/engine/doc.ts +0 -93
  17. package/src/engine/edits.ts +0 -79
  18. package/src/engine/explain.ts +0 -337
  19. package/src/engine/gaps.ts +0 -361
  20. package/src/engine/hunks.ts +0 -319
  21. package/src/engine/index.ts +0 -273
  22. package/src/engine/model.ts +0 -103
  23. package/src/engine/rules.ts +0 -104
  24. package/src/engine/types.ts +0 -38
  25. package/src/files.ts +0 -1186
  26. package/src/format.ts +0 -159
  27. package/src/gitignore.ts +0 -280
  28. package/src/hook.ts +0 -380
  29. package/src/languages/index.ts +0 -34
  30. package/src/languages/javascript/ast.ts +0 -261
  31. package/src/languages/javascript/braces.ts +0 -374
  32. package/src/languages/javascript/config/biome.ts +0 -112
  33. package/src/languages/javascript/config/eslint.ts +0 -128
  34. package/src/languages/javascript/config/evaluate.ts +0 -278
  35. package/src/languages/javascript/config/find.ts +0 -105
  36. package/src/languages/javascript/config/glob.ts +0 -149
  37. package/src/languages/javascript/config/index.ts +0 -48
  38. package/src/languages/javascript/config/layers.ts +0 -468
  39. package/src/languages/javascript/config/module.ts +0 -327
  40. package/src/languages/javascript/config/oxlint.ts +0 -170
  41. package/src/languages/javascript/config/width.ts +0 -723
  42. package/src/languages/javascript/deserializers.ts +0 -17
  43. package/src/languages/javascript/embedded.ts +0 -7
  44. package/src/languages/javascript/entry.ts +0 -42
  45. package/src/languages/javascript/facts.ts +0 -294
  46. package/src/languages/javascript/index.ts +0 -20
  47. package/src/languages/javascript/lists.ts +0 -64
  48. package/src/languages/javascript/oracle.ts +0 -191
  49. package/src/languages/javascript/oxc-raw.d.ts +0 -31
  50. package/src/languages/javascript/parse.ts +0 -131
  51. package/src/languages/javascript/raw.ts +0 -95
  52. package/src/languages/javascript/scan.ts +0 -68
  53. package/src/languages/language.ts +0 -109
  54. package/src/native-file.d.ts +0 -4
  55. package/src/pool.ts +0 -473
  56. package/src/start.ts +0 -49
  57. package/src/step.ts +0 -113
  58. package/src/usage.ts +0 -35
  59. package/src/worker.ts +0 -5
package/src/cli.ts DELETED
@@ -1,684 +0,0 @@
1
- #!/usr/bin/env bun
2
- import { version } from "../package.json" with { type: "json" };
3
- import { readFileSync, realpathSync } from "node:fs";
4
- import { dirname, relative, resolve } from "node:path";
5
- import { beginInvocation } from "./directories.ts";
6
- import {
7
- collectChanged,
8
- collectFiles,
9
- collectStaged,
10
- emptyReason,
11
- errorCode,
12
- ignoredByGit,
13
- isGeneratedHeader,
14
- landsWithin,
15
- locate,
16
- stdinTarget,
17
- trackedInHead,
18
- type EmptyReason,
19
- type Location,
20
- type StagedFile,
21
- } from "./files.ts";
22
- import { explain } from "./engine/explain.ts";
23
- import { languageOf } from "./languages/index.ts";
24
- import { blockReason, claudeCodeHooks, hookCall, rereadLine, type HookCall } from "./hook.ts";
25
- import { systemIo, type Io } from "./start.ts";
26
- import type { Changed } from "./engine/model.ts";
27
- import { RULES } from "./engine/rules.ts";
28
- import { decode, withoutMark } from "./step.ts";
29
- import { type Braces, type Finding, type Mode } from "./engine/types.ts";
30
- import { formatFile, mergeOutcomes, readText, type Formatted, type Input } from "./format.ts";
31
- import { bothBraces, columns, flags, usage } from "./usage.ts";
32
-
33
- export type { Io } from "./start.ts";
34
-
35
- declare const STANZA_COMMIT: string | undefined;
36
-
37
- interface Arguments {
38
- changed: boolean;
39
- hunks: boolean;
40
- json: boolean;
41
- mode: Mode;
42
- braces: Braces | undefined;
43
- paths: string[];
44
- staged: boolean;
45
- stdin: string | undefined;
46
- }
47
-
48
- interface ExplainArguments {
49
- path: string;
50
- line: number;
51
- noBraces: boolean;
52
- }
53
-
54
- interface Run {
55
- cwd: string;
56
- mode: Mode;
57
- braces: Braces | undefined;
58
- write: boolean;
59
- display?: (path: string) => string;
60
- }
61
-
62
- const switches = new Set(["--braces", "--changed", "--hunks", "--json", "--no-braces", "--staged"]);
63
- const standalone = new Set(["--help", "-h", "--version"]);
64
-
65
- function help(): string {
66
- const rules = Object.entries(RULES).map(([id, rule]) => [
67
- ` ${id}`,
68
- rule.fixable ? "fix" : "check",
69
- rule.summary,
70
- ]);
71
-
72
- return [
73
- usage,
74
- "",
75
- ...columns(flags),
76
- "",
77
- "Claude Code .claude/settings.json:",
78
- claudeCodeHooks,
79
- "",
80
- "Rules:",
81
- ...columns(rules),
82
- ].join("\n");
83
- }
84
-
85
- function parseExplain(args: string[]): ExplainArguments | { error: string } {
86
- const delimiter = args.indexOf("--");
87
- const optionArgs = delimiter < 0 ? args : args.slice(0, delimiter);
88
- const literalLocations = delimiter < 0 ? [] : args.slice(delimiter + 1);
89
-
90
- const optionTargets = optionArgs.filter((arg) => arg !== "--no-braces");
91
- const noBracesCount = optionArgs.length - optionTargets.length;
92
- if (noBracesCount > 1) return { error: "--no-braces given twice" };
93
-
94
- const targets = [...optionTargets, ...literalLocations];
95
- const extra =
96
- targets.length > 1 ? (targets.find((arg) => arg === "--") ?? targets[1]) : undefined;
97
-
98
- const unexpected = optionTargets.find((arg) => arg.startsWith("-")) ?? extra;
99
- if (unexpected !== undefined) return { error: `unexpected argument ${unexpected}` };
100
-
101
- const match = /^(.+):(\d+)$/.exec(targets[0] ?? "");
102
- if (!match) return { error: "explain needs <file>:<line>" };
103
-
104
- const line = Number(match[2]);
105
- if (line < 1) return { error: `${targets[0]}: the line must be 1 or more` };
106
-
107
- return { path: match[1]!, line, noBraces: noBracesCount > 0 };
108
- }
109
-
110
- function bracesFlag(given: (flag: string) => boolean): Arguments["braces"] {
111
- if (given("--braces")) return "on";
112
- if (given("--no-braces")) return "off";
113
- }
114
-
115
- function parseArguments(args: string[]): Arguments | { error: string } {
116
- const paths: string[] = [];
117
- const seen = new Set<string>();
118
- const queue = args.values();
119
-
120
- let mode: Mode | undefined;
121
- let stdin: string | undefined;
122
- for (const arg of queue) {
123
- if (arg === "--") {
124
- paths.push(...queue);
125
- break;
126
- }
127
-
128
- if (arg === "--fix" || arg === "--check") {
129
- if (mode !== undefined) return { error: "use one of --fix or --check" };
130
- mode = arg.slice(2) as Mode;
131
- continue;
132
- }
133
-
134
- if (switches.has(arg)) {
135
- if (seen.has(arg)) return { error: `${arg} given twice` };
136
- seen.add(arg);
137
- continue;
138
- }
139
-
140
- if (arg === "--stdin") {
141
- const path = queue.next().value;
142
- if (stdin !== undefined) return { error: "--stdin given twice" };
143
- if (path === undefined || path.startsWith("-")) return { error: "--stdin needs a path" };
144
-
145
- stdin = path;
146
- continue;
147
- }
148
-
149
- if (standalone.has(arg)) return { error: `${arg} takes no other arguments` };
150
- if (arg.startsWith("-")) return { error: `unknown flag ${arg}` };
151
-
152
- paths.push(arg);
153
- }
154
-
155
- const changed = seen.has("--changed");
156
- const hunks = seen.has("--hunks");
157
- const staged = seen.has("--staged");
158
- const sources = [changed, staged, paths.length > 0, stdin !== undefined].filter(Boolean).length;
159
- if (mode === undefined) return { error: "--fix or --check is required" };
160
- if (sources === 0)
161
- return { error: "nothing to format: give paths, --changed, --staged or --stdin <path>" };
162
-
163
- if (sources > 1) return { error: "use only one of --changed, --staged, --stdin or paths" };
164
- if (staged && mode === "fix") return { error: "--staged works only with --check" };
165
- if (hunks && !changed && !staged) return { error: "--hunks needs --changed or --staged" };
166
- if (seen.has("--braces") && seen.has("--no-braces")) return { error: bothBraces };
167
-
168
- return {
169
- changed,
170
- hunks,
171
- json: seen.has("--json"),
172
- mode,
173
- braces: bracesFlag((flag) => seen.has(flag)),
174
- paths,
175
- staged,
176
- stdin,
177
- };
178
- }
179
-
180
- function printedPath(path: string, cwd: string): string {
181
- const output = relative(cwd, path);
182
- return output || path;
183
- }
184
-
185
- function printFindings(findings: Finding[], json: boolean, print: (line: string) => void): void {
186
- if (json) {
187
- print(JSON.stringify(findings));
188
- return;
189
- }
190
-
191
- for (const finding of findings)
192
- print(`${finding.path}:${finding.line}:${finding.col} ${finding.rule} ${finding.message}`);
193
- }
194
-
195
- function runStdin(input: string, args: Arguments, io: Io): number {
196
- let source: Uint8Array;
197
- try {
198
- source = io.stdin();
199
- } catch (error: unknown) {
200
- const message = `cannot read stdin: ${errorCode(error)}`;
201
- warn(io, `stanza: ${message}`);
202
- return status([message], { failed: false, findings: [] });
203
- }
204
-
205
- const target = stdinTarget(input, io.cwd);
206
- const inputs =
207
- target.status === "format" ? [{ path: target.path, read: () => decode(source) }] : [];
208
-
209
- const result = formatInputs(inputs, {
210
- cwd: io.cwd,
211
- mode: args.mode,
212
- braces: args.braces,
213
- write: false,
214
- });
215
-
216
- const fix = args.mode === "fix";
217
- if (fix)
218
- io.stdout((target.status === "format" ? result.kept.get(target.path) : undefined) ?? source);
219
-
220
- printFindings(result.findings, args.json, (line) => (fix ? io.stderr : io.stdout)(`${line}\n`));
221
-
222
- if (!(fix && args.json)) warnUnread(io, io.cwd, result.unread, result.unreadWidth);
223
-
224
- const errors =
225
- target.status === "unsupported" || target.status === "failed" ? [target.error] : [];
226
-
227
- for (const error of errors) warn(io, `stanza: ${error}`);
228
-
229
- return status(errors, result);
230
- }
231
-
232
- function runExplain(argv: string[], io: Io): number {
233
- const { cwd } = io;
234
- const args = parseExplain(argv);
235
- if ("error" in args) {
236
- warn(io, `stanza: ${args.error}\n${usage}`);
237
- return 2;
238
- }
239
-
240
- const target = stdinTarget(args.path, cwd);
241
- if (target.status === "unsupported" || target.status === "failed") {
242
- warn(io, `stanza: ${target.error}`);
243
- return 2;
244
- }
245
-
246
- let bytes: Uint8Array;
247
- try {
248
- bytes = readFileSync(resolve(cwd, args.path));
249
- } catch (error: unknown) {
250
- const code = errorCode(error);
251
- const failure =
252
- code === "ENOENT" ? `no such file: ${args.path}` : `cannot read ${args.path}: ${code}`;
253
-
254
- warn(io, `stanza: ${failure}`);
255
- return 2;
256
- }
257
-
258
- const text = decode(bytes);
259
- if (typeof text !== "string") {
260
- warn(io, `stanza: ${args.path}: ${text.message}`);
261
- return 2;
262
- }
263
-
264
- const root = realpathSync(cwd);
265
- const body = withoutMark(text);
266
- const result = explain({
267
- language: languageOf(args.path),
268
- path: realpathSync(resolve(cwd, args.path)),
269
- text: body,
270
- line: args.line,
271
- braces: args.noBraces ? "off" : undefined,
272
- display: (path) => printedPath(path, root),
273
- });
274
-
275
- if ("error" in result) {
276
- warn(io, `stanza: ${result.error}`);
277
- return 2;
278
- }
279
-
280
- for (const line of result.lines) io.stdout(`${line}\n`);
281
-
282
- if (target.status === "skip" || isGeneratedHeader(body))
283
- io.stdout("\nnote: --fix and --check skip this file as generated or excluded\n");
284
-
285
- return result.found ? 0 : 1;
286
- }
287
-
288
- function formatInputs(inputs: Input[], run: Run): Formatted {
289
- const warnings = { unread: new Set<string>(), unreadWidth: new Set<string>() };
290
- const outcomes = inputs.map((input) =>
291
- formatFile(input, run, run.display?.(input.path) ?? printedPath(input.path, run.cwd), warnings),
292
- );
293
-
294
- return mergeOutcomes(outcomes, warnings);
295
- }
296
-
297
- function status(
298
- errors: readonly string[],
299
- formatted: { failed: boolean; findings: Finding[] },
300
- ): number {
301
- if (errors.length > 0 || formatted.failed) return 2;
302
- return formatted.findings.length > 0 ? 1 : 0;
303
- }
304
-
305
- function runFiles(args: Arguments, io: Io): number | Promise<number> {
306
- const { cwd } = io;
307
-
308
- let changed: ReturnType<typeof collectChanged> | undefined;
309
- let collected: ReturnType<typeof collectFiles>;
310
- let reason: EmptyReason | undefined;
311
- try {
312
- changed = args.changed ? collectChanged(cwd, locate(cwd), args.hunks) : undefined;
313
- collected = changed ?? collectFiles(args.paths, cwd);
314
- if (args.paths.length > 0 && collected.files.length === 0 && collected.errors.length === 0)
315
- reason = emptyReason(args.paths, cwd);
316
- } catch (error: unknown) {
317
- warn(
318
- io,
319
- `stanza: file selection failed: ${error instanceof Error ? error.message : String(error)}`,
320
- );
321
-
322
- return 2;
323
- }
324
-
325
- for (const warning of collected.warnings) warn(io, `stanza: ${warning}`);
326
-
327
- if (reason !== undefined) {
328
- const paths = args.paths.join(", ");
329
- const messages: Record<EmptyReason, string> = {
330
- generated: `stanza: nothing to format in ${paths}, generated files are skipped`,
331
- ignored: `stanza: nothing to format in ${paths}, git ignores its files`,
332
- skipped: `stanza: nothing to format in ${paths}, its files are always skipped`,
333
- none: `stanza: no TypeScript or JavaScript files under ${paths}`,
334
- };
335
-
336
- warn(io, messages[reason]);
337
- }
338
-
339
- const inputs = collected.files.map((path) => ({
340
- path,
341
- output: printedPath(path, cwd),
342
- changedLines: args.hunks ? changed?.changedLines?.get(path) : undefined,
343
- }));
344
-
345
- const run = { cwd, mode: args.mode, braces: args.braces, write: true };
346
- const { workerCount, formatPooled } = require("./pool.ts") as typeof import("./pool.ts");
347
- const count = workerCount(inputs.length, io.env);
348
- if (count > 0)
349
- return formatPooled(inputs, run, count).then(({ formatted, errors }) =>
350
- finishFiles(args, io, collected.errors, formatted, errors),
351
- );
352
-
353
- const formatted = formatInputs(
354
- inputs.map((input) => ({ ...input, read: () => readText(input.path) })),
355
- run,
356
- );
357
-
358
- return finishFiles(args, io, collected.errors, formatted, []);
359
- }
360
-
361
- function finishFiles(
362
- args: Arguments,
363
- io: Io,
364
- selectionErrors: readonly string[],
365
- result: Formatted,
366
- poolErrors: readonly string[],
367
- ): number {
368
- for (const error of poolErrors) warn(io, `stanza: ${error}`);
369
-
370
- printFindings(result.findings, args.json, (line) => io.stdout(`${line}\n`));
371
-
372
- for (const error of selectionErrors) warn(io, `stanza: ${error}`);
373
-
374
- warnUnread(io, io.cwd, result.unread, result.unreadWidth);
375
- return status([...selectionErrors, ...poolErrors], result);
376
- }
377
-
378
- function shellWord(word: string): string {
379
- return /^[\w@%+:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", "'\\''")}'`;
380
- }
381
-
382
- function repairLines(
383
- findings: Finding[],
384
- files: StagedFile[],
385
- cwd: string,
386
- braces: Braces | undefined,
387
- ): string[] {
388
- const fixable = new Set(
389
- findings.filter((finding) => finding.fixable).map((finding) => finding.path),
390
- );
391
-
392
- const targets = files.filter((file) => fixable.has(printedPath(file.path, cwd)));
393
- const scoped = targets.filter((file) => file.lines !== undefined);
394
- const whole = targets.filter((file) => file.lines === undefined);
395
- const dirty = whole.filter((file) => file.unstaged);
396
- const clean = whole.filter((file) => !file.unstaged);
397
- const names = (list: StagedFile[]) =>
398
- list.map((file) => shellWord(printedPath(file.path, cwd))).join(" ");
399
-
400
- const lines: string[] = [];
401
- if (scoped.length > 0)
402
- lines.push(
403
- `fix the staged lines of these by hand and restage them with git add -p, --fix would change the whole file: ${names(scoped)}`,
404
- );
405
-
406
- if (dirty.length > 0)
407
- lines.push(
408
- `these also have unstaged changes, fix them with --fix and restage by hand: ${names(dirty)}`,
409
- );
410
-
411
- if (clean.length > 0) {
412
- const flags = braces === undefined ? "" : { on: " --braces", off: " --no-braces" }[braces];
413
-
414
- lines.push(
415
- `fix and restage with: cd ${shellWord(cwd)} && stanza --fix${flags} -- ${names(clean)} && git --literal-pathspecs add -- ${names(clean)}`,
416
- );
417
- }
418
-
419
- return lines;
420
- }
421
-
422
- function runStaged(args: Arguments, io: Io): number {
423
- const collected = collectStaged(io.cwd, args.hunks);
424
- const files = collected.ok ? collected.files : [];
425
- const result = formatInputs(
426
- files.map((file) => ({
427
- path: file.path,
428
- read: () => decode(file.bytes),
429
- changedLines: file.lines,
430
- })),
431
- { cwd: io.cwd, mode: args.mode, braces: args.braces, write: false },
432
- );
433
-
434
- printFindings(result.findings, args.json, (line) => io.stdout(`${line}\n`));
435
-
436
- if (!collected.ok) warn(io, `stanza: ${collected.error}`);
437
- else for (const line of repairLines(result.findings, files, io.cwd, args.braces)) warn(io, line);
438
-
439
- warnUnread(io, io.cwd, result.unread, result.unreadWidth);
440
- return status(collected.ok ? [] : [collected.error], result);
441
- }
442
-
443
- function warn(io: Io, line: string): void {
444
- io.stderr(`${line}\n`);
445
- }
446
-
447
- function warnUnread(io: Io, cwd: string, unread: Set<string>, unreadWidth: Set<string>): void {
448
- for (const [fileset, message] of [
449
- [
450
- unread,
451
- (files: string) =>
452
- `stanza: could not tell whether ${files} enforces braces, so braces stay; pass --braces or --no-braces to settle it`,
453
- ],
454
- [
455
- unreadWidth,
456
- (files: string) =>
457
- `stanza: could not read the line width from ${files}, so stanza used the next source for it`,
458
- ],
459
- ] as const) {
460
- if (fileset.size === 0) continue;
461
-
462
- const files = [...fileset]
463
- .map((path) => printedPath(path, cwd))
464
- .sort()
465
- .join(", ");
466
-
467
- warn(io, message(files));
468
- }
469
- }
470
-
471
- function runWriteHook(
472
- cwdInput: string,
473
- toolInput: { file_path: string; content: string } & Record<string, unknown>,
474
- args: string[],
475
- io: Io,
476
- ): number {
477
- let cwd: string;
478
- try {
479
- cwd = realpathSync(cwdInput);
480
- } catch {
481
- return 0;
482
- }
483
-
484
- let target: ReturnType<typeof stdinTarget>;
485
- try {
486
- target = stdinTarget(toolInput.file_path, cwd);
487
- } catch {
488
- return 0;
489
- }
490
-
491
- if (target.status === "failed") {
492
- warn(io, `stanza hook: ${target.error}`);
493
- return 1;
494
- }
495
-
496
- if (target.status !== "format" || target.root === undefined) return 0;
497
- if (!landsWithin(target.root, target.path) || ignoredByGit(target.root, target.path)) return 0;
498
- if (args.includes("--hunks") && trackedInHead(target.root, target.path)) return 0;
499
-
500
- const result = formatInputs([{ path: target.path, read: () => toolInput.content }], {
501
- cwd,
502
- mode: "fix",
503
- braces: bracesFlag((flag) => args.includes(flag)),
504
- write: false,
505
- });
506
-
507
- const fixed = result.kept.get(target.path);
508
- if (fixed !== undefined)
509
- io.stdout(
510
- `${JSON.stringify({
511
- hookSpecificOutput: {
512
- hookEventName: "PreToolUse",
513
- updatedInput: { ...toolInput, content: fixed },
514
- additionalContext: `stanza formatted ${toolInput.file_path} before writing it, so read it again before editing.`,
515
- },
516
- })}\n`,
517
- );
518
-
519
- warnUnread(io, cwd, result.unread, result.unreadWidth);
520
- return 0;
521
- }
522
-
523
- interface Repository {
524
- location: Location;
525
- written: Set<string> | undefined;
526
- }
527
-
528
- function repositories(cwd: string, written: Set<string> | undefined): Repository[] {
529
- if (written === undefined) {
530
- const location = locate(cwd);
531
- return location.kind === "outside" ? [] : [{ location, written }];
532
- }
533
-
534
- const found = new Map<string, Repository & { written: Set<string> }>();
535
- for (const path of written) {
536
- let real: string;
537
- try {
538
- real = realpathSync(path);
539
- } catch {
540
- continue;
541
- }
542
-
543
- const location = locate(dirname(path));
544
- if (location.kind === "outside") continue;
545
- if (location.kind === "repository" && !landsWithin(location.root, real)) continue;
546
-
547
- const key = location.kind === "repository" ? location.root : location.error;
548
- const repository = found.get(key) ?? { location, written: new Set<string>() };
549
- repository.written.add(real);
550
- found.set(key, repository);
551
- }
552
-
553
- return [...found.values()];
554
- }
555
-
556
- function fixChanged(
557
- cwdInput: string,
558
- written: Set<string> | undefined,
559
- args: string[],
560
- io: Io,
561
- ): Formatted | number {
562
- let cwd: string;
563
- try {
564
- cwd = realpathSync(cwdInput);
565
- } catch {
566
- return 0;
567
- }
568
-
569
- const hunks = args.includes("--hunks");
570
- const files: string[] = [];
571
- const changedLines = new Map<string, Changed>();
572
-
573
- let failed = false;
574
- for (const { location, written: kept } of repositories(cwd, written)) {
575
- const collected = collectChanged(cwd, location, hunks, kept);
576
- for (const warning of collected.warnings) warn(io, `stanza hook: ${warning}`);
577
- for (const error of collected.errors) warn(io, `stanza hook: ${error}`);
578
-
579
- failed ||= collected.errors.length > 0;
580
- files.push(...collected.files);
581
- for (const [path, lines] of collected.changedLines) changedLines.set(path, lines);
582
- }
583
-
584
- if (failed) return 1;
585
-
586
- const result = formatInputs(
587
- files.map((path) => ({
588
- path,
589
- read: () => readText(path),
590
- changedLines: hunks ? changedLines.get(path) : undefined,
591
- })),
592
- {
593
- cwd,
594
- mode: "fix",
595
- braces: bracesFlag((flag) => args.includes(flag)),
596
- write: true,
597
- display: (path) => (landsWithin(cwd, path) ? printedPath(path, cwd) : path),
598
- },
599
- );
600
-
601
- warnUnread(io, cwd, result.unread, result.unreadWidth);
602
- return result;
603
- }
604
-
605
- function runStopHook(
606
- input: { cwd: string; written: Set<string> | undefined },
607
- args: string[],
608
- io: Io,
609
- ): number {
610
- const result = fixChanged(input.cwd, input.written, args, io);
611
- if (typeof result === "number") return result;
612
-
613
- const reason = blockReason(result.findings, result.rewritten);
614
- if (reason !== undefined) io.stdout(`${JSON.stringify({ decision: "block", reason })}\n`);
615
-
616
- return 0;
617
- }
618
-
619
- function runEditHook(input: { cwd: string; paths: string[] }, args: string[], io: Io): number {
620
- const result = fixChanged(input.cwd, new Set(input.paths), args, io);
621
- if (typeof result === "number") return result;
622
-
623
- if (result.rewritten.length > 0)
624
- io.stdout(
625
- `${JSON.stringify({
626
- hookSpecificOutput: {
627
- hookEventName: "PostToolUse",
628
- additionalContext: rereadLine("formatted", result.rewritten),
629
- },
630
- })}\n`,
631
- );
632
-
633
- return 0;
634
- }
635
-
636
- function runHook(args: string[], io: Io): number {
637
- const call = hookCall(args, io);
638
- return typeof call === "number" ? call : runHookCall(call, args, io);
639
- }
640
-
641
- export function runHookCall(call: HookCall, args: string[], io: Io): number {
642
- beginInvocation();
643
-
644
- if (call.event === "write") return runWriteHook(call.cwd, call.toolInput, args, io);
645
- if (call.event === "edit") return runEditHook(call, args, io);
646
- return runStopHook(call, args, io);
647
- }
648
-
649
- export function main(argv: string[], io: Io): number | Promise<number> {
650
- if (argv[0] === "hook") return runHook(argv.slice(1), io);
651
-
652
- beginInvocation();
653
-
654
- if (argv[0] === "explain") return runExplain(argv.slice(1), io);
655
-
656
- const [only] = argv;
657
- if (argv.length === 1 && (only === "--help" || only === "-h")) {
658
- io.stdout(`${help()}\n`);
659
- return 0;
660
- }
661
-
662
- if (argv.length === 1 && only === "--version") {
663
- const commit = typeof STANZA_COMMIT === "string" ? STANZA_COMMIT : undefined;
664
- io.stdout(`${commit === undefined ? `stanza ${version}` : `stanza ${version} (${commit})`}\n`);
665
- return 0;
666
- }
667
-
668
- const args = parseArguments(argv);
669
- if ("error" in args) {
670
- warn(io, `stanza: ${args.error}\n${usage}`);
671
- return 2;
672
- }
673
-
674
- if (args.stdin !== undefined) return runStdin(args.stdin, args, io);
675
- if (args.staged) return runStaged(args, io);
676
-
677
- return runFiles(args, io);
678
- }
679
-
680
- // A bytecode build rejects top level await in this module, which the compiled binary bundles.
681
- if (import.meta.main)
682
- void Promise.resolve(main(process.argv.slice(2), systemIo())).then((code) => {
683
- process.exitCode = code;
684
- });
@@ -1,3 +0,0 @@
1
- import addon from "../../node_modules/@oxc-parser/binding-darwin-arm64/parser.darwin-arm64.node" with { type: "file" };
2
- import { start } from "../compiled.ts";
3
- void start(addon);
@@ -1,3 +0,0 @@
1
- import addon from "../../node_modules/@oxc-parser/binding-darwin-x64/parser.darwin-x64.node" with { type: "file" };
2
- import { start } from "../compiled.ts";
3
- void start(addon);
@@ -1,3 +0,0 @@
1
- import addon from "../../node_modules/@oxc-parser/binding-linux-arm64-musl/parser.linux-arm64-musl.node" with { type: "file" };
2
- import { start } from "../compiled.ts";
3
- void start(addon);
@@ -1,3 +0,0 @@
1
- import addon from "../../node_modules/@oxc-parser/binding-linux-arm64-gnu/parser.linux-arm64-gnu.node" with { type: "file" };
2
- import { start } from "../compiled.ts";
3
- void start(addon);
@@ -1,3 +0,0 @@
1
- import addon from "../../node_modules/@oxc-parser/binding-linux-x64-musl/parser.linux-x64-musl.node" with { type: "file" };
2
- import { start } from "../compiled.ts";
3
- void start(addon);
@@ -1,3 +0,0 @@
1
- import addon from "../../node_modules/@oxc-parser/binding-linux-x64-gnu/parser.linux-x64-gnu.node" with { type: "file" };
2
- import { start } from "../compiled.ts";
3
- void start(addon);