@ryuugg/stanza 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +7 -5
  2. package/package.json +19 -11
  3. package/src/launcher +102 -8
  4. package/src/cli.ts +0 -684
  5. package/src/compile/darwin-arm64.ts +0 -3
  6. package/src/compile/darwin-x64.ts +0 -3
  7. package/src/compile/linux-arm64-musl.ts +0 -3
  8. package/src/compile/linux-arm64.ts +0 -3
  9. package/src/compile/linux-x64-musl.ts +0 -3
  10. package/src/compile/linux-x64.ts +0 -3
  11. package/src/compiled.ts +0 -9
  12. package/src/depth.ts +0 -372
  13. package/src/directories.ts +0 -77
  14. package/src/engine/attach.ts +0 -62
  15. package/src/engine/directives.ts +0 -157
  16. package/src/engine/doc.ts +0 -93
  17. package/src/engine/edits.ts +0 -79
  18. package/src/engine/explain.ts +0 -337
  19. package/src/engine/gaps.ts +0 -361
  20. package/src/engine/hunks.ts +0 -319
  21. package/src/engine/index.ts +0 -273
  22. package/src/engine/model.ts +0 -103
  23. package/src/engine/rules.ts +0 -104
  24. package/src/engine/types.ts +0 -38
  25. package/src/files.ts +0 -1186
  26. package/src/format.ts +0 -159
  27. package/src/gitignore.ts +0 -280
  28. package/src/hook.ts +0 -380
  29. package/src/languages/index.ts +0 -34
  30. package/src/languages/javascript/ast.ts +0 -261
  31. package/src/languages/javascript/braces.ts +0 -374
  32. package/src/languages/javascript/config/biome.ts +0 -112
  33. package/src/languages/javascript/config/eslint.ts +0 -128
  34. package/src/languages/javascript/config/evaluate.ts +0 -278
  35. package/src/languages/javascript/config/find.ts +0 -105
  36. package/src/languages/javascript/config/glob.ts +0 -149
  37. package/src/languages/javascript/config/index.ts +0 -48
  38. package/src/languages/javascript/config/layers.ts +0 -468
  39. package/src/languages/javascript/config/module.ts +0 -327
  40. package/src/languages/javascript/config/oxlint.ts +0 -170
  41. package/src/languages/javascript/config/width.ts +0 -723
  42. package/src/languages/javascript/deserializers.ts +0 -17
  43. package/src/languages/javascript/embedded.ts +0 -7
  44. package/src/languages/javascript/entry.ts +0 -42
  45. package/src/languages/javascript/facts.ts +0 -294
  46. package/src/languages/javascript/index.ts +0 -20
  47. package/src/languages/javascript/lists.ts +0 -64
  48. package/src/languages/javascript/oracle.ts +0 -191
  49. package/src/languages/javascript/oxc-raw.d.ts +0 -31
  50. package/src/languages/javascript/parse.ts +0 -131
  51. package/src/languages/javascript/raw.ts +0 -95
  52. package/src/languages/javascript/scan.ts +0 -68
  53. package/src/languages/language.ts +0 -109
  54. package/src/native-file.d.ts +0 -4
  55. package/src/pool.ts +0 -473
  56. package/src/start.ts +0 -49
  57. package/src/step.ts +0 -113
  58. package/src/usage.ts +0 -35
  59. package/src/worker.ts +0 -5
