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