@ryuugg/stanza 0.1.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/LICENSE.md +21 -0
- package/README.md +163 -0
- package/package.json +63 -0
- package/src/cli.ts +820 -0
- package/src/compile/darwin-arm64.ts +3 -0
- package/src/compile/darwin-x64.ts +3 -0
- package/src/compile/linux-arm64-musl.ts +3 -0
- package/src/compile/linux-arm64.ts +3 -0
- package/src/compile/linux-x64-musl.ts +3 -0
- package/src/compile/linux-x64.ts +3 -0
- package/src/compiled.ts +6 -0
- package/src/engine/attach.ts +62 -0
- package/src/engine/directives.ts +154 -0
- package/src/engine/doc.ts +93 -0
- package/src/engine/edits.ts +79 -0
- package/src/engine/explain.ts +333 -0
- package/src/engine/gaps.ts +329 -0
- package/src/engine/hunks.ts +320 -0
- package/src/engine/index.ts +254 -0
- package/src/engine/model.ts +102 -0
- package/src/engine/rules.ts +103 -0
- package/src/engine/types.ts +37 -0
- package/src/files.ts +822 -0
- package/src/gitignore.ts +279 -0
- package/src/hook.ts +282 -0
- package/src/languages/index.ts +32 -0
- package/src/languages/javascript/ast.ts +199 -0
- package/src/languages/javascript/braces.ts +374 -0
- package/src/languages/javascript/config/biome.ts +112 -0
- package/src/languages/javascript/config/eslint.ts +128 -0
- package/src/languages/javascript/config/evaluate.ts +278 -0
- package/src/languages/javascript/config/find.ts +108 -0
- package/src/languages/javascript/config/glob.ts +150 -0
- package/src/languages/javascript/config/index.ts +48 -0
- package/src/languages/javascript/config/layers.ts +425 -0
- package/src/languages/javascript/config/module.ts +326 -0
- package/src/languages/javascript/config/oxlint.ts +170 -0
- package/src/languages/javascript/config/width.ts +715 -0
- package/src/languages/javascript/entry.ts +39 -0
- package/src/languages/javascript/facts.ts +268 -0
- package/src/languages/javascript/index.ts +20 -0
- package/src/languages/javascript/lists.ts +49 -0
- package/src/languages/javascript/oracle.ts +191 -0
- package/src/languages/javascript/parse.ts +58 -0
- package/src/languages/javascript/scan.ts +68 -0
- package/src/languages/language.ts +109 -0
- package/src/launcher +21 -0
- package/src/native-file.d.ts +4 -0
- package/src/step.ts +108 -0
- package/src/usage.ts +30 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { dirname, extname } from "node:path";
|
|
2
|
+
import { FALLBACK_WIDTH, type Language, type Opening, type Width } from "../languages/language.ts";
|
|
3
|
+
import { within } from "./directives.ts";
|
|
4
|
+
import { document, lineAt } from "./doc.ts";
|
|
5
|
+
import { offsetMap, type OffsetMap } from "./edits.ts";
|
|
6
|
+
import { blockSpacing, listGaps, matches, type Ruled } from "./gaps.ts";
|
|
7
|
+
import { traceFix, type Trace } from "./index.ts";
|
|
8
|
+
import type { Doc, Gap, StatementList, Stmt } from "./model.ts";
|
|
9
|
+
import { RULES } from "./rules.ts";
|
|
10
|
+
import type { Braces } from "./types.ts";
|
|
11
|
+
|
|
12
|
+
export interface ExplainRequest {
|
|
13
|
+
language: Language;
|
|
14
|
+
path: string;
|
|
15
|
+
text: string;
|
|
16
|
+
line: number;
|
|
17
|
+
braces: Braces | undefined;
|
|
18
|
+
display: (path: string) => string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type Explanation = { found: boolean; lines: string[] } | { error: string };
|
|
22
|
+
|
|
23
|
+
interface Traced extends Trace {
|
|
24
|
+
width: Width;
|
|
25
|
+
display: (path: string) => string;
|
|
26
|
+
maps: OffsetMap[];
|
|
27
|
+
unbraced: number[];
|
|
28
|
+
excerpts: Map<string, string>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const LABEL = 11;
|
|
32
|
+
|
|
33
|
+
function field(label: string, text: string): string {
|
|
34
|
+
return `${label ? `${label}:` : ""}`.padEnd(LABEL) + text;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function plural(count: number, noun: string): string {
|
|
38
|
+
return `${count === 0 ? "no" : count} ${noun}${count === 1 ? "" : "s"}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function lineList(lines: number[]): string {
|
|
42
|
+
const words = lines.map(String);
|
|
43
|
+
const last = words.pop()!;
|
|
44
|
+
return words.length === 0 ? `line ${last}` : `lines ${words.join(", ")} and ${last}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function back(maps: OffsetMap[], offset: number): number {
|
|
48
|
+
return maps.reduceRight((current, map) => map.back(current), offset);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function originalLine(trace: Traced, offset: number): number {
|
|
52
|
+
return lineAt(trace.original, back(trace.maps, offset));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function excerpt(trace: Traced, first: number, last: number): string {
|
|
56
|
+
const label = first === last ? `${first}` : `${first}-${last}`;
|
|
57
|
+
const cached = trace.excerpts.get(label);
|
|
58
|
+
if (cached !== undefined) return cached;
|
|
59
|
+
|
|
60
|
+
const line = ` ${label.padEnd(LABEL - 2)}${trace.original.lines[first - 1]!.trim()}`;
|
|
61
|
+
trace.excerpts.set(label, line);
|
|
62
|
+
return line;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function wants(decision: Ruled): string {
|
|
66
|
+
return decision.want === "none" ? "no blank line" : "a blank line";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function codeLine(doc: Doc, stmt: Stmt): number {
|
|
70
|
+
return lineAt(doc, stmt.start);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function because(
|
|
74
|
+
trace: Traced,
|
|
75
|
+
list: StatementList,
|
|
76
|
+
gap: Gap,
|
|
77
|
+
decision: Ruled,
|
|
78
|
+
at: (line: number) => number,
|
|
79
|
+
): string {
|
|
80
|
+
const { prev, next } = gap;
|
|
81
|
+
if ("name" in decision)
|
|
82
|
+
return `\`${decision.name}\` is bound on line ${at(codeLine(trace.doc, prev))} and read by the \`${decision.reader}\` below it`;
|
|
83
|
+
|
|
84
|
+
switch (decision.rule) {
|
|
85
|
+
case "short-body":
|
|
86
|
+
return `the block holds ${list.stmts.length} statements, each compact`;
|
|
87
|
+
|
|
88
|
+
case "guard-chain":
|
|
89
|
+
return prev.joined !== null || next.joined !== null
|
|
90
|
+
? `both guards fit on one line at ${trace.width.columns} columns`
|
|
91
|
+
: "both statements are compact guards";
|
|
92
|
+
|
|
93
|
+
case "after-multiline":
|
|
94
|
+
return `the statement above spans lines ${at(codeLine(trace.doc, prev))} to ${at(prev.endLine)}`;
|
|
95
|
+
|
|
96
|
+
case "switch-clauses":
|
|
97
|
+
return "the clause above has a body";
|
|
98
|
+
|
|
99
|
+
case "after-guard":
|
|
100
|
+
return `the guard on line ${at(codeLine(trace.doc, prev))} ends in ${prev.guard?.jump ? `\`${prev.guard.jump}\`` : "a jump"}, so the next statement starts a new step`;
|
|
101
|
+
|
|
102
|
+
case "let-step":
|
|
103
|
+
return `the \`${next.declaration?.keyword}\` on line ${at(codeLine(trace.doc, next))} joins the block below it, so it starts its own step`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function gapResult(trace: Traced, gap: Gap, at: (line: number) => number): string {
|
|
108
|
+
const { prev, next, blank, decision } = gap;
|
|
109
|
+
if (decision.want === "keep" || decision.want === "frozen") return "--fix leaves the gap alone";
|
|
110
|
+
if (next.detached)
|
|
111
|
+
return `the comment above line ${at(codeLine(trace.doc, next))} is set apart by a blank line, so --fix leaves the gap alone`;
|
|
112
|
+
|
|
113
|
+
if (next.startLine <= prev.endLine)
|
|
114
|
+
return "both statements share a line, so --fix leaves the gap alone";
|
|
115
|
+
|
|
116
|
+
if (decision.want === "none")
|
|
117
|
+
return blank === 0
|
|
118
|
+
? "already has no blank line"
|
|
119
|
+
: `--fix removes ${plural(blank, "blank line")}`;
|
|
120
|
+
|
|
121
|
+
return blank > 0 ? "already has a blank line" : "--fix adds a blank line";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function explainGap(trace: Traced, list: StatementList, gap: Gap): string[] {
|
|
125
|
+
const { prev, next, decision } = gap;
|
|
126
|
+
const at = (line: number) => originalLine(trace, trace.doc.lineStarts[line - 1]!);
|
|
127
|
+
const lines = [
|
|
128
|
+
`gap above line ${at(codeLine(trace.doc, next))}, ${plural(gap.blank, "blank line")}`,
|
|
129
|
+
excerpt(trace, at(codeLine(trace.doc, prev)), at(prev.endLine)),
|
|
130
|
+
excerpt(trace, at(codeLine(trace.doc, next)), at(codeLine(trace.doc, next))),
|
|
131
|
+
];
|
|
132
|
+
|
|
133
|
+
const first = originalLine(trace, list.start);
|
|
134
|
+
const last = originalLine(trace, list.end - 1);
|
|
135
|
+
const unbraced = trace.unbraced.filter((line) => first <= line && line <= last);
|
|
136
|
+
if (unbraced.length > 0)
|
|
137
|
+
lines.push(field("note", `decided after --fix removes the braces on ${lineList(unbraced)}`));
|
|
138
|
+
|
|
139
|
+
const joined = [prev, next].filter((stmt) => stmt.joined !== null);
|
|
140
|
+
if (joined.length > 0) {
|
|
141
|
+
const source = trace.width.source;
|
|
142
|
+
const label =
|
|
143
|
+
source.kind === "config"
|
|
144
|
+
? trace.display(source.file)
|
|
145
|
+
: source.kind === "default"
|
|
146
|
+
? `${source.formatter} default`
|
|
147
|
+
: "fallback";
|
|
148
|
+
|
|
149
|
+
lines.push(field("width", `${trace.width.columns} columns, from ${label}`));
|
|
150
|
+
|
|
151
|
+
for (const stmt of joined)
|
|
152
|
+
lines.push(
|
|
153
|
+
field(
|
|
154
|
+
"",
|
|
155
|
+
`line ${at(codeLine(trace.doc, stmt))} joins to ${stmt.joined} columns, ${stmt.joined! <= trace.width.columns ? "fits on one line" : "too long for one line"}`,
|
|
156
|
+
),
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (decision.want === "frozen")
|
|
161
|
+
lines.push(field("rule", "none, a stanza directive covers one of the two statements"));
|
|
162
|
+
else if (decision.want === "keep")
|
|
163
|
+
lines.push(field("rule", "none applies, the blank lines stay as written"));
|
|
164
|
+
else {
|
|
165
|
+
const outranked = [...matches(list, prev, next)].filter(
|
|
166
|
+
(match) => match.rule !== decision.rule,
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
lines.push(
|
|
170
|
+
field("rule", `${decision.rule}, wants ${wants(decision)}`),
|
|
171
|
+
field("because", because(trace, list, gap, decision, at)),
|
|
172
|
+
...(outranked.length === 0
|
|
173
|
+
? [field("outranked", "none")]
|
|
174
|
+
: outranked.map((match, index) =>
|
|
175
|
+
field(
|
|
176
|
+
index === 0 ? "outranked" : "",
|
|
177
|
+
`${match.rule}, wants ${wants(match)}: ${because(trace, list, gap, match, at)}`,
|
|
178
|
+
),
|
|
179
|
+
)),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
lines.push(field("result", gapResult(trace, gap, at)));
|
|
184
|
+
|
|
185
|
+
const [reported] = blockSpacing(trace.doc, gap);
|
|
186
|
+
if (reported) lines.push(field("reported", `block-spacing, ${reported.message}`));
|
|
187
|
+
|
|
188
|
+
return lines;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function configHolds(language: Language, request: Omit<ExplainRequest, "line">): string[] {
|
|
192
|
+
if (request.braces === "off") return ["--no-braces turns the rule off"];
|
|
193
|
+
if (request.braces === "on") return [];
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
return (language.config?.decisions(dirname(request.path), extname(request.path)) ?? [])
|
|
197
|
+
.filter((decision) => decision.setting !== "off")
|
|
198
|
+
.map((decision) => {
|
|
199
|
+
const files = decision.files.map(request.display).join(", ");
|
|
200
|
+
return decision.setting === "on"
|
|
201
|
+
? `${files} enforces braces`
|
|
202
|
+
: `stanza cannot tell whether ${files} enforces braces, so it keeps them`;
|
|
203
|
+
});
|
|
204
|
+
} catch (error: unknown) {
|
|
205
|
+
return [`reading the lint config failed, so stanza keeps them: ${String(error)}`];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function explainBlock(trace: Traced, block: Opening, config: string[]): string[] {
|
|
210
|
+
const { open, close, inner } = block;
|
|
211
|
+
const lines = [
|
|
212
|
+
open === close ? `braced body on line ${open}` : `braced body, lines ${open} to ${close}`,
|
|
213
|
+
];
|
|
214
|
+
|
|
215
|
+
if (inner) lines.push(excerpt(trace, ...inner));
|
|
216
|
+
|
|
217
|
+
const rule = field("rule", `braces, ${RULES.braces.summary}`);
|
|
218
|
+
|
|
219
|
+
let offset = block.start;
|
|
220
|
+
for (const [pass, map] of trace.maps.entries()) {
|
|
221
|
+
if (map.removes(offset))
|
|
222
|
+
return [
|
|
223
|
+
...lines,
|
|
224
|
+
rule,
|
|
225
|
+
field(
|
|
226
|
+
"result",
|
|
227
|
+
pass === 0
|
|
228
|
+
? "--fix removes the braces"
|
|
229
|
+
: "--fix removes the braces once the braces inside them are gone",
|
|
230
|
+
),
|
|
231
|
+
];
|
|
232
|
+
|
|
233
|
+
offset = map.forward(offset);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const hold = trace.scanned.braces!.hold(offset, (moved) => originalLine(trace, moved));
|
|
237
|
+
if (hold.kind === "directive")
|
|
238
|
+
return [...lines, field("rule", "braces, but a stanza directive covers this body")];
|
|
239
|
+
|
|
240
|
+
if (hold.kind === "inapplicable")
|
|
241
|
+
return [...lines, field("rule", `braces does not apply, ${hold.reason}`)];
|
|
242
|
+
|
|
243
|
+
const reasons = [...config, ...(hold.reason ? [hold.reason] : [])];
|
|
244
|
+
return [
|
|
245
|
+
...lines,
|
|
246
|
+
rule,
|
|
247
|
+
...reasons.map((reason, index) => field(index === 0 ? "kept by" : "", reason)),
|
|
248
|
+
field("result", reasons.length > 0 ? "the braces stay" : "--fix removes the braces"),
|
|
249
|
+
];
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function explainer(
|
|
253
|
+
request: Omit<ExplainRequest, "line">,
|
|
254
|
+
): { error: string } | ((line: number) => Explanation) {
|
|
255
|
+
const { language, path, text } = request;
|
|
256
|
+
const parsed = language.parse(path, text);
|
|
257
|
+
const original = document(path, text, parsed.comments);
|
|
258
|
+
const rejected = parsed.rejection(text);
|
|
259
|
+
if (rejected) {
|
|
260
|
+
const at = lineAt(original, rejected.start);
|
|
261
|
+
return { error: `${request.display(path)}:${at} does not parse: ${rejected.message}` };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const config = configHolds(language, request);
|
|
265
|
+
const width = language.config?.width(path) ?? FALLBACK_WIDTH;
|
|
266
|
+
|
|
267
|
+
const fixed = traceFix(language, original, parsed, config.length > 0, width);
|
|
268
|
+
const maps = fixed.passes.map(offsetMap);
|
|
269
|
+
const unbraced: number[] = [];
|
|
270
|
+
for (const [pass, edits] of fixed.passes.entries())
|
|
271
|
+
for (let index = 0; index < edits.length; index += 2)
|
|
272
|
+
unbraced.push(lineAt(original, back(maps.slice(0, pass), edits[index]!.start)));
|
|
273
|
+
|
|
274
|
+
const traced: Traced = {
|
|
275
|
+
...fixed,
|
|
276
|
+
width,
|
|
277
|
+
display: request.display,
|
|
278
|
+
maps,
|
|
279
|
+
unbraced,
|
|
280
|
+
excerpts: new Map(),
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const gapsAt = new Map<number, [StatementList, Gap][]>();
|
|
284
|
+
for (const list of traced.scanned.lists)
|
|
285
|
+
for (const gap of listGaps(traced.doc, list)) {
|
|
286
|
+
const starts = [codeLine(traced.doc, gap.next), gap.next.startLine].map((number) =>
|
|
287
|
+
originalLine(traced, traced.doc.lineStarts[number - 1]!),
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
for (const start of new Set(starts))
|
|
291
|
+
gapsAt.set(start, [...(gapsAt.get(start) ?? []), [list, gap]]);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return (line: number): Explanation => {
|
|
295
|
+
if (line > original.lines.length)
|
|
296
|
+
return { error: `${request.display(path)} has ${original.lines.length} lines, not ${line}` };
|
|
297
|
+
|
|
298
|
+
const sections = (gapsAt.get(line) ?? []).map(([list, gap]) => explainGap(traced, list, gap));
|
|
299
|
+
for (const block of traced.first.braces?.opening(line) ?? [])
|
|
300
|
+
sections.push(explainBlock(traced, block, config));
|
|
301
|
+
|
|
302
|
+
if (sections.length === 0 && within(traced.first.frozen, original.lineStarts[line - 1]!))
|
|
303
|
+
sections.push([`line ${line} is inside a stanza-off region, so stanza leaves it alone`]);
|
|
304
|
+
|
|
305
|
+
const header = `${request.display(path)}:${line}`;
|
|
306
|
+
if (sections.length === 0)
|
|
307
|
+
return {
|
|
308
|
+
found: false,
|
|
309
|
+
lines: [`${header}: no gap ends and no braced body starts on this line`],
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const unread =
|
|
313
|
+
width.unread.length > 0
|
|
314
|
+
? [
|
|
315
|
+
"",
|
|
316
|
+
field(
|
|
317
|
+
"note",
|
|
318
|
+
`could not read the line width from ${width.unread.map(request.display).join(", ")}`,
|
|
319
|
+
),
|
|
320
|
+
]
|
|
321
|
+
: [];
|
|
322
|
+
|
|
323
|
+
return {
|
|
324
|
+
found: true,
|
|
325
|
+
lines: [header, ...unread, ...sections.flatMap((section) => ["", ...section])],
|
|
326
|
+
};
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function explain(request: ExplainRequest): Explanation {
|
|
331
|
+
const result = explainer(request);
|
|
332
|
+
return typeof result === "function" ? result(request.line) : result;
|
|
333
|
+
}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { blankLines, commentIndex, finding, lineAt } from "./doc.ts";
|
|
2
|
+
import { within, type Region } from "./directives.ts";
|
|
3
|
+
import type {
|
|
4
|
+
Binding,
|
|
5
|
+
Doc,
|
|
6
|
+
Gap,
|
|
7
|
+
GapDecision,
|
|
8
|
+
JoinRule,
|
|
9
|
+
Kind,
|
|
10
|
+
LineEdits,
|
|
11
|
+
Path,
|
|
12
|
+
StatementList,
|
|
13
|
+
Stmt,
|
|
14
|
+
} from "./model.ts";
|
|
15
|
+
import type { Finding } from "./types.ts";
|
|
16
|
+
|
|
17
|
+
const JUMP_KINDS = new Set<Kind>(["return", "throw", "continue", "break"]);
|
|
18
|
+
|
|
19
|
+
const USE_KINDS = new Set<Kind>(["loop", "try", "function"]);
|
|
20
|
+
|
|
21
|
+
const BLOCK_KINDS = new Set<Kind>(["if", "loop", "try", "switch"]);
|
|
22
|
+
|
|
23
|
+
function pathName(path: Path): string {
|
|
24
|
+
return path.reduce((name, segment) =>
|
|
25
|
+
segment.startsWith("[") ? name + segment : `${name}.${segment}`,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function readBy(bound: Binding, stmt: Stmt): string | undefined {
|
|
30
|
+
if (bound instanceof Set) return stmt.references(bound)[Symbol.iterator]().next().value;
|
|
31
|
+
return stmt.readsPath(bound) ? pathName(bound) : undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function joinRule(prev: Stmt, next: Stmt): JoinRule | null {
|
|
35
|
+
if (next.kind === "if") return "guard-join";
|
|
36
|
+
if (next.kind === "return" || next.kind === "switch") return "consume-join";
|
|
37
|
+
if (USE_KINDS.has(next.kind) && prev.kind === "declaration") return "use-join";
|
|
38
|
+
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function declarationJoin(prev: Stmt, next: Stmt): Ruled | null {
|
|
43
|
+
const name = prev.binds === null ? undefined : readBy(prev.binds, next);
|
|
44
|
+
if (name === undefined) return null;
|
|
45
|
+
|
|
46
|
+
const rule = joinRule(prev, next);
|
|
47
|
+
if (rule === null || next.word === null) return null;
|
|
48
|
+
|
|
49
|
+
return { want: "none", rule, name, reader: next.word };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function shortBody(list: StatementList): boolean {
|
|
53
|
+
return (
|
|
54
|
+
list.kind !== "switch" &&
|
|
55
|
+
list.stmts.length >= 2 &&
|
|
56
|
+
list.stmts.length <= 3 &&
|
|
57
|
+
list.stmts.every((stmt) => stmt.compact)
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type Ruled = Exclude<GapDecision, { want: "keep" | "frozen" }>;
|
|
62
|
+
|
|
63
|
+
type Step = (list: StatementList, prev: Stmt, next: Stmt) => Ruled | null;
|
|
64
|
+
|
|
65
|
+
const LADDER: Step[] = [
|
|
66
|
+
(list) => (shortBody(list) ? { want: "none", rule: "short-body" } : null),
|
|
67
|
+
(_list, prev, next) =>
|
|
68
|
+
prev.guard && next.guard && prev.compact && next.compact
|
|
69
|
+
? { want: "none", rule: "guard-chain" }
|
|
70
|
+
: null,
|
|
71
|
+
(_list, prev) => (prev.multiline ? { want: "at-least-one", rule: "after-multiline" } : null),
|
|
72
|
+
(list, prev) =>
|
|
73
|
+
list.kind === "switch" && prev.kind === "case" && prev.caseBody
|
|
74
|
+
? { want: "at-least-one", rule: "switch-clauses" }
|
|
75
|
+
: null,
|
|
76
|
+
(_list, prev, next) => declarationJoin(prev, next),
|
|
77
|
+
(_list, prev, next) =>
|
|
78
|
+
prev.guard?.jump && next.kind !== "if" && !JUMP_KINDS.has(next.kind)
|
|
79
|
+
? { want: "at-least-one", rule: "after-guard" }
|
|
80
|
+
: null,
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
export function* matches(list: StatementList, prev: Stmt, next: Stmt): Generator<Ruled> {
|
|
84
|
+
for (const step of LADDER) {
|
|
85
|
+
const decision = step(list, prev, next);
|
|
86
|
+
if (decision) yield decision;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function decide(list: StatementList, prev: Stmt, next: Stmt): GapDecision {
|
|
91
|
+
if (prev.frozen || next.frozen) return { want: "frozen" };
|
|
92
|
+
for (const decision of matches(list, prev, next)) return decision;
|
|
93
|
+
return { want: "keep" };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function mayJoinRun(gap: Gap): boolean {
|
|
97
|
+
return (
|
|
98
|
+
gap.decision.want === "keep" &&
|
|
99
|
+
!gap.next.detached &&
|
|
100
|
+
gap.prev.declaration?.letLike === true &&
|
|
101
|
+
!gap.prev.multiline &&
|
|
102
|
+
gap.prev.binds !== null
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function referenceRanks(block: Stmt, bindings: Binding[]): Map<string, number> {
|
|
107
|
+
const names = new Set<string>();
|
|
108
|
+
for (const bound of bindings) if (bound instanceof Set) for (const name of bound) names.add(name);
|
|
109
|
+
|
|
110
|
+
const ranks = new Map<string, number>();
|
|
111
|
+
if (names.size === 0) return ranks;
|
|
112
|
+
|
|
113
|
+
for (const name of block.references(names)) {
|
|
114
|
+
if (!ranks.has(name)) ranks.set(name, ranks.size);
|
|
115
|
+
if (ranks.size === names.size) break;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return ranks;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function firstRead(bound: Binding, block: Stmt, ranks: Map<string, number>): string | undefined {
|
|
122
|
+
if (!(bound instanceof Set)) return readBy(bound, block);
|
|
123
|
+
|
|
124
|
+
let first: string | undefined;
|
|
125
|
+
for (const name of bound) {
|
|
126
|
+
const rank = ranks.get(name);
|
|
127
|
+
if (rank !== undefined && (first === undefined || rank < ranks.get(first)!)) first = name;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return first;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function letSteps(gaps: Gap[]): void {
|
|
134
|
+
for (let index = 1; index < gaps.length; index++) {
|
|
135
|
+
const joined = gaps[index]!;
|
|
136
|
+
const { decision } = joined;
|
|
137
|
+
if (
|
|
138
|
+
!joined.prev.declaration?.letLike ||
|
|
139
|
+
decision.want !== "none" ||
|
|
140
|
+
decision.rule === "short-body"
|
|
141
|
+
)
|
|
142
|
+
continue;
|
|
143
|
+
|
|
144
|
+
let start = index;
|
|
145
|
+
while (start > 0 && mayJoinRun(gaps[start - 1]!)) start--;
|
|
146
|
+
|
|
147
|
+
const run = gaps.slice(start, index).toReversed();
|
|
148
|
+
const ranks = referenceRanks(
|
|
149
|
+
joined.next,
|
|
150
|
+
run.map((gap) => gap.prev.binds!),
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
let first = index;
|
|
154
|
+
for (const gap of run) {
|
|
155
|
+
const name = firstRead(gap.prev.binds!, joined.next, ranks);
|
|
156
|
+
if (name === undefined) break;
|
|
157
|
+
|
|
158
|
+
first--;
|
|
159
|
+
gap.decision = "name" in decision ? { ...decision, name } : decision;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const above = gaps[first - 1];
|
|
163
|
+
if (above?.decision.want === "keep")
|
|
164
|
+
above.decision = { want: "at-least-one", rule: "let-step" };
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function bracketedTry(prev: Stmt, next: Stmt): boolean {
|
|
169
|
+
return prev.operation !== null && next.finallyRepeats(prev.operation);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function blockSpacing(doc: Doc, gap: Gap): Finding[] {
|
|
173
|
+
const { prev, next, blank, decision } = gap;
|
|
174
|
+
if (
|
|
175
|
+
decision.want !== "keep" ||
|
|
176
|
+
blank !== 0 ||
|
|
177
|
+
prev.multiline ||
|
|
178
|
+
!next.multiline ||
|
|
179
|
+
!BLOCK_KINDS.has(next.kind)
|
|
180
|
+
)
|
|
181
|
+
return [];
|
|
182
|
+
|
|
183
|
+
if (prev.kind === "if" && next.kind === "if") return [];
|
|
184
|
+
if (bracketedTry(prev, next)) return [];
|
|
185
|
+
|
|
186
|
+
return [finding(doc, next.start, "block-spacing")];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function gapEdits(doc: Doc, gap: Gap, edits: LineEdits): Finding[] {
|
|
190
|
+
const { prev, next, blank, decision } = gap;
|
|
191
|
+
if (
|
|
192
|
+
decision.want === "keep" ||
|
|
193
|
+
decision.want === "frozen" ||
|
|
194
|
+
next.detached ||
|
|
195
|
+
next.startLine <= prev.endLine ||
|
|
196
|
+
(decision.want === "none" ? blank === 0 : blank > 0)
|
|
197
|
+
)
|
|
198
|
+
return [];
|
|
199
|
+
|
|
200
|
+
if (decision.want === "none")
|
|
201
|
+
for (const line of blankLines(doc, prev.endLine, next.startLine)) edits.deleteLines.add(line);
|
|
202
|
+
else edits.insertAfter.add(next.startLine - 1);
|
|
203
|
+
|
|
204
|
+
if ("name" in decision)
|
|
205
|
+
return [finding(doc, next.start, decision.rule, decision.name, decision.reader)];
|
|
206
|
+
|
|
207
|
+
return [finding(doc, next.start, decision.rule)];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function edgeEdits(
|
|
211
|
+
doc: Doc,
|
|
212
|
+
list: StatementList,
|
|
213
|
+
regions: Region[],
|
|
214
|
+
edits: LineEdits,
|
|
215
|
+
touches: (first: number, last: number) => boolean,
|
|
216
|
+
): Finding[] {
|
|
217
|
+
const { openLine, closeLine } = list;
|
|
218
|
+
if (openLine === null || closeLine === null || openLine === closeLine) return [];
|
|
219
|
+
|
|
220
|
+
const to = commentIndex(doc, list.end);
|
|
221
|
+
|
|
222
|
+
let opened = openLine;
|
|
223
|
+
let from = commentIndex(doc, list.start + 1);
|
|
224
|
+
while (from < to && lineAt(doc, doc.comments[from]!.start) === openLine) {
|
|
225
|
+
opened = lineAt(doc, doc.comments[from]!.end - 1);
|
|
226
|
+
from++;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const firstComment = from < to ? lineAt(doc, doc.comments[from]!.start) : closeLine;
|
|
230
|
+
const lastComment = from < to ? lineAt(doc, doc.comments[to - 1]!.end - 1) : opened;
|
|
231
|
+
|
|
232
|
+
const first = Math.min(list.stmts[0]?.startLine ?? closeLine, firstComment);
|
|
233
|
+
const last = Math.max(list.stmts.at(-1)?.endLine ?? opened, lastComment);
|
|
234
|
+
|
|
235
|
+
const findings: Finding[] = [];
|
|
236
|
+
for (const [after, before, side, frozen] of [
|
|
237
|
+
[opened, first, "after {", list.stmts[0]?.frozen],
|
|
238
|
+
[last, closeLine, "before }", list.stmts.at(-1)?.frozen],
|
|
239
|
+
] as const) {
|
|
240
|
+
if (frozen || !touches(after, before)) continue;
|
|
241
|
+
|
|
242
|
+
for (const line of blankLines(doc, after, before)) {
|
|
243
|
+
if (edits.deleteLines.has(line) || within(regions, doc.lineStarts[line - 1]!)) continue;
|
|
244
|
+
edits.deleteLines.add(line);
|
|
245
|
+
findings.push(finding(doc, doc.lineStarts[line - 1]!, "edge-blank", side));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return findings;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function walls(
|
|
253
|
+
doc: Doc,
|
|
254
|
+
list: StatementList,
|
|
255
|
+
gaps: Gap[],
|
|
256
|
+
touches: (first: number, last: number) => boolean,
|
|
257
|
+
): Finding[] {
|
|
258
|
+
const findings: Finding[] = [];
|
|
259
|
+
if (list.kind === "switch") return findings;
|
|
260
|
+
|
|
261
|
+
let runStart: Stmt | undefined;
|
|
262
|
+
let length = 0;
|
|
263
|
+
for (let index = 0; index <= list.stmts.length; index++) {
|
|
264
|
+
const stmt = list.stmts[index];
|
|
265
|
+
const gap = gaps[index - 1];
|
|
266
|
+
const separated =
|
|
267
|
+
gap &&
|
|
268
|
+
(touches(gap.prev.startLine, gap.next.endLine)
|
|
269
|
+
? gap.decision.want === "frozen" ||
|
|
270
|
+
gap.decision.want === "at-least-one" ||
|
|
271
|
+
(gap.decision.want === "keep" && gap.blank > 0)
|
|
272
|
+
: gap.decision.want === "frozen" || gap.blank > 0);
|
|
273
|
+
|
|
274
|
+
if (!stmt || stmt.multiline || separated) {
|
|
275
|
+
if (runStart && length >= 6 && touches(runStart.startLine, list.stmts[index - 1]!.endLine))
|
|
276
|
+
findings.push(finding(doc, runStart.start, "wall"));
|
|
277
|
+
|
|
278
|
+
runStart = undefined;
|
|
279
|
+
length = 0;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!stmt || stmt.multiline) continue;
|
|
283
|
+
|
|
284
|
+
runStart ??= stmt;
|
|
285
|
+
length++;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return findings;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function listGaps(doc: Doc, list: StatementList): Gap[] {
|
|
292
|
+
const gaps: Gap[] = [];
|
|
293
|
+
for (let index = 1; index < list.stmts.length; index++) {
|
|
294
|
+
const prev = list.stmts[index - 1]!;
|
|
295
|
+
const next = list.stmts[index]!;
|
|
296
|
+
gaps.push({
|
|
297
|
+
prev,
|
|
298
|
+
next,
|
|
299
|
+
blank: blankLines(doc, prev.endLine, next.startLine).length,
|
|
300
|
+
decision: decide(list, prev, next),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
letSteps(gaps);
|
|
305
|
+
return gaps;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export function spacing(
|
|
309
|
+
doc: Doc,
|
|
310
|
+
lists: StatementList[],
|
|
311
|
+
regions: Region[],
|
|
312
|
+
touches: (first: number, last: number) => boolean = () => true,
|
|
313
|
+
): { edits: LineEdits; findings: Finding[] } {
|
|
314
|
+
const edits: LineEdits = { deleteLines: new Set(), insertAfter: new Set() };
|
|
315
|
+
const findings: Finding[] = [];
|
|
316
|
+
for (const list of lists) {
|
|
317
|
+
const gaps = listGaps(doc, list);
|
|
318
|
+
for (const gap of gaps)
|
|
319
|
+
if (touches(gap.prev.startLine, gap.next.endLine))
|
|
320
|
+
findings.push(...gapEdits(doc, gap, edits), ...blockSpacing(doc, gap));
|
|
321
|
+
|
|
322
|
+
findings.push(
|
|
323
|
+
...edgeEdits(doc, list, regions, edits, touches),
|
|
324
|
+
...walls(doc, list, gaps, touches),
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return { edits, findings };
|
|
329
|
+
}
|