@@ -1,131 +0,0 @@
1
- import {
2
- parseSync,
3
- type Comment,
4
- type Node,
5
- type OxcError,
6
- type ParserOptions,
7
- type Program,
8
- } from "oxc-parser";
9
- import { extname } from "node:path";
10
- import type { Rejection } from "../language.ts";
11
- import { walk } from "./ast.ts";
12
-
13
- export interface Parsed {
14
- program: Program;
15
- comments: Comment[];
16
- errors: OxcError[];
17
- }
18
-
19
- const javascript: Record<string, ParserOptions> = {
20
- ".js": { lang: "jsx" },
21
- ".mjs": { lang: "jsx", sourceType: "module" },
22
- ".cjs": { lang: "jsx", sourceType: "commonjs" },
23
- };
24
-
25
- const loneCr = /\r(?!\n)/g;
26
-
27
- const RAW_AFTER_LENGTH = 500_000;
28
- const RAW_MAX_LENGTH = 1_000_000;
29
- const RAW_COLLECT_EVERY = 1_000_000;
30
-
31
- let parsedSinceCollect = 0;
32
-
33
- type Handoff =
34
- | { kind: "pending"; remaining: number; required: boolean }
35
- | { kind: "raw"; parseRaw: typeof import("./raw.ts").parseRaw }
36
- | { kind: "json" }
37
- | { kind: "unavailable"; error: Error };
38
-
39
- let handoff: Handoff | undefined;
40
-
41
- export function parse(path: string, text: string): Parsed {
42
- const options = javascript[extname(path)];
43
- const current = currentHandoff(text.length <= RAW_MAX_LENGTH ? text.length : 0);
44
- if (current.kind === "unavailable") throw current.error;
45
-
46
- if (current.kind === "raw") {
47
- collectWhileRaw(text.length);
48
-
49
- if (text.length <= RAW_MAX_LENGTH)
50
- try {
51
- return current.parseRaw(path, text, options);
52
- } catch {
53
- // The raw deserializer recurses and overflows the stack on deep or long inputs that JSON.parse handles.
54
- }
55
- }
56
-
57
- const result = parseSync(path, text, options);
58
- return { program: result.program, comments: result.comments, errors: result.errors };
59
- }
60
-
61
- function collectWhileRaw(length: number): void {
62
- parsedSinceCollect += length;
63
- if (parsedSinceCollect < RAW_COLLECT_EVERY) return;
64
-
65
- parsedSinceCollect = 0;
66
- // JSC counts the 4 GiB raw transfer buffer as live heap, so on its own it never collects the ASTs a whole repo run leaves behind.
67
- Bun.gc(true);
68
- }
69
-
70
- function currentHandoff(length: number): Handoff {
71
- handoff ??= initialHandoff();
72
- if (handoff.kind === "pending" && (handoff.remaining -= length) <= 0)
73
- handoff = loadRaw(handoff.required);
74
-
75
- return handoff;
76
- }
77
-
78
- function initialHandoff(): Handoff {
79
- const mode = process.env.STANZA_RAW_TRANSFER;
80
- if (mode === "0") return { kind: "json" };
81
- return {
82
- kind: "pending",
83
- remaining: mode === "1" ? 0 : RAW_AFTER_LENGTH,
84
- required: mode === "1",
85
- };
86
- }
87
-
88
- function loadRaw(required: boolean): Handoff {
89
- try {
90
- // A bare require is the one lazy load bun build --compile embeds; createRequire and import.meta.require fail at runtime in the binary.
91
- const { parseRaw } = require("./raw.ts") as typeof import("./raw.ts");
92
- parseRaw("probe.js", "", javascript[".js"]);
93
- return { kind: "raw", parseRaw };
94
- } catch (cause) {
95
- if (!required) return { kind: "json" };
96
-
97
- const error = new Error(`STANZA_RAW_TRANSFER=1 but raw transfer is unavailable: ${cause}`, {
98
- cause,
99
- });
100
-
101
- return { kind: "unavailable", error };
102
- }
103
- }
104
-
105
- function crLineEnding(text: string, program: Program): number | undefined {
106
- if (text.search(loneCr) < 0) return undefined;
107
-
108
- const quasis: Node[] = [];
109
- walk(program, (node) => {
110
- if (node.type === "TemplateElement") quasis.push(node);
111
- });
112
-
113
- quasis.sort((a, b) => a.start - b.start);
114
-
115
- let index = 0;
116
- for (const { index: offset } of text.matchAll(loneCr)) {
117
- while (index < quasis.length && quasis[index]!.end <= offset) index++;
118
- if (index === quasis.length || offset < quasis[index]!.start) return offset;
119
- }
120
-
121
- return undefined;
122
- }
123
-
124
- export function rejection(text: string, parsed: Parsed): Rejection | undefined {
125
- const error = parsed.errors[0];
126
- if (error) return { start: error.labels[0]?.start ?? 0, message: error.message };
127
-
128
- const cr = crLineEnding(text, parsed.program);
129
- if (cr !== undefined)
130
- return { start: cr, message: "CR line endings are not supported, left untouched" };
131
- }
@@ -1,95 +0,0 @@
1
- import type { Comment, OxcError, ParserOptions, Program } from "oxc-parser";
2
- import { getBufferOffset, parseRawSync, rawTransferSupported } from "oxc-parser/src-js/bindings.js";
3
- import {
4
- ACTIVE_SIZE,
5
- BLOCK_ALIGN,
6
- BLOCK_SIZE,
7
- BUFFER_SIZE,
8
- IS_TS_FLAG_POS,
9
- } from "oxc-parser/src-js/generated/constants.js";
10
- import { deserializerFiles } from "./deserializers.ts";
11
- import type { Parsed } from "./parse.ts";
12
-
13
- interface RawBuffer extends Uint8Array {
14
- block: Uint8Array;
15
- int32: Int32Array;
16
- float64: Float64Array;
17
- }
18
-
19
- interface Deserializer {
20
- deserialize(
21
- buffer: RawBuffer,
22
- text: string,
23
- sourceStartPos: number,
24
- sourceByteLen: number,
25
- ): { program: Program; comments: Comment[]; errors: OxcError[] };
26
- }
27
-
28
- const ALLOCATION_ATTEMPTS = 8;
29
- const MAX_SOURCE_BYTES = 1 << 30;
30
-
31
- if (!rawTransferSupported()) throw new Error("raw transfer needs a 64-bit little-endian platform");
32
-
33
- // Required by path, not by literal, so a compiled binary does not parse 330 KB of deserializers on every start.
34
- const files = deserializerFiles();
35
- const deserializeJs = (require(files.js) as Deserializer).deserialize;
36
- const deserializeTs = (require(files.ts) as Deserializer).deserialize;
37
-
38
- const encoder = new TextEncoder();
39
-
40
- let shared: RawBuffer | undefined;
41
- export function parseRaw(path: string, text: string, options?: ParserOptions): Parsed {
42
- shared ??= createBuffer();
43
-
44
- const { sourceStartPos, sourceByteLen } = writeSource(shared, text);
45
- parseRawSync(path, shared.block, sourceStartPos, sourceByteLen, options ?? {});
46
-
47
- const isJs = shared[IS_TS_FLAG_POS] === 0;
48
- const deserialize = isJs ? deserializeJs : deserializeTs;
49
- const { program, comments, errors } = deserialize(shared, text, sourceStartPos, sourceByteLen);
50
- if (isJs && program.hashbang !== null) {
51
- const { value, start, end } = program.hashbang;
52
- comments.unshift({ type: "Line", value, start, end });
53
- }
54
-
55
- return { program, comments, errors };
56
- }
57
-
58
- function writeSource(
59
- buffer: RawBuffer,
60
- text: string,
61
- ): { sourceStartPos: number; sourceByteLen: number } {
62
- const maxBytes = text.length * 3;
63
- if (maxBytes > MAX_SOURCE_BYTES) throw new Error("Source text is too long for raw transfer");
64
-
65
- const sourceStartPos = ACTIVE_SIZE - maxBytes;
66
- const target = new Uint8Array(buffer.buffer, buffer.byteOffset + sourceStartPos, maxBytes);
67
- const { read, written } = encoder.encodeInto(text, target);
68
- if (read !== text.length) throw new Error("Failed to write source text into the raw buffer");
69
-
70
- return { sourceStartPos, sourceByteLen: written };
71
- }
72
-
73
- function createBuffer(): RawBuffer {
74
- const { arrayBuffer, offset } = allocate();
75
-
76
- const buffer = new Uint8Array(arrayBuffer, offset, BUFFER_SIZE) as RawBuffer;
77
- buffer.int32 = new Int32Array(arrayBuffer, offset, BUFFER_SIZE / 4);
78
- buffer.float64 = new Float64Array(arrayBuffer, offset, BUFFER_SIZE / 8);
79
- buffer.block = new Uint8Array(arrayBuffer, offset, BLOCK_SIZE);
80
- return buffer;
81
- }
82
-
83
- // Bun caps an ArrayBuffer at 4 GiB, so the aligned block only fits some starts; the live spacer shifts each retry by 2 GiB.
84
- function allocate(): { arrayBuffer: ArrayBuffer; offset: number } {
85
- const spacers: ArrayBuffer[] = [];
86
- for (let attempt = 0; attempt < ALLOCATION_ATTEMPTS; attempt++) {
87
- const arrayBuffer = new ArrayBuffer(BLOCK_ALIGN);
88
- const offset = getBufferOffset(new Uint8Array(arrayBuffer));
89
- if (offset + BLOCK_SIZE <= BLOCK_ALIGN) return { arrayBuffer, offset };
90
-
91
- spacers.push(new ArrayBuffer(BLOCK_ALIGN / 2));
92
- }
93
-
94
- throw new Error("Failed to allocate a raw transfer buffer aligned on 4 GiB");
95
- }
@@ -1,68 +0,0 @@
1
- import type { Node, Program } from "oxc-parser";
2
- import { freeze, ignored, marks, within, type Region } from "../../engine/directives.ts";
3
- import type { Doc } from "../../engine/model.ts";
4
- import type { Layout, Scan } from "../language.ts";
5
- import { braceScan, controlledBlocks } from "./braces.ts";
6
- import { listAt, type List } from "./lists.ts";
7
-
8
- const CONTAINERS = new Set([
9
- "BlockStatement",
10
- "StaticBlock",
11
- "SwitchStatement",
12
- "SwitchCase",
13
- "ClassBody",
14
- "TSModuleBlock",
15
- ]);
16
-
17
- export function scan(doc: Doc, program: Program, width: Layout): Scan {
18
- const marked = marks(doc);
19
- const containers: Region[] = [];
20
- const escaped = new Set<Node>();
21
-
22
- const lists: List[] = [];
23
- const chains = new Map<Node, Node>();
24
- const frozenOwners = new Set<Node>();
25
-
26
- const owners = controlledBlocks(program, (node, parent) => {
27
- const list = listAt(doc, node, width);
28
- if (list) lists.push(list);
29
-
30
- if (marked.length > 0)
31
- if (
32
- parent !== null &&
33
- (escaped.has(parent) || node.start < parent.start || parent.end < node.end)
34
- )
35
- escaped.add(node);
36
- else if (CONTAINERS.has(node.type)) containers.push({ start: node.start, end: node.end });
37
-
38
- if (node.type === "IfStatement")
39
- chains.set(
40
- node,
41
- parent?.type === "IfStatement" && parent.alternate === node
42
- ? (chains.get(parent) ?? parent)
43
- : node,
44
- );
45
-
46
- if (
47
- (node.type.endsWith("Statement") && ignored(doc, node.start)) ||
48
- (parent !== null &&
49
- frozenOwners.has(parent) &&
50
- ((parent.type === "IfStatement" && parent.alternate === node) ||
51
- parent.type === "LabeledStatement"))
52
- )
53
- frozenOwners.add(node);
54
- });
55
-
56
- const { lists: active, frozen } = freeze(doc, marked, lists, containers);
57
- const blocks = [...owners]
58
- .filter(
59
- ([block, owner]) =>
60
- !frozenOwners.has(owner) &&
61
- !frozenOwners.has(block) &&
62
- !within(frozen, block.start) &&
63
- !within(frozen, block.end - 1),
64
- )
65
- .map(([block]) => block);
66
-
67
- return { lists: active, frozen, braces: braceScan(doc, blocks, owners, chains) };
68
- }
@@ -1,109 +0,0 @@
1
- import type { Region } from "../engine/directives.ts";
2
- import type { OffsetEdit } from "../engine/edits.ts";
3
- import type { Comment, Doc, StatementList, Stmt } from "../engine/model.ts";
4
- import type { Finding } from "../engine/types.ts";
5
-
6
- export interface Rejection {
7
- start: number;
8
- message: string;
9
- }
10
-
11
- export interface Parsed {
12
- comments: Comment[];
13
- error: Rejection | undefined;
14
- rejection(text: string): Rejection | undefined;
15
- scan(doc: Doc, width: Layout): Scan;
16
- }
17
-
18
- export type WidthSource =
19
- | { kind: "config"; file: string }
20
- | { kind: "default"; formatter: "oxfmt" | "prettier" | "biome" }
21
- | { kind: "fallback" };
22
-
23
- export interface Width {
24
- columns: number;
25
- tab: number;
26
- source: WidthSource;
27
- unread: string[];
28
- }
29
-
30
- export type Layout = Pick<Width, "columns" | "tab">;
31
- export const FALLBACK_WIDTH: Width = {
32
- columns: 80,
33
- tab: 2,
34
- source: { kind: "fallback" },
35
- unread: [],
36
- };
37
-
38
- export interface Scan {
39
- lists: StatementList[];
40
- frozen: Region[];
41
- braces: BraceScan | undefined;
42
- }
43
-
44
- export type Touches = (first: number, last: number) => boolean;
45
-
46
- export interface BracePass {
47
- edits: OffsetEdit[];
48
- findings: Finding[];
49
- }
50
-
51
- export interface Opening {
52
- start: number;
53
- open: number;
54
- close: number;
55
- inner: [number, number] | null;
56
- }
57
-
58
- export type Hold =
59
- | { kind: "directive" }
60
- | { kind: "inapplicable"; reason: string }
61
- | { kind: "held"; reason: string | null };
62
-
63
- export interface BraceScan {
64
- pass(touches: Touches, touched: ReadonlySet<Stmt>, collapseChains: boolean): BracePass;
65
- opening(line: number): Opening[];
66
- hold(start: number, at: (offset: number) => number): Hold;
67
- }
68
-
69
- export interface ConfigArm {
70
- width(path: string): Width;
71
- setting(dir: string, extension: string): { enforced: boolean; unread: string[] };
72
- decisions(
73
- dir: string,
74
- extension: string,
75
- ): { setting: "on" | "off" | "unknown"; files: string[] }[];
76
- }
77
-
78
- export interface OracleStatement {
79
- start: number;
80
- clause: boolean;
81
- skeleton(): string;
82
- gaps(): [string | undefined, string | undefined];
83
- }
84
-
85
- export interface OracleSide {
86
- comments: Comment[];
87
- blocks: { start: number; end: number; single: boolean }[];
88
- shape(): string | undefined;
89
- container(comment: Comment): Region;
90
- statementAfter(offset: number): OracleStatement | undefined;
91
- }
92
-
93
- export interface Oracle {
94
- side(path: string, text: string): OracleSide;
95
- }
96
-
97
- export interface Language {
98
- parse(path: string, text: string): Parsed;
99
- config?: ConfigArm;
100
- oracle(): Oracle;
101
- }
102
-
103
- export interface Entry {
104
- id: string;
105
- extensions: readonly string[];
106
- directories: ReadonlySet<string>;
107
- skipsName(name: string, keepGenerated: boolean): boolean;
108
- load(): Language;
109
- }
@@ -1,4 +0,0 @@
1
- declare module "*.node" {
2
- const path: string;
3
- export default path;
4
- }