@ryuugg/stanza 0.3.1 → 0.3.3
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 +68 -150
- 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 +372 -0
- package/src/directories.ts +77 -0
- package/src/engine/explain.ts +7 -2
- package/src/engine/gaps.ts +46 -18
- package/src/engine/rules.ts +2 -1
- package/src/files.ts +388 -23
- package/src/format.ts +159 -0
- package/src/hook.ts +53 -12
- 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 +473 -0
- package/src/usage.ts +6 -3
- package/src/worker.ts +5 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ryuugg/stanza",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "An opinionated code formatter for TypeScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"formatter",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"oxfmt": "^0.70.0",
|
|
55
55
|
"oxlint": "^1.85.0",
|
|
56
56
|
"tegami": "^1.5.0",
|
|
57
|
-
"typescript": "
|
|
57
|
+
"typescript": "^7.0.2"
|
|
58
58
|
},
|
|
59
59
|
"engines": {
|
|
60
60
|
"bun": ">=1.4.0"
|
package/src/cli.ts
CHANGED
|
@@ -1,19 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
import { version } from "../package.json" with { type: "json" };
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
chmodSync,
|
|
7
|
-
constants,
|
|
8
|
-
readFileSync,
|
|
9
|
-
realpathSync,
|
|
10
|
-
renameSync,
|
|
11
|
-
type Stats,
|
|
12
|
-
statSync,
|
|
13
|
-
unlinkSync,
|
|
14
|
-
writeFileSync,
|
|
15
|
-
} from "node:fs";
|
|
16
|
-
import { dirname, join, relative, resolve } from "node:path";
|
|
3
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
4
|
+
import { dirname, relative, resolve } from "node:path";
|
|
5
|
+
import { beginInvocation } from "./directories.ts";
|
|
17
6
|
import {
|
|
18
7
|
collectChanged,
|
|
19
8
|
collectFiles,
|
|
@@ -36,8 +25,9 @@ import { blockReason, claudeCodeHooks, hookCall, rereadLine, type HookCall } fro
|
|
|
36
25
|
import { systemIo, type Io } from "./start.ts";
|
|
37
26
|
import type { Changed } from "./engine/model.ts";
|
|
38
27
|
import { RULES } from "./engine/rules.ts";
|
|
39
|
-
import { decode,
|
|
40
|
-
import {
|
|
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";
|
|
41
31
|
import { bothBraces, columns, flags, usage } from "./usage.ts";
|
|
42
32
|
|
|
43
33
|
export type { Io } from "./start.ts";
|
|
@@ -61,12 +51,6 @@ interface ExplainArguments {
|
|
|
61
51
|
noBraces: boolean;
|
|
62
52
|
}
|
|
63
53
|
|
|
64
|
-
interface Input {
|
|
65
|
-
path: string;
|
|
66
|
-
read: () => Decoded;
|
|
67
|
-
changedLines?: Changed | undefined;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
54
|
interface Run {
|
|
71
55
|
cwd: string;
|
|
72
56
|
mode: Mode;
|
|
@@ -75,15 +59,6 @@ interface Run {
|
|
|
75
59
|
display?: (path: string) => string;
|
|
76
60
|
}
|
|
77
61
|
|
|
78
|
-
interface Formatted {
|
|
79
|
-
findings: Finding[];
|
|
80
|
-
failed: boolean;
|
|
81
|
-
rewritten: string[];
|
|
82
|
-
kept: Map<string, string>;
|
|
83
|
-
unread: Set<string>;
|
|
84
|
-
unreadWidth: Set<string>;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
62
|
const switches = new Set(["--braces", "--changed", "--hunks", "--json", "--no-braces", "--staged"]);
|
|
88
63
|
const standalone = new Set(["--help", "-h", "--version"]);
|
|
89
64
|
|
|
@@ -108,10 +83,19 @@ function help(): string {
|
|
|
108
83
|
}
|
|
109
84
|
|
|
110
85
|
function parseExplain(args: string[]): ExplainArguments | { error: string } {
|
|
111
|
-
const
|
|
112
|
-
|
|
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" };
|
|
113
93
|
|
|
114
|
-
const
|
|
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;
|
|
115
99
|
if (unexpected !== undefined) return { error: `unexpected argument ${unexpected}` };
|
|
116
100
|
|
|
117
101
|
const match = /^(.+):(\d+)$/.exec(targets[0] ?? "");
|
|
@@ -120,7 +104,7 @@ function parseExplain(args: string[]): ExplainArguments | { error: string } {
|
|
|
120
104
|
const line = Number(match[2]);
|
|
121
105
|
if (line < 1) return { error: `${targets[0]}: the line must be 1 or more` };
|
|
122
106
|
|
|
123
|
-
return { path: match[1]!, line, noBraces:
|
|
107
|
+
return { path: match[1]!, line, noBraces: noBracesCount > 0 };
|
|
124
108
|
}
|
|
125
109
|
|
|
126
110
|
function bracesFlag(given: (flag: string) => boolean): Arguments["braces"] {
|
|
@@ -208,14 +192,6 @@ function printFindings(findings: Finding[], json: boolean, print: (line: string)
|
|
|
208
192
|
print(`${finding.path}:${finding.line}:${finding.col} ${finding.rule} ${finding.message}`);
|
|
209
193
|
}
|
|
210
194
|
|
|
211
|
-
function readText(path: string): Decoded {
|
|
212
|
-
try {
|
|
213
|
-
return decode(readFileSync(path));
|
|
214
|
-
} catch (error: unknown) {
|
|
215
|
-
return { message: String(error) };
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
195
|
function runStdin(input: string, args: Arguments, io: Io): number {
|
|
220
196
|
let source: Uint8Array;
|
|
221
197
|
try {
|
|
@@ -309,102 +285,13 @@ function runExplain(argv: string[], io: Io): number {
|
|
|
309
285
|
return result.found ? 0 : 1;
|
|
310
286
|
}
|
|
311
287
|
|
|
312
|
-
function removeQuietly(path: string): void {
|
|
313
|
-
try {
|
|
314
|
-
unlinkSync(path);
|
|
315
|
-
} catch {}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
function replaceFile(real: string, text: string, target: Stats): boolean {
|
|
319
|
-
const temp = join(dirname(real), `.stanza-${randomUUID()}.tmp`);
|
|
320
|
-
try {
|
|
321
|
-
writeFileSync(temp, text, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
322
|
-
} catch (error: unknown) {
|
|
323
|
-
removeQuietly(temp);
|
|
324
|
-
if (["EACCES", "EPERM"].includes(errorCode(error))) return false;
|
|
325
|
-
throw error;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
try {
|
|
329
|
-
const created = statSync(temp);
|
|
330
|
-
if (created.uid !== target.uid || created.gid !== target.gid) {
|
|
331
|
-
removeQuietly(temp);
|
|
332
|
-
return false;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
chmodSync(temp, target.mode & 0o7777);
|
|
336
|
-
renameSync(temp, real);
|
|
337
|
-
return true;
|
|
338
|
-
} catch (error: unknown) {
|
|
339
|
-
removeQuietly(temp);
|
|
340
|
-
throw error;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
function writeInPlace(real: string, text: string): void {
|
|
345
|
-
const original = readFileSync(real);
|
|
346
|
-
try {
|
|
347
|
-
writeFileSync(real, text, "utf8");
|
|
348
|
-
} catch (error: unknown) {
|
|
349
|
-
try {
|
|
350
|
-
writeFileSync(real, original);
|
|
351
|
-
} catch {}
|
|
352
|
-
|
|
353
|
-
throw error;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
function writeFixed(path: string, text: string, output: string): Finding | undefined {
|
|
358
|
-
try {
|
|
359
|
-
const real = realpathSync(path);
|
|
360
|
-
const target = statSync(real);
|
|
361
|
-
accessSync(real, constants.W_OK);
|
|
362
|
-
|
|
363
|
-
if (target.nlink > 1 || !replaceFile(real, text, target)) writeInPlace(real, text);
|
|
364
|
-
} catch (error: unknown) {
|
|
365
|
-
return { path: output, line: 1, col: 1, rule: "write", message: String(error), fixable: false };
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
288
|
function formatInputs(inputs: Input[], run: Run): Formatted {
|
|
370
|
-
const
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
const unreadWidth = new Set<string>();
|
|
375
|
-
|
|
376
|
-
let failed = false;
|
|
377
|
-
for (const input of inputs) {
|
|
378
|
-
const output = run.display?.(input.path) ?? printedPath(input.path, run.cwd);
|
|
379
|
-
const result = formatText(input.path, input.read(), {
|
|
380
|
-
mode: run.mode,
|
|
381
|
-
braces: run.braces,
|
|
382
|
-
changedLines: input.changedLines,
|
|
383
|
-
unread,
|
|
384
|
-
unreadWidth,
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
findings.push(...result.findings.map((finding) => ({ ...finding, path: output })));
|
|
388
|
-
failed ||= result.parseError;
|
|
389
|
-
|
|
390
|
-
if (result.fixed === undefined) continue;
|
|
391
|
-
|
|
392
|
-
if (!run.write) {
|
|
393
|
-
kept.set(input.path, result.fixed);
|
|
394
|
-
continue;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
const unwritten = writeFixed(input.path, result.fixed, output);
|
|
398
|
-
if (unwritten) {
|
|
399
|
-
findings.push(unwritten);
|
|
400
|
-
failed = true;
|
|
401
|
-
continue;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
rewritten.push(output);
|
|
405
|
-
}
|
|
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
|
+
);
|
|
406
293
|
|
|
407
|
-
return
|
|
294
|
+
return mergeOutcomes(outcomes, warnings);
|
|
408
295
|
}
|
|
409
296
|
|
|
410
297
|
function status(
|
|
@@ -415,7 +302,7 @@ function status(
|
|
|
415
302
|
return formatted.findings.length > 0 ? 1 : 0;
|
|
416
303
|
}
|
|
417
304
|
|
|
418
|
-
function runFiles(args: Arguments, io: Io): number {
|
|
305
|
+
function runFiles(args: Arguments, io: Io): number | Promise<number> {
|
|
419
306
|
const { cwd } = io;
|
|
420
307
|
|
|
421
308
|
let changed: ReturnType<typeof collectChanged> | undefined;
|
|
@@ -449,21 +336,43 @@ function runFiles(args: Arguments, io: Io): number {
|
|
|
449
336
|
warn(io, messages[reason]);
|
|
450
337
|
}
|
|
451
338
|
|
|
452
|
-
const
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
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,
|
|
459
356
|
);
|
|
460
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
|
+
|
|
461
370
|
printFindings(result.findings, args.json, (line) => io.stdout(`${line}\n`));
|
|
462
371
|
|
|
463
|
-
for (const error of
|
|
372
|
+
for (const error of selectionErrors) warn(io, `stanza: ${error}`);
|
|
464
373
|
|
|
465
|
-
warnUnread(io, cwd, result.unread, result.unreadWidth);
|
|
466
|
-
return status(
|
|
374
|
+
warnUnread(io, io.cwd, result.unread, result.unreadWidth);
|
|
375
|
+
return status([...selectionErrors, ...poolErrors], result);
|
|
467
376
|
}
|
|
468
377
|
|
|
469
378
|
function shellWord(word: string): string {
|
|
@@ -730,13 +639,18 @@ function runHook(args: string[], io: Io): number {
|
|
|
730
639
|
}
|
|
731
640
|
|
|
732
641
|
export function runHookCall(call: HookCall, args: string[], io: Io): number {
|
|
642
|
+
beginInvocation();
|
|
643
|
+
|
|
733
644
|
if (call.event === "write") return runWriteHook(call.cwd, call.toolInput, args, io);
|
|
734
645
|
if (call.event === "edit") return runEditHook(call, args, io);
|
|
735
646
|
return runStopHook(call, args, io);
|
|
736
647
|
}
|
|
737
648
|
|
|
738
|
-
export function main(argv: string[], io: Io): number {
|
|
649
|
+
export function main(argv: string[], io: Io): number | Promise<number> {
|
|
739
650
|
if (argv[0] === "hook") return runHook(argv.slice(1), io);
|
|
651
|
+
|
|
652
|
+
beginInvocation();
|
|
653
|
+
|
|
740
654
|
if (argv[0] === "explain") return runExplain(argv.slice(1), io);
|
|
741
655
|
|
|
742
656
|
const [only] = argv;
|
|
@@ -763,4 +677,8 @@ export function main(argv: string[], io: Io): number {
|
|
|
763
677
|
return runFiles(args, io);
|
|
764
678
|
}
|
|
765
679
|
|
|
766
|
-
|
|
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
|
+
});
|
package/src/compile/linux-x64.ts
CHANGED