@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.
Files changed (50) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +163 -0
  3. package/package.json +63 -0
  4. package/src/cli.ts +820 -0
  5. package/src/compile/darwin-arm64.ts +3 -0
  6. package/src/compile/darwin-x64.ts +3 -0
  7. package/src/compile/linux-arm64-musl.ts +3 -0
  8. package/src/compile/linux-arm64.ts +3 -0
  9. package/src/compile/linux-x64-musl.ts +3 -0
  10. package/src/compile/linux-x64.ts +3 -0
  11. package/src/compiled.ts +6 -0
  12. package/src/engine/attach.ts +62 -0
  13. package/src/engine/directives.ts +154 -0
  14. package/src/engine/doc.ts +93 -0
  15. package/src/engine/edits.ts +79 -0
  16. package/src/engine/explain.ts +333 -0
  17. package/src/engine/gaps.ts +329 -0
  18. package/src/engine/hunks.ts +320 -0
  19. package/src/engine/index.ts +254 -0
  20. package/src/engine/model.ts +102 -0
  21. package/src/engine/rules.ts +103 -0
  22. package/src/engine/types.ts +37 -0
  23. package/src/files.ts +822 -0
  24. package/src/gitignore.ts +279 -0
  25. package/src/hook.ts +282 -0
  26. package/src/languages/index.ts +32 -0
  27. package/src/languages/javascript/ast.ts +199 -0
  28. package/src/languages/javascript/braces.ts +374 -0
  29. package/src/languages/javascript/config/biome.ts +112 -0
  30. package/src/languages/javascript/config/eslint.ts +128 -0
  31. package/src/languages/javascript/config/evaluate.ts +278 -0
  32. package/src/languages/javascript/config/find.ts +108 -0
  33. package/src/languages/javascript/config/glob.ts +150 -0
  34. package/src/languages/javascript/config/index.ts +48 -0
  35. package/src/languages/javascript/config/layers.ts +425 -0
  36. package/src/languages/javascript/config/module.ts +326 -0
  37. package/src/languages/javascript/config/oxlint.ts +170 -0
  38. package/src/languages/javascript/config/width.ts +715 -0
  39. package/src/languages/javascript/entry.ts +39 -0
  40. package/src/languages/javascript/facts.ts +268 -0
  41. package/src/languages/javascript/index.ts +20 -0
  42. package/src/languages/javascript/lists.ts +49 -0
  43. package/src/languages/javascript/oracle.ts +191 -0
  44. package/src/languages/javascript/parse.ts +58 -0
  45. package/src/languages/javascript/scan.ts +68 -0
  46. package/src/languages/language.ts +109 -0
  47. package/src/launcher +21 -0
  48. package/src/native-file.d.ts +4 -0
  49. package/src/step.ts +108 -0
  50. package/src/usage.ts +30 -0
