@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
@@ -1,273 +0,0 @@
1
- import { languageOf } from "../languages/index.ts";
2
- import type {
3
- BracePass,
4
- Language,
5
- Parsed,
6
- Rejection,
7
- Scan,
8
- Touches,
9
- } from "../languages/language.ts";
10
- import { blankLines, document, lineAt, parseFinding, position } from "./doc.ts";
11
- import { applyLines, applyOffsets, offsetMap, type OffsetEdit } from "./edits.ts";
12
- import { spacing } from "./gaps.ts";
13
- import { afterLines, afterOffsets, touching } from "./hunks.ts";
14
- import type { Changed, Doc, LineEdits, StatementList, Stmt } from "./model.ts";
15
- import { RULES, type RuleId } from "./rules.ts";
16
- import {
17
- compareFindings,
18
- type FileResult,
19
- type Finding,
20
- type Mode,
21
- type Options,
22
- } from "./types.ts";
23
-
24
- function touchedStatements(doc: Doc, lists: StatementList[], touches: Touches): Set<Stmt> {
25
- const touched = new Set<Stmt>();
26
- for (const list of lists)
27
- for (let index = 1; index < list.stmts.length; index++) {
28
- const prev = list.stmts[index - 1]!;
29
- const next = list.stmts[index]!;
30
- if (!touches(prev.startLine, next.endLine)) continue;
31
-
32
- const shortBodyMayJoin =
33
- list.stmts.length <= 3 && blankLines(doc, prev.endLine, next.startLine).length > 0;
34
-
35
- for (const stmt of shortBodyMayJoin ? list.stmts : [prev, next]) touched.add(stmt);
36
- }
37
-
38
- return touched;
39
- }
40
-
41
- function bracePass(doc: Doc, scanned: Scan, touches: Touches, collapseChains: boolean): BracePass {
42
- const touched = touchedStatements(doc, scanned.lists, touches);
43
- return scanned.braces?.pass(touches, touched, collapseChains) ?? NO_BRACES;
44
- }
45
-
46
- interface Trail {
47
- input: Doc;
48
- back: (offset: number) => number;
49
- findings: Finding[];
50
- }
51
-
52
- function startTrail(input: Doc): Trail {
53
- return { input, back: (offset) => offset, findings: [] };
54
- }
55
-
56
- function record(trail: Trail, doc: Doc, findings: Finding[]): void {
57
- for (const item of findings) {
58
- const offset = trail.back(doc.lineStarts[item.line - 1]! + item.col - 1);
59
- trail.findings.push({ ...item, ...position(trail.input, offset) });
60
- }
61
- }
62
-
63
- function retrace(trail: Trail, step: (offset: number) => number): void {
64
- const back = trail.back;
65
- trail.back = (offset) => back(step(offset));
66
- }
67
-
68
- function lineBack(before: Doc, after: Doc, edits: LineEdits): (offset: number) => number {
69
- const origins: number[] = [];
70
- for (let line = 1; line <= before.lines.length; line++) {
71
- if (!edits.deleteLines.has(line)) origins.push(line);
72
- if (edits.insertAfter.has(line)) origins.push(Math.min(line + 1, before.lines.length));
73
- }
74
-
75
- return (offset) => {
76
- const line = lineAt(after, offset);
77
- const origin = origins[line - 1]!;
78
- const column = Math.min(offset - after.lineStarts[line - 1]!, before.lines[origin - 1]!.length);
79
- return before.lineStarts[origin - 1]! + column;
80
- };
81
- }
82
-
83
- function gapBlanks(doc: Doc, scanned: Scan, back: (offset: number) => number): Map<number, number> {
84
- const blanks = new Map<number, number>();
85
- for (const list of scanned.lists)
86
- for (let index = 1; index < list.stmts.length; index++) {
87
- const prev = list.stmts[index - 1]!;
88
- const next = list.stmts[index]!;
89
- blanks.set(back(next.start), blankLines(doc, prev.endLine, next.startLine).length);
90
- }
91
-
92
- return blanks;
93
- }
94
-
95
- function netGaps(trail: Trail, first: Scan, final: Doc, finalScan: Scan): Finding[] {
96
- const { input, findings } = trail;
97
- const offsetOf = (item: Finding) => input.lineStarts[item.line - 1]! + item.col - 1;
98
- const gapOf = (item: Finding) => (item.rule in RULES ? RULES[item.rule as RuleId].gap : null);
99
-
100
- const gaps = new Map<number, Finding[]>();
101
- for (const item of findings)
102
- if (gapOf(item) !== null) gaps.set(offsetOf(item), [...(gaps.get(offsetOf(item)) ?? []), item]);
103
-
104
- if ([...gaps.values()].every((group) => group.length === 1)) return findings;
105
-
106
- const before = gapBlanks(input, first, (offset) => offset);
107
- const after = gapBlanks(final, finalScan, trail.back);
108
- return findings.filter((item) => {
109
- const offset = offsetOf(item);
110
- const group = gaps.get(offset);
111
- if (!group || group.length === 1) return true;
112
-
113
- const change = Math.sign((after.get(offset) ?? 0) - (before.get(offset) ?? 0));
114
- const want = change > 0 ? "at-least-one" : "none";
115
- return change !== 0 && item === group.findLast((other) => gapOf(other) === want);
116
- });
117
- }
118
-
119
- interface Parse {
120
- doc: Doc;
121
- scanned: Scan;
122
- error: Rejection | undefined;
123
- }
124
-
125
- function unbrace(
126
- language: Language,
127
- last: Parse,
128
- pass: BracePass,
129
- lines: Changed | undefined,
130
- trail: Trail,
131
- collapseChains: boolean,
132
- width: Options["width"],
133
- passes?: OffsetEdit[][],
134
- ): Parse & { lines: Changed | undefined } {
135
- let touches = touching(lines);
136
- for (; pass.edits.length > 0; pass = bracePass(last.doc, last.scanned, touches, collapseChains)) {
137
- const { doc } = last;
138
- passes?.push(pass.edits);
139
- record(trail, doc, pass.findings);
140
- retrace(trail, offsetMap(pass.edits).back);
141
-
142
- const text = applyOffsets(doc.text, pass.edits);
143
- const parsed = language.parse(doc.path, text);
144
- const next = document(doc.path, text, parsed.comments);
145
-
146
- lines = afterOffsets(doc, lines, pass.edits, next);
147
- touches = touching(lines);
148
- last = { doc: next, scanned: parsed.scan(next, width), error: parsed.error };
149
- }
150
-
151
- return { ...last, lines };
152
- }
153
-
154
- export interface Trace {
155
- original: Doc;
156
- first: Scan;
157
- passes: OffsetEdit[][];
158
- doc: Doc;
159
- scanned: Scan;
160
- }
161
-
162
- const NO_BRACES: BracePass = { edits: [], findings: [] };
163
- export function traceFix(
164
- language: Language,
165
- original: Doc,
166
- parsed: Parsed,
167
- keepBraces: boolean,
168
- width: Options["width"],
169
- ): Trace {
170
- const first = parsed.scan(original, width);
171
- const pass = keepBraces ? NO_BRACES : bracePass(original, first, touching(undefined), false);
172
- const passes: OffsetEdit[][] = [];
173
- const { doc, scanned } = unbrace(
174
- language,
175
- { doc: original, scanned: first, error: parsed.error },
176
- pass,
177
- undefined,
178
- startTrail(original),
179
- false,
180
- width,
181
- passes,
182
- );
183
-
184
- return { original, first, passes, doc, scanned };
185
- }
186
-
187
- export function processFile(path: string, text: string, mode: Mode, options: Options): FileResult {
188
- const language = languageOf(path);
189
- const parsed = language.parse(path, text);
190
- const doc = document(path, text, parsed.comments);
191
- const rejected = parsed.rejection(text);
192
- if (rejected)
193
- return {
194
- text,
195
- lastParse: { text, error: parsed.error },
196
- findings: [parseFinding(doc, rejected.start, rejected.message)],
197
- parseError: true,
198
- };
199
-
200
- const scanned = parsed.scan(doc, options.width);
201
- let lines = options.changedLines;
202
- let touches = touching(lines);
203
- const trail = startTrail(doc);
204
-
205
- let last: Parse = { doc, scanned, error: parsed.error };
206
- let settled: Finding[] | undefined;
207
- let pass = options.keepBraces ? NO_BRACES : bracePass(doc, scanned, touches, lines === undefined);
208
- while (true) {
209
- const unbraced = unbrace(
210
- language,
211
- last,
212
- pass,
213
- lines,
214
- trail,
215
- lines === undefined,
216
- options.width,
217
- );
218
-
219
- lines = unbraced.lines;
220
- touches = touching(lines);
221
-
222
- const spaced = spacing(unbraced.doc, unbraced.scanned.lists, unbraced.scanned.frozen, touches);
223
- record(
224
- trail,
225
- unbraced.doc,
226
- spaced.findings.filter((item) => item.fixable),
227
- );
228
-
229
- const spacedText = applyLines(unbraced.doc, spaced.edits);
230
- lines = afterLines(unbraced.doc, lines, spaced.edits);
231
- touches = touching(lines);
232
-
233
- if (spacedText === unbraced.doc.text || (mode === "check" && lines === undefined)) {
234
- last = unbraced;
235
- settled = spaced.findings;
236
- break;
237
- }
238
-
239
- const spacedParse = language.parse(path, spacedText);
240
- const spacedDoc = document(path, spacedText, spacedParse.comments);
241
- last = {
242
- doc: spacedDoc,
243
- scanned: spacedParse.scan(spacedDoc, options.width),
244
- error: spacedParse.error,
245
- };
246
-
247
- retrace(trail, lineBack(unbraced.doc, last.doc, spaced.edits));
248
- if (lines === undefined || options.keepBraces) break;
249
-
250
- pass = bracePass(last.doc, last.scanned, touches, false);
251
- if (pass.edits.length === 0) break;
252
- }
253
-
254
- const remaining = (
255
- settled ?? spacing(last.doc, last.scanned.lists, last.scanned.frozen, touches).findings
256
- ).filter((item) => !item.fixable);
257
-
258
- if (mode === "fix")
259
- return {
260
- text: last.doc.text,
261
- lastParse: { text: last.doc.text, error: last.error },
262
- findings: remaining.sort(compareFindings),
263
- parseError: false,
264
- };
265
-
266
- record(trail, last.doc, remaining);
267
- return {
268
- text,
269
- lastParse: { text: last.doc.text, error: last.error },
270
- findings: netGaps(trail, scanned, last.doc, last.scanned).sort(compareFindings),
271
- parseError: false,
272
- };
273
- }
@@ -1,103 +0,0 @@
1
- import type { GapRule } from "./rules.ts";
2
-
3
- export interface Comment {
4
- type: "Line" | "Block";
5
- value: string;
6
- start: number;
7
- end: number;
8
- }
9
-
10
- export interface Doc {
11
- path: string;
12
- text: string;
13
- lines: string[];
14
- lineStarts: number[];
15
- comments: Comment[];
16
- }
17
-
18
- export type Kind =
19
- | "declaration"
20
- | "assignment"
21
- | "if"
22
- | "loop"
23
- | "switch"
24
- | "case"
25
- | "try"
26
- | "function"
27
- | "return"
28
- | "throw"
29
- | "break"
30
- | "continue"
31
- | "expression"
32
- | "import"
33
- | "other";
34
-
35
- export type Path = [string, ...string[]];
36
-
37
- export type Binding = Set<string> | Path;
38
-
39
- export interface Facts {
40
- kind: Kind;
41
- word: string | null;
42
- binds: Binding | null;
43
- declaration: { keyword: string; letLike: boolean } | null;
44
- guard: { jump: string | null } | null;
45
- compact: boolean;
46
- joined: number | null;
47
- caseBody: boolean;
48
- operation: string | null;
49
- references: (names: Set<string>) => Iterable<string>;
50
- readsPath: (path: Path) => boolean;
51
- finallyRepeats: (operation: string) => boolean;
52
- }
53
-
54
- export interface Item<Node = unknown> extends Facts {
55
- node: Node;
56
- start: number;
57
- end: number;
58
- }
59
-
60
- export interface Stmt<Node = unknown> extends Item<Node> {
61
- frozen: boolean;
62
- startLine: number;
63
- endLine: number;
64
- multiline: boolean;
65
- detached: boolean;
66
- }
67
-
68
- export type ListKind = "block" | "switch" | "module";
69
-
70
- export interface StatementList<Node = unknown> {
71
- kind: ListKind;
72
- start: number;
73
- end: number;
74
- openLine: number | null;
75
- closeLine: number | null;
76
- stmts: Stmt<Node>[];
77
- }
78
-
79
- export type JoinRule = "guard-join" | "consume-join" | "use-join";
80
-
81
- export type GapDecision =
82
- | { want: "none"; rule: JoinRule; name: string; reader: string }
83
- | { want: "none"; rule: Exclude<GapRule<"none">, JoinRule> }
84
- | { want: "at-least-one"; rule: GapRule<"at-least-one"> }
85
- | { want: "keep" }
86
- | { want: "frozen" };
87
-
88
- export interface Gap {
89
- prev: Stmt;
90
- next: Stmt;
91
- blank: number;
92
- decision: GapDecision;
93
- }
94
-
95
- export interface LineEdits {
96
- deleteLines: Set<number>;
97
- insertAfter: Set<number>;
98
- }
99
-
100
- export interface Changed {
101
- lines: ReadonlySet<number>;
102
- deletedAfter: ReadonlySet<number>;
103
- }
@@ -1,104 +0,0 @@
1
- type Rule = { summary: string; message: (...args: never[]) => string } & (
2
- | { fixable: true; gap: "none" | "at-least-one" | null }
3
- | { fixable: false; gap: null }
4
- );
5
-
6
- const joinMessage = (name: string, reader: string) =>
7
- `keep \`${name}\` next to the \`${reader}\` that reads it (remove the blank line)`;
8
-
9
- export const RULES = {
10
- "after-multiline": {
11
- fixable: true,
12
- gap: "at-least-one",
13
- summary: "a statement that spans several lines is followed by a blank line",
14
- message: () => "blank line expected after the multi-line statement above",
15
- },
16
- "switch-clauses": {
17
- fixable: true,
18
- gap: "at-least-one",
19
- summary:
20
- "a blank line goes in between switch clauses and none is ever removed; the gap under a fall through label with an empty body is left as it is",
21
- message: () => "blank line expected between switch clauses",
22
- },
23
- "edge-blank": {
24
- fixable: true,
25
- gap: null,
26
- summary: "a block has no blank line right after `{` or right before `}`",
27
- message: (side: "after {" | "before }") => `no blank line right ${side}`,
28
- },
29
- "guard-join": {
30
- fixable: true,
31
- gap: "none",
32
- summary:
33
- "a single-line declaration or assignment is followed directly by an `if` that references what it binds, whatever the body size and with or without `else`",
34
- message: joinMessage,
35
- },
36
- "consume-join": {
37
- fixable: true,
38
- gap: "none",
39
- summary:
40
- "a single-line declaration or assignment is followed directly by a `return` or `switch` that references what it binds",
41
- message: joinMessage,
42
- },
43
- "use-join": {
44
- fixable: true,
45
- gap: "none",
46
- summary:
47
- "a single-line declaration is followed directly by a loop, `try` or function declaration that references what it binds",
48
- message: joinMessage,
49
- },
50
- "guard-chain": {
51
- fixable: true,
52
- gap: "none",
53
- summary:
54
- "consecutive compact guards (`if` with a one statement body and no `else`) have no blank line between them",
55
- message: () => "no blank line between consecutive compact guards",
56
- },
57
- "let-step": {
58
- fixable: true,
59
- gap: "at-least-one",
60
- summary:
61
- "a `let` that joins the block below it gets a blank line above it, so it starts its own step",
62
- message: () => "blank line expected above a let that the block below assigns or uses",
63
- },
64
- "after-guard": {
65
- fixable: true,
66
- gap: "at-least-one",
67
- summary:
68
- "a single-line guard that returns, throws, continues or breaks is followed by a blank line, unless the next statement is an `if` or a jump (`return`, `throw`, `break`, `continue`)",
69
- message: () =>
70
- "blank line expected after the guard above, the next statement starts a new step",
71
- },
72
- "short-body": {
73
- fixable: true,
74
- gap: "none",
75
- summary:
76
- "a block of two or three compact statements has no blank lines, whether it is a function body, an arrow body, a method, a nested block or a switch clause body",
77
- message: () => "no blank lines between 2 or 3 compact statements",
78
- },
79
- braces: {
80
- fixable: true,
81
- gap: null,
82
- summary: "a control flow body that is a single statement has no braces",
83
- message: () => "braces around a single statement body",
84
- },
85
- "block-spacing": {
86
- fixable: false,
87
- gap: null,
88
- summary: "a multi-line block directly under a statement, when no join rule explains it",
89
- message: () => "blank line expected before this block, or join it to the step above",
90
- },
91
- wall: {
92
- fixable: false,
93
- gap: null,
94
- summary:
95
- "six or more consecutive single-line statements with no blank line, reported only where a blank line `--fix` keeps can split the run",
96
- message: () => "6 or more statements with no blank line between them; separate the steps",
97
- },
98
- } as const satisfies Record<string, Rule>;
99
-
100
- export type RuleId = keyof typeof RULES;
101
-
102
- export type GapRule<W extends Rule["gap"]> = {
103
- [K in RuleId]: (typeof RULES)[K]["gap"] extends W ? K : never;
104
- }[RuleId];
@@ -1,38 +0,0 @@
1
- import type { Layout, Rejection } from "../languages/language.ts";
2
- import type { Changed } from "./model.ts";
3
- import type { RuleId } from "./rules.ts";
4
-
5
- export type Braces = "on" | "off";
6
-
7
- export type Mode = "fix" | "check";
8
-
9
- export interface Options {
10
- keepBraces: boolean;
11
- width: Layout;
12
- changedLines?: Changed;
13
- }
14
-
15
- export interface Finding {
16
- path: string;
17
- line: number;
18
- col: number;
19
- rule: RuleId | "parse" | "error" | "write";
20
- message: string;
21
- fixable: boolean;
22
- }
23
-
24
- export function compareFindings(left: Finding, right: Finding): number {
25
- return (
26
- left.path.localeCompare(right.path) ||
27
- left.line - right.line ||
28
- left.col - right.col ||
29
- left.rule.localeCompare(right.rule)
30
- );
31
- }
32
-
33
- export interface FileResult {
34
- text: string;
35
- lastParse: { text: string; error: Rejection | undefined };
36
- findings: Finding[];
37
- parseError: boolean;
38
- }