@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,3 @@
1
+ import addon from "../../node_modules/@oxc-parser/binding-darwin-arm64/parser.darwin-arm64.node" with { type: "file" };
2
+ import { start } from "../compiled.ts";
3
+ await start(addon);
@@ -0,0 +1,3 @@
1
+ import addon from "../../node_modules/@oxc-parser/binding-darwin-x64/parser.darwin-x64.node" with { type: "file" };
2
+ import { start } from "../compiled.ts";
3
+ await start(addon);
@@ -0,0 +1,3 @@
1
+ import addon from "../../node_modules/@oxc-parser/binding-linux-arm64-musl/parser.linux-arm64-musl.node" with { type: "file" };
2
+ import { start } from "../compiled.ts";
3
+ await start(addon);
@@ -0,0 +1,3 @@
1
+ import addon from "../../node_modules/@oxc-parser/binding-linux-arm64-gnu/parser.linux-arm64-gnu.node" with { type: "file" };
2
+ import { start } from "../compiled.ts";
3
+ await start(addon);
@@ -0,0 +1,3 @@
1
+ import addon from "../../node_modules/@oxc-parser/binding-linux-x64-musl/parser.linux-x64-musl.node" with { type: "file" };
2
+ import { start } from "../compiled.ts";
3
+ await start(addon);
@@ -0,0 +1,3 @@
1
+ import addon from "../../node_modules/@oxc-parser/binding-linux-x64-gnu/parser.linux-x64-gnu.node" with { type: "file" };
2
+ import { start } from "../compiled.ts";
3
+ await start(addon);
@@ -0,0 +1,6 @@
1
+ export async function start(addon: string): Promise<void> {
2
+ // Must precede any oxc-parser import (its loader cannot find the binding in a compiled binary), hence the dynamic import.
3
+ process.env.NAPI_RS_NATIVE_LIBRARY_PATH = addon;
4
+ const { main, systemIo } = await import("./cli.ts");
5
+ process.exitCode = main(process.argv.slice(2), systemIo());
6
+ }
@@ -0,0 +1,62 @@
1
+ import { blankLines, commentIndex, lineAt } from "./doc.ts";
2
+ import { ignored } from "./directives.ts";
3
+ import type { Doc, Item, Stmt } from "./model.ts";
4
+
5
+ function fallthroughEnd<Node>(
6
+ doc: Doc,
7
+ item: Item<Node>,
8
+ next: Item<Node> | undefined,
9
+ end: number,
10
+ ): number {
11
+ if (!item.caseBody || next?.kind !== "case") return end;
12
+
13
+ const index = commentIndex(doc, end);
14
+ const comment = doc.comments[index];
15
+ if (!comment || (doc.comments[index + 1]?.start ?? Infinity) < next.start) return end;
16
+
17
+ const directlyBetween =
18
+ lineAt(doc, comment.start) === lineAt(doc, end - 1) + 1 &&
19
+ lineAt(doc, comment.end - 1) + 1 === lineAt(doc, next.start);
20
+
21
+ return directlyBetween && /^(falls through|fallthrough)/i.test(comment.value.trimStart())
22
+ ? comment.end
23
+ : end;
24
+ }
25
+
26
+ export function attach<Node>(
27
+ doc: Doc,
28
+ items: Item<Node>[],
29
+ opener: number,
30
+ limit: number,
31
+ ): Stmt<Node>[] {
32
+ let previousEnd = opener;
33
+ return items.map((item, itemIndex) => {
34
+ const itemStartLine = lineAt(doc, item.start);
35
+ const itemEndLine = lineAt(doc, item.end - 1);
36
+ const leading = doc.comments[commentIndex(doc, previousEnd)];
37
+ const leadLine =
38
+ leading && leading.end <= item.start ? lineAt(doc, leading.start) : itemStartLine;
39
+
40
+ const detached =
41
+ leadLine !== itemStartLine && blankLines(doc, leadLine - 1, itemStartLine).length > 0;
42
+
43
+ let end = item.end;
44
+ for (let index = commentIndex(doc, item.end); index < doc.comments.length; index++) {
45
+ const comment = doc.comments[index]!;
46
+ if (comment.start >= limit || lineAt(doc, comment.start) !== itemEndLine) break;
47
+ end = Math.max(end, comment.end);
48
+ }
49
+
50
+ if (item.kind === "case") end = fallthroughEnd(doc, item, items[itemIndex + 1], end);
51
+
52
+ previousEnd = end;
53
+ return {
54
+ ...item,
55
+ frozen: ignored(doc, item.start),
56
+ startLine: detached ? itemStartLine : leadLine,
57
+ endLine: lineAt(doc, end - 1),
58
+ multiline: itemStartLine !== itemEndLine,
59
+ detached,
60
+ };
61
+ });
62
+ }
@@ -0,0 +1,154 @@
1
+ import { commentIndex, lineAt } from "./doc.ts";
2
+ import type { Comment, Doc, StatementList } from "./model.ts";
3
+
4
+ export type Region = { start: number; end: number };
5
+
6
+ type Directive = "ignore" | "off" | "on";
7
+
8
+ function directive(comment: Comment): Directive | null {
9
+ const value = comment.type === "Block" ? comment.value.replace(/^[\s*]*/, "") : comment.value;
10
+ const match = /^stanza-(ignore|off|on)(?![\p{ID_Continue}-])/u.exec(value.trim());
11
+ return match ? (match[1] as Directive) : null;
12
+ }
13
+
14
+ function directlyAbove(doc: Doc, comment: Comment, top: number): boolean {
15
+ const between = doc.text.slice(comment.end, top);
16
+ const lineStart = doc.lineStarts[lineAt(doc, comment.start) - 1]!;
17
+ return (
18
+ /^[^\S\n]*\n[^\S\n]*$/.test(between) && /^\s*$/.test(doc.text.slice(lineStart, comment.start))
19
+ );
20
+ }
21
+
22
+ export function ignored(doc: Doc, start: number): boolean {
23
+ let top = start;
24
+ for (let index = commentIndex(doc, start) - 1; index >= 0; index--) {
25
+ const comment = doc.comments[index]!;
26
+ if (!directlyAbove(doc, comment, top)) return false;
27
+ if (directive(comment) === "ignore") return true;
28
+
29
+ top = comment.start;
30
+ }
31
+
32
+ return false;
33
+ }
34
+
35
+ function enclosing(doc: Doc, containers: Region[], comment: Comment): Region {
36
+ let block: Region = { start: 0, end: doc.text.length };
37
+ for (const container of containers)
38
+ if (
39
+ container.start < comment.start &&
40
+ comment.end <= container.end &&
41
+ (container.start > block.start ||
42
+ (container.start === block.start && container.end < block.end))
43
+ )
44
+ block = container;
45
+
46
+ return block;
47
+ }
48
+
49
+ function nests(sorted: Region[]): boolean {
50
+ const open: Region[] = [];
51
+ for (const container of sorted) {
52
+ while (open.length > 0 && open.at(-1)!.end <= container.start) open.pop();
53
+ if (open.length > 0 && container.end > open.at(-1)!.end) return false;
54
+ open.push(container);
55
+ }
56
+
57
+ return true;
58
+ }
59
+
60
+ function enclosingAll(doc: Doc, containers: Region[], marks: Comment[]): Region[] {
61
+ const sorted = containers.toSorted(
62
+ (left, right) => left.start - right.start || right.end - left.end,
63
+ );
64
+
65
+ if (!nests(sorted)) return marks.map((comment) => enclosing(doc, containers, comment));
66
+
67
+ const whole: Region = { start: 0, end: doc.text.length };
68
+ const open: Region[] = [];
69
+
70
+ let next = 0;
71
+ return marks.map((comment) => {
72
+ for (; next < sorted.length && sorted[next]!.start < comment.start; next++) {
73
+ while (open.length > 0 && open.at(-1)!.end <= sorted[next]!.start) open.pop();
74
+ open.push(sorted[next]!);
75
+ }
76
+
77
+ while (open.length > 0 && open.at(-1)!.end < comment.end) open.pop();
78
+ return open.at(-1) ?? whole;
79
+ });
80
+ }
81
+
82
+ export function marks(doc: Doc): Comment[] {
83
+ return doc.comments.filter(
84
+ (comment) => directive(comment) === "off" || directive(comment) === "on",
85
+ );
86
+ }
87
+
88
+ export function regions(doc: Doc, marks: Comment[], containers: Region[]): Region[] {
89
+ if (marks.length === 0) return [];
90
+
91
+ const ranges = enclosingAll(doc, containers, marks);
92
+ const open = new Map<string, Region & { depth: number }>();
93
+ const result: Region[] = [];
94
+ for (const [index, comment] of marks.entries()) {
95
+ const range = ranges[index]!;
96
+ const key = `${range.start}:${range.end}`;
97
+ const current = open.get(key);
98
+ if (directive(comment) === "off") {
99
+ if (current) current.depth++;
100
+ else open.set(key, { start: comment.start, end: range.end, depth: 1 });
101
+
102
+ continue;
103
+ }
104
+
105
+ if (!current) continue;
106
+
107
+ current.depth--;
108
+ if (current.depth > 0) continue;
109
+
110
+ result.push({ start: current.start, end: comment.start });
111
+ open.delete(key);
112
+ }
113
+
114
+ for (const { start, end } of open.values()) result.push({ start, end });
115
+
116
+ result.sort((left, right) => left.start - right.start);
117
+
118
+ const merged: Region[] = [];
119
+ for (const region of result) {
120
+ const previous = merged.at(-1);
121
+ if (previous && region.start <= previous.end) previous.end = Math.max(previous.end, region.end);
122
+ else merged.push(region);
123
+ }
124
+
125
+ return merged;
126
+ }
127
+
128
+ export function within(regions: Region[], offset: number): boolean {
129
+ let low = 0;
130
+ let high = regions.length;
131
+ while (low < high) {
132
+ const middle = Math.floor((low + high) / 2);
133
+ if (regions[middle]!.start <= offset) low = middle + 1;
134
+ else high = middle;
135
+ }
136
+
137
+ const region = regions[low - 1];
138
+ return region !== undefined && offset < region.end;
139
+ }
140
+
141
+ export function freeze(
142
+ doc: Doc,
143
+ marked: Comment[],
144
+ lists: StatementList[],
145
+ containers: Region[],
146
+ ): { lists: StatementList[]; frozen: Region[] } {
147
+ if (marked.length === 0) return { lists, frozen: [] };
148
+
149
+ const frozen = regions(doc, marked, containers);
150
+ for (const list of lists)
151
+ for (const stmt of list.stmts) if (within(frozen, stmt.start)) stmt.frozen = true;
152
+
153
+ return { lists: lists.filter((list) => !within(frozen, list.start)), frozen };
154
+ }
@@ -0,0 +1,93 @@
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
+ }
@@ -0,0 +1,79 @@
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
+ }