@@ -0,0 +1,199 @@
1
+ import { visitorKeys, type Node } from "oxc-parser";
2
+
3
+ function isNode(value: unknown): value is Node {
4
+ return (
5
+ typeof value === "object" &&
6
+ value !== null &&
7
+ "type" in value &&
8
+ typeof value.type === "string" &&
9
+ "start" in value &&
10
+ typeof value.start === "number" &&
11
+ "end" in value &&
12
+ typeof value.end === "number"
13
+ );
14
+ }
15
+
16
+ function eachChild(node: Node, each: (key: string, child: Node) => void): void {
17
+ const keys = visitorKeys[node.type];
18
+ if (keys) {
19
+ const fields = node as unknown as Record<string, unknown>;
20
+ for (const key of keys) visitValue(key, fields[key], each);
21
+ } else
22
+ for (const [key, value] of Object.entries(node))
23
+ if (key !== "parent") visitValue(key, value, each);
24
+ }
25
+
26
+ function visitValue(key: string, value: unknown, each: (key: string, child: Node) => void): void {
27
+ if (Array.isArray(value)) {
28
+ for (const child of value) if (isNode(child)) each(key, child);
29
+ } else if (isNode(value)) each(key, value);
30
+ }
31
+
32
+ export function children(node: Node): [string, Node][] {
33
+ const result: [string, Node][] = [];
34
+ eachChild(node, (key, child) => result.push([key, child]));
35
+ return result;
36
+ }
37
+
38
+ function reverseFrom(stack: unknown[], first: number): void {
39
+ for (let left = first, right = stack.length - 1; left < right; left++, right--) {
40
+ const swapped = stack[left];
41
+ stack[left] = stack[right];
42
+ stack[right] = swapped;
43
+ }
44
+ }
45
+
46
+ export function walk(
47
+ root: Node,
48
+ enter: (node: Node, parent: Node | null) => void,
49
+ leave?: (node: Node, parent: Node | null) => void,
50
+ ): void {
51
+ const stack = [{ node: root, parent: null as Node | null, leaving: false }];
52
+ while (stack.length > 0) {
53
+ const frame = stack.pop()!;
54
+ const { node, parent } = frame;
55
+ if (frame.leaving) {
56
+ leave?.(node, parent);
57
+ continue;
58
+ }
59
+
60
+ enter(node, parent);
61
+ frame.leaving = true;
62
+ stack.push(frame);
63
+
64
+ const first = stack.length;
65
+ eachChild(node, (_key, child) => stack.push({ node: child, parent: node, leaving: false }));
66
+ reverseFrom(stack, first);
67
+ }
68
+ }
69
+
70
+ export function boundNames(root: Node): Set<string> {
71
+ const names = new Set<string>();
72
+ const stack = [root];
73
+ while (stack.length > 0) {
74
+ const node = stack.pop()!;
75
+ switch (node.type) {
76
+ case "Identifier":
77
+ names.add(node.name);
78
+ break;
79
+
80
+ case "VariableDeclaration":
81
+ for (const declaration of node.declarations.toReversed()) stack.push(declaration.id);
82
+ break;
83
+
84
+ case "ObjectPattern":
85
+ for (const property of node.properties.toReversed()) stack.push(property);
86
+ break;
87
+
88
+ case "ArrayPattern":
89
+ for (const element of node.elements.toReversed()) if (element) stack.push(element);
90
+ break;
91
+
92
+ case "Property":
93
+ stack.push(node.value);
94
+ break;
95
+
96
+ case "AssignmentPattern":
97
+ stack.push(node.left);
98
+ break;
99
+
100
+ case "RestElement":
101
+ stack.push(node.argument);
102
+ }
103
+ }
104
+
105
+ return names;
106
+ }
107
+
108
+ export function referenceChild(node: Node, key: string): boolean {
109
+ if (["id", "label", "typeAnnotation", "typeParameters", "returnType"].includes(key)) return false;
110
+ if (node.type.startsWith("TS")) return key === "expression";
111
+ if (key === "key" || (node.type === "MemberExpression" && key === "property"))
112
+ return "computed" in node && node.computed === true;
113
+
114
+ return true;
115
+ }
116
+
117
+ function lexicalNames(node: Node): string[] {
118
+ if (node.type === "VariableDeclaration") return node.kind === "var" ? [] : [...boundNames(node)];
119
+ if ((node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") && node.id)
120
+ return [node.id.name];
121
+
122
+ return [];
123
+ }
124
+
125
+ export function declared(node: Node): string[] {
126
+ switch (node.type) {
127
+ case "ForStatement":
128
+ return node.init ? lexicalNames(node.init) : [];
129
+
130
+ case "ForInStatement":
131
+ case "ForOfStatement":
132
+ return lexicalNames(node.left);
133
+
134
+ case "BlockStatement":
135
+ case "StaticBlock":
136
+ return node.body.flatMap(lexicalNames);
137
+
138
+ case "SwitchStatement":
139
+ return node.cases.flatMap((clause) => clause.consequent.flatMap(lexicalNames));
140
+
141
+ case "CatchClause":
142
+ return node.param ? [...boundNames(node.param)] : [];
143
+
144
+ case "FunctionDeclaration":
145
+ return node.params.flatMap((param) => [...boundNames(param)]);
146
+
147
+ default:
148
+ return [];
149
+ }
150
+ }
151
+
152
+ export function* references(root: Node, names: Set<string>): Generator<string> {
153
+ const stack = [{ node: root, names, binding: false }];
154
+ while (stack.length > 0) {
155
+ const { node, names, binding } = stack.pop()!;
156
+ if (node.type === "Identifier") {
157
+ if (!binding && names.has(node.name)) yield node.name;
158
+ continue;
159
+ }
160
+
161
+ if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") continue;
162
+ if (node.type === "FunctionDeclaration" && node !== root) continue;
163
+
164
+ const shadowed = declared(node);
165
+ const visible = shadowed.some((name) => names.has(name))
166
+ ? names.difference(new Set(shadowed))
167
+ : names;
168
+
169
+ if (visible.size === 0) continue;
170
+
171
+ const next = children(node);
172
+ for (let index = next.length - 1; index >= 0; index--) {
173
+ const [key, child] = next[index]!;
174
+ if (!referenceChild(node, key)) continue;
175
+
176
+ const pattern =
177
+ key === "params" ||
178
+ (binding && key !== "right" && key !== "key") ||
179
+ (node.type === "CatchClause" && key === "param");
180
+
181
+ stack.push({ node: child, names: visible, binding: pattern });
182
+ }
183
+ }
184
+ }
185
+
186
+ export const LOOP_TYPES = new Set([
187
+ "ForStatement",
188
+ "ForInStatement",
189
+ "ForOfStatement",
190
+ "WhileStatement",
191
+ "DoWhileStatement",
192
+ ]);
193
+
194
+ export const JUMP_TYPES = new Set([
195
+ "ReturnStatement",
196
+ "ThrowStatement",
197
+ "ContinueStatement",
198
+ "BreakStatement",
199
+ ]);
@@ -0,0 +1,374 @@
1
+ import type { BlockStatement, Node, Statement } from "oxc-parser";
2
+ import type { BracePass, BraceScan } from "../language.ts";
3
+ import { JUMP_TYPES, LOOP_TYPES, walk } from "./ast.ts";
4
+ import { commentIndex, finding, lineAt, nextToken, source } from "../../engine/doc.ts";
5
+ import type { OffsetEdit } from "../../engine/edits.ts";
6
+ import type { Comment, Doc } from "../../engine/model.ts";
7
+ import type { Finding } from "../../engine/types.ts";
8
+
9
+ const REMOVABLE = new Set(["ExpressionStatement", ...JUMP_TYPES, "IfStatement", ...LOOP_TYPES]);
10
+
11
+ function addOwned(owners: Map<BlockStatement, Node>, node: Node): void {
12
+ switch (node.type) {
13
+ case "IfStatement":
14
+ if (node.consequent.type === "BlockStatement") owners.set(node.consequent, node);
15
+ if (node.alternate?.type === "BlockStatement") owners.set(node.alternate, node);
16
+ return;
17
+
18
+ case "ForStatement":
19
+ case "ForInStatement":
20
+ case "ForOfStatement":
21
+ case "WhileStatement":
22
+ case "DoWhileStatement":
23
+ if (node.body.type === "BlockStatement") owners.set(node.body, node);
24
+ }
25
+ }
26
+
27
+ export function controlledBlocks(
28
+ root: Node,
29
+ enter: (node: Node, parent: Node | null) => void = () => {},
30
+ ): Map<BlockStatement, Node> {
31
+ const owners = new Map<BlockStatement, Node>();
32
+ walk(root, enter, (node) => addOwned(owners, node));
33
+ return owners;
34
+ }
35
+
36
+ function endsWithOpenIf(node: Statement, removed: ReadonlySet<BlockStatement>): boolean {
37
+ let current = node;
38
+ while (true) {
39
+ switch (current.type) {
40
+ case "IfStatement":
41
+ if (!current.alternate) return true;
42
+ current = current.alternate;
43
+ continue;
44
+
45
+ case "ForStatement":
46
+ case "ForInStatement":
47
+ case "ForOfStatement":
48
+ case "WhileStatement":
49
+ case "DoWhileStatement":
50
+ case "LabeledStatement":
51
+ case "WithStatement":
52
+ current = current.body;
53
+ continue;
54
+
55
+ case "BlockStatement":
56
+ if (!removed.has(current)) return false;
57
+ current = current.body[0]!;
58
+ continue;
59
+
60
+ default:
61
+ return false;
62
+ }
63
+ }
64
+ }
65
+
66
+ function openingEdit(doc: Doc, offset: number): OffsetEdit {
67
+ const line = lineAt(doc, offset);
68
+ const start = doc.lineStarts[line - 1]!;
69
+ const end = start + doc.lines[line - 1]!.length;
70
+ if (doc.lines[line - 1]!.trim() === "{") return { start, end: end + 1 };
71
+ if (doc.text.slice(offset + 1, end).trim() === "") {
72
+ const horizontal = /[^\S\r\n]*$/.exec(doc.text.slice(start, offset))![0].length;
73
+ return { start: offset - horizontal, end: offset + 1 };
74
+ }
75
+
76
+ return { start: offset, end: offset + (doc.text[offset + 1] === " " ? 2 : 1) };
77
+ }
78
+
79
+ function closingEdit(doc: Doc, offset: number): OffsetEdit {
80
+ const line = lineAt(doc, offset);
81
+ const start = doc.lineStarts[line - 1]!;
82
+ const end = start + doc.lines[line - 1]!.length;
83
+ if (doc.lines[line - 1]!.trim() === "}") {
84
+ const finalLine = end === doc.text.length;
85
+ return { start, end: finalLine ? end : end + 1 };
86
+ }
87
+
88
+ if (/^ \w/.test(doc.text.slice(offset + 1))) return { start: offset, end: offset + 2 };
89
+ return { start: offset - (doc.text[offset - 1] === " " ? 1 : 0), end: offset + 1 };
90
+ }
91
+
92
+ function continuation(doc: Doc, inner: Statement, block: BlockStatement): number | null {
93
+ if (source(doc, inner).endsWith(";")) return null;
94
+
95
+ const after = nextToken(doc, block.end);
96
+ const separated = /[\r\n]/.test(doc.text.slice(block.end, after));
97
+ const safe = after >= doc.text.length || (separated && !/^[([`+\-/.<;]/.test(doc.text[after]!));
98
+ return safe ? null : after;
99
+ }
100
+
101
+ function fusesIdentifiers(doc: Doc, edit: OffsetEdit): boolean {
102
+ return (
103
+ /[$\\\p{ID_Continue}\u200C\u200D]$/u.test(
104
+ doc.text.slice(Math.max(0, edit.start - 2), edit.start),
105
+ ) && /^[$\\\p{ID_Continue}\u200C\u200D]/u.test(doc.text.slice(edit.end, edit.end + 2))
106
+ );
107
+ }
108
+
109
+ export type BraceHold =
110
+ | { kind: "count"; count: number }
111
+ | { kind: "statement"; inner: Statement }
112
+ | { kind: "continues"; inner: Statement; after: number }
113
+ | { kind: "comment"; comment: Comment }
114
+ | { kind: "closing"; comment: Comment }
115
+ | { kind: "else"; inner: Statement }
116
+ | { kind: "fuse" };
117
+
118
+ export function braceHold(
119
+ doc: Doc,
120
+ block: BlockStatement,
121
+ removed: ReadonlySet<BlockStatement>,
122
+ chained = false,
123
+ ): BraceHold | null {
124
+ const inner = block.body[0];
125
+ if (block.body.length !== 1 || !inner) return { kind: "count", count: block.body.length };
126
+ if (!REMOVABLE.has(inner.type)) return { kind: "statement", inner };
127
+
128
+ const after = chained ? null : continuation(doc, inner, block);
129
+ if (after !== null) return { kind: "continues", inner, after };
130
+
131
+ for (
132
+ let index = commentIndex(doc, block.start + 1);
133
+ index < commentIndex(doc, block.end);
134
+ index++
135
+ ) {
136
+ const comment = doc.comments[index]!;
137
+ if (comment.start < inner.start || comment.end > inner.end) return { kind: "comment", comment };
138
+ }
139
+
140
+ const comment = doc.comments[commentIndex(doc, block.end)];
141
+ const line = lineAt(doc, block.end - 1);
142
+ if (
143
+ comment &&
144
+ doc.text.slice(block.end, comment.start).trim() === "" &&
145
+ lineAt(doc, comment.start) === line &&
146
+ doc.text.slice(doc.lineStarts[line - 1], block.end - 1).trim() === ""
147
+ )
148
+ return { kind: "closing", comment };
149
+
150
+ const next = nextToken(doc, block.end);
151
+ if (/^else\b/.test(doc.text.slice(next)) && endsWithOpenIf(inner, removed))
152
+ return { kind: "else", inner };
153
+
154
+ const opening = openingEdit(doc, block.start);
155
+ const closing = closingEdit(doc, block.end - 1);
156
+ if (fusesIdentifiers(doc, opening) || fusesIdentifiers(doc, closing)) return { kind: "fuse" };
157
+
158
+ return null;
159
+ }
160
+
161
+ function trailingBody(statement: Statement): Statement | null {
162
+ switch (statement.type) {
163
+ case "IfStatement":
164
+ return statement.alternate ? null : statement.consequent;
165
+
166
+ case "ForStatement":
167
+ case "ForInStatement":
168
+ case "ForOfStatement":
169
+ case "WhileStatement":
170
+ return statement.body;
171
+
172
+ default:
173
+ return null;
174
+ }
175
+ }
176
+
177
+ function closersEndAtFixedBrace(
178
+ doc: Doc,
179
+ from: number,
180
+ byClosing: ReadonlyMap<number, BlockStatement>,
181
+ known: Map<number, boolean>,
182
+ ): boolean {
183
+ const visited: number[] = [];
184
+
185
+ let at = from;
186
+ let answer: boolean | undefined;
187
+ while (answer === undefined) {
188
+ while (at < doc.text.length && /\s/.test(doc.text[at]!)) at++;
189
+ answer = known.get(at);
190
+ if (answer !== undefined) break;
191
+
192
+ if (at >= doc.text.length || (doc.text[at] === "}" && !byClosing.has(at))) answer = true;
193
+ else if (doc.text[at] !== "}") answer = false;
194
+ else visited.push(at++);
195
+ }
196
+
197
+ for (const offset of visited) known.set(offset, answer);
198
+ return answer;
199
+ }
200
+
201
+ function closesChain(
202
+ doc: Doc,
203
+ block: BlockStatement,
204
+ removed: ReadonlySet<BlockStatement>,
205
+ outerGoes: boolean,
206
+ settled: () => boolean,
207
+ codeStart: (offset: number) => number,
208
+ ): boolean {
209
+ const inner = block.body.length === 1 ? trailingBody(block.body[0]!) : null;
210
+ if (inner?.type !== "BlockStatement" || !removed.has(inner)) return false;
211
+
212
+ return (
213
+ doc.text.slice(inner.end, block.end - 1) === " " &&
214
+ codeStart(inner.end - 1) < inner.end - 1 &&
215
+ doc.text.slice(block.end, block.end + 2) === " }" &&
216
+ !outerGoes &&
217
+ settled() &&
218
+ braceHold(doc, block, removed, true) === null
219
+ );
220
+ }
221
+
222
+ export function braceEdits(doc: Doc, blocks: BlockStatement[], collapseChains: boolean): BracePass {
223
+ const edits: OffsetEdit[] = [];
224
+ const findings: Finding[] = [];
225
+ const removed = new Set<BlockStatement>();
226
+ const remove = (block: BlockStatement) => {
227
+ removed.add(block);
228
+ edits.push(openingEdit(doc, block.start), closingEdit(doc, block.end - 1));
229
+ findings.push(finding(doc, block.start, "braces"));
230
+ };
231
+
232
+ for (const block of blocks) if (braceHold(doc, block, removed) === null) remove(block);
233
+
234
+ if (!collapseChains) return { edits, findings };
235
+
236
+ const standard = new Set(removed);
237
+ const byClosing = new Map(blocks.map((block) => [block.end - 1, block]));
238
+ const known = new Map<number, boolean>();
239
+ const codeStarts = new Map<number, number>();
240
+ const codeStart = (offset: number) => {
241
+ const line = doc.lineStarts[lineAt(doc, offset) - 1]!;
242
+
243
+ let start = codeStarts.get(line);
244
+ if (start === undefined) {
245
+ const code = /[^\s{}]|\n/g;
246
+ code.lastIndex = line;
247
+
248
+ const match = code.exec(doc.text);
249
+ start = match && match[0] !== "\n" ? match.index : Infinity;
250
+ codeStarts.set(line, start);
251
+ }
252
+
253
+ return start;
254
+ };
255
+
256
+ for (const block of blocks) {
257
+ const outer = byClosing.get(block.end + 1);
258
+ const outerGoes = outer !== undefined && standard.has(outer);
259
+ const settled = () => closersEndAtFixedBrace(doc, block.end, byClosing, known);
260
+ if (!standard.has(block) && closesChain(doc, block, removed, outerGoes, settled, codeStart))
261
+ remove(block);
262
+ }
263
+
264
+ return { edits, findings };
265
+ }
266
+
267
+ function statementName(doc: Doc, inner: Statement): string {
268
+ if (inner.type === "BlockStatement") return "a nested block";
269
+ if (inner.type === "EmptyStatement") return "an empty statement";
270
+ if (inner.type === "LabeledStatement") return "a labeled statement";
271
+
272
+ const word = /^[\w$]+/.exec(source(doc, inner))?.[0];
273
+ if (word) return `a \`${word}\` statement`;
274
+
275
+ return `a ${inner.type.replaceAll(/(?<=[a-z])(?=[A-Z])/g, " ").toLowerCase()}`;
276
+ }
277
+
278
+ function holdReason(
279
+ doc: Doc,
280
+ block: BlockStatement,
281
+ hold: BraceHold,
282
+ at: (offset: number) => number,
283
+ ): string {
284
+ switch (hold.kind) {
285
+ case "count":
286
+ return hold.count === 0 ? "the body is empty" : `the body holds ${hold.count} statements`;
287
+
288
+ case "statement":
289
+ return `the body is ${statementName(doc, hold.inner)}, and only an expression, \`return\`, \`throw\`, \`break\`, \`continue\`, \`if\` or loop stands without braces`;
290
+
291
+ case "continues": {
292
+ const line = lineAt(doc, hold.after);
293
+ const end = doc.lineStarts[line - 1]! + doc.lines[line - 1]!.length;
294
+ const where =
295
+ line === lineAt(doc, block.end - 1) ? "the same line" : `line ${at(hold.after)}`;
296
+
297
+ return `\`${source(doc, hold.inner).split("\n")[0]!.trim()}\` has no semicolon, so without braces it could continue onto \`${doc.text.slice(hold.after, end).trim()}\` on ${where}`;
298
+ }
299
+
300
+ case "comment":
301
+ return `a comment on line ${at(hold.comment.start)} sits inside the braces but outside the statement`;
302
+
303
+ case "closing":
304
+ return `a comment after the closing brace on line ${at(hold.comment.start)} would move onto the next line without it`;
305
+
306
+ case "else":
307
+ return `without braces, the \`else\` on line ${at(nextToken(doc, block.end))} would attach to the \`if\` inside them`;
308
+
309
+ case "fuse":
310
+ return "removing the braces would run two words together";
311
+ }
312
+ }
313
+
314
+ export function braceScan(
315
+ doc: Doc,
316
+ blocks: BlockStatement[],
317
+ owners: Map<BlockStatement, Node>,
318
+ chains: Map<Node, Node>,
319
+ ): BraceScan {
320
+ return {
321
+ pass(touches, touched, collapseChains) {
322
+ const selected = new Set<Node>();
323
+ for (const stmt of touched) {
324
+ let node = stmt.node as Node;
325
+ while (node.type === "LabeledStatement") node = node.body;
326
+ selected.add(node);
327
+ }
328
+
329
+ return braceEdits(
330
+ doc,
331
+ blocks.filter((block) => {
332
+ const owner = owners.get(block)!;
333
+ return (
334
+ touches(lineAt(doc, block.start), lineAt(doc, block.end - 1)) ||
335
+ selected.has(chains.get(owner) ?? owner)
336
+ );
337
+ }),
338
+ collapseChains,
339
+ );
340
+ },
341
+ opening(line) {
342
+ const opening = [...owners.keys()].filter((block) => lineAt(doc, block.start) === line);
343
+ const selected =
344
+ opening.length > 0
345
+ ? opening
346
+ : [...owners]
347
+ .filter(([, owner]) => lineAt(doc, owner.start) === line)
348
+ .map(([block]) => block);
349
+
350
+ return selected.map((block) => {
351
+ const [inner] = block.body;
352
+ return {
353
+ start: block.start,
354
+ open: lineAt(doc, block.start),
355
+ close: lineAt(doc, block.end - 1),
356
+ inner:
357
+ inner && block.body.length === 1
358
+ ? [lineAt(doc, inner.start), lineAt(doc, inner.end - 1)]
359
+ : null,
360
+ };
361
+ });
362
+ },
363
+ hold(start, at) {
364
+ const survivor = [...owners.keys()].find((candidate) => candidate.start === start);
365
+ if (!survivor || !blocks.includes(survivor)) return { kind: "directive" };
366
+
367
+ const hold = braceHold(doc, survivor, new Set());
368
+ if (hold?.kind === "count")
369
+ return { kind: "inapplicable", reason: holdReason(doc, survivor, hold, at) };
370
+
371
+ return { kind: "held", reason: hold ? holdReason(doc, survivor, hold, at) : null };
372
+ },
373
+ };
374
+ }
@@ -0,0 +1,112 @@
1
+ import { existsSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { UNKNOWN, type Value, object, property } from "./evaluate.ts";
4
+ import {
5
+ type Context,
6
+ type Layer,
7
+ type Reader,
8
+ type Tree,
9
+ type Setting,
10
+ UNKNOWN_LAYER,
11
+ decided,
12
+ enforcing,
13
+ everyConfig,
14
+ excluding,
15
+ fileLayers,
16
+ levelOf,
17
+ patterns,
18
+ read,
19
+ resolved,
20
+ tree,
21
+ } from "./layers.ts";
22
+
23
+ interface BiomeFindings {
24
+ rule: Setting[];
25
+ group: Setting[];
26
+ }
27
+
28
+ function biomeSettings(
29
+ value: Value,
30
+ context: Context,
31
+ inside = "",
32
+ found: BiomeFindings = { rule: [], group: [] },
33
+ ): BiomeFindings {
34
+ if (value === UNKNOWN) found.rule.push("unknown");
35
+ else if (Array.isArray(value))
36
+ for (const item of value) biomeSettings(item, context, inside, found);
37
+ else if (object(value))
38
+ for (const key of Object.keys(value)) {
39
+ if (["extends", "overrides", "includes", "include", "ignore"].includes(key)) continue;
40
+
41
+ const item = read(value, key, context);
42
+ if (key === "useBlockStatements") found.rule.push(levelOf(item));
43
+ else if (key === "style" && inside === "rules" && typeof item === "string" && item !== "off")
44
+ found.group.push("on");
45
+ else if (key === "all" && item === true && (inside === "rules" || inside === "style"))
46
+ found.group.push("on");
47
+ else biomeSettings(item, context, key, found);
48
+ }
49
+
50
+ return found;
51
+ }
52
+
53
+ function ownLayers(value: Value, context: Context): Layer[] {
54
+ const findings = biomeSettings(value, context);
55
+ const found = findings.rule.length ? findings.rule : findings.group;
56
+ return found.length
57
+ ? [
58
+ {
59
+ reach: "all",
60
+ setting: found.every((item) => item === "off")
61
+ ? "off"
62
+ : found.includes("unknown")
63
+ ? "unknown"
64
+ : "on",
65
+ },
66
+ ]
67
+ : [];
68
+ }
69
+
70
+ const biomeTree: Tree = {
71
+ scope(value, context) {
72
+ const includes = property(value, "includes");
73
+
74
+ let reach = patterns(includes, context, "anchored");
75
+ if (includes === undefined) reach = patterns(property(value, "include"), context, "biome1");
76
+
77
+ return excluding(reach, property(value, "ignore"), context, "anchored");
78
+ },
79
+ own: ownLayers,
80
+ extend(entry, _parent, context) {
81
+ if (entry === "//") {
82
+ let dir = dirname(context.file);
83
+ while (dirname(dir) !== dir) {
84
+ dir = dirname(dir);
85
+
86
+ for (const name of biome.files) {
87
+ const file = join(dir, name);
88
+ if (existsSync(file)) return fileLayers(file, { ...context, shared: false }).layers;
89
+ }
90
+ }
91
+
92
+ return [UNKNOWN_LAYER];
93
+ }
94
+
95
+ return resolved(entry, context, "import");
96
+ },
97
+ };
98
+
99
+ export const biome: Reader = {
100
+ family: "biome",
101
+ files: ["biome.json", "biome.jsonc"],
102
+ backstop: ["useBlockStatements"],
103
+ backstopImports: false,
104
+ layers(value, context) {
105
+ return tree(value, context, biomeTree);
106
+ },
107
+ decide(dir, extension) {
108
+ const found = everyConfig(dir, extension, biome);
109
+ const nearest = found.find((item) => item.setting !== null);
110
+ return decided(biome, nearest?.root === false ? nearest : enforcing(found));
111
+ },
112
+ };