prettier-plugin-julia 0.0.0-stage → 0.2.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/src/tree.js ADDED
@@ -0,0 +1,118 @@
1
+ // The full JuliaSyntax green tree, with trivia (keywords, punctuation, whitespace and
2
+ // comments) kept, as the printer needs it. Built on the vendored parser layer
3
+ // (src/parser/: Wasm host, green-tree placement, byte -> UTF-16 offsets), unmodified.
4
+ import { readWasm } from './parser/wasm-host.js';
5
+ import { placeGreen } from './parser/green-tree.js';
6
+
7
+ export const CLASS = {
8
+ INTERIOR: 1,
9
+ TRIVIA: 2,
10
+ ERROR: 4,
11
+ KEYWORD: 8,
12
+ OPERATOR: 16,
13
+ LITERAL: 32,
14
+ IDENTIFIER: 64,
15
+ WHITESPACE: 128,
16
+ };
17
+
18
+ /**
19
+ * Parse Julia source into a tree of nodes:
20
+ * { kind, flags, cls, start, end, children, text? }
21
+ * `start`/`end` are UTF-16 offsets into `text`; leaves carry their source `text`.
22
+ * TOMBSTONE leaves of width zero are dropped; everything else (whitespace, newlines,
23
+ * comments, keywords, punctuation) stays, in source order.
24
+ */
25
+ // The Wasm reader; replaceable for benchmarks and candidate parser builds (setReader).
26
+ let read = readWasm;
27
+ export function setReader(fn) {
28
+ read = fn ?? readWasm;
29
+ }
30
+
31
+ export function parseTree(text) {
32
+ const parsed = read(text);
33
+ const { nodes: raw, roots } = placeGreen(parsed.raw, parsed.bytes);
34
+ // Pure ASCII (most Julia files): byte offsets are UTF-16 offsets; skip the offset table.
35
+ const off = parsed.bytes === text.length ? asciiPositions(text) : utf8Positions(text, parsed.bytes);
36
+ const nodes = new Array(raw.length);
37
+ for (let i = 0; i < raw.length; i++) {
38
+ const o = raw[i];
39
+ const start = off.at(o.start),
40
+ end = off.at(o.end);
41
+ const n = { kind: o.kind, flags: o.flags, cls: o.class, start, end, children: [] };
42
+ if (!o.interior) n.text = text.slice(start, end);
43
+ else for (const c of o.children) if (keep(nodes[c])) n.children.push(nodes[c]);
44
+ nodes[i] = n;
45
+ }
46
+ const root = {
47
+ kind: 'toplevel-file',
48
+ flags: 0,
49
+ cls: CLASS.INTERIOR,
50
+ start: 0,
51
+ end: text.length,
52
+ children: roots.map((i) => nodes[i]).filter(keep),
53
+ };
54
+ const diagnostics = parsed.diagnostics.map((d) => ({
55
+ message: d.message,
56
+ level: d.level,
57
+ start: off.at(d.first - 1),
58
+ end: Math.max(off.at(d.first - 1), off.at(d.last)),
59
+ loc: off.loc(off.at(d.first - 1)),
60
+ }));
61
+ return { root, diagnostics, loc: off.loc };
62
+ }
63
+
64
+ /** Same mapping as the vendored `positions` (byte offset -> UTF-16 index), in one charCodeAt pass. */
65
+ export function utf8Positions(text, byteLength) {
66
+ const map = new Uint32Array(byteLength + 1);
67
+ let b = 0;
68
+ for (let i = 0; i < text.length; i++) {
69
+ const c = text.charCodeAt(i);
70
+ if (c < 0x80) {
71
+ map[b++] = i;
72
+ continue;
73
+ }
74
+ if (c >= 0xd800 && c <= 0xdbff && i + 1 < text.length) {
75
+ map[b] = map[b + 1] = map[b + 2] = map[b + 3] = i;
76
+ b += 4;
77
+ i++;
78
+ continue;
79
+ }
80
+ const w = c < 0x800 ? 2 : 3;
81
+ for (let k = 0; k < w; k++) map[b + k] = i;
82
+ b += w;
83
+ }
84
+ map[b] = text.length;
85
+ const { loc } = asciiPositions(text);
86
+ return { at: (x) => map[Math.max(0, Math.min(byteLength, x))], loc };
87
+ }
88
+
89
+ function asciiPositions(text) {
90
+ let starts = null;
91
+ const loc = (index) => {
92
+ if (!starts) {
93
+ starts = [0];
94
+ for (let i = 0; i < text.length; i++) if (text.charCodeAt(i) === 10) starts.push(i + 1);
95
+ }
96
+ let lo = 0,
97
+ hi = starts.length;
98
+ while (lo + 1 < hi) {
99
+ const mid = (lo + hi) >>> 1;
100
+ if (starts[mid] <= index) lo = mid;
101
+ else hi = mid;
102
+ }
103
+ return { line: lo + 1, column: index - starts[lo] };
104
+ };
105
+ return { at: (b) => b, loc };
106
+ }
107
+
108
+ function keep(n) {
109
+ return !(n.text !== undefined && n.start === n.end && n.kind === 'TOMBSTONE');
110
+ }
111
+
112
+ export const isLeaf = (n) => n.text !== undefined;
113
+ export const isTrivia = (n) => Boolean(n.cls & CLASS.TRIVIA);
114
+ export const isWhitespace = (n) => Boolean(n.cls & CLASS.WHITESPACE);
115
+ export const isError = (n) => Boolean(n.cls & CLASS.ERROR);
116
+ export const isComment = (n) => n.kind === 'Comment';
117
+ export const isErrorDiagnostic = (d) => d.level === 1;
118
+ export const hasErrors = ({ diagnostics }) => diagnostics.some(isErrorDiagnostic);
package/src/verify.js ADDED
@@ -0,0 +1,206 @@
1
+ // The safety net: input and output are parsed by JuliaSyntax and compared in a strict
2
+ // normal form, ported from topiary-julia's verifier (src/jlparse.rs, `sem_tree`).
3
+ //
4
+ // Dropped: trivia (whitespace, comments, punctuation and keywords implied by the node kind;
5
+ // error tokens are kept) and TOMBSTONEs. Kept: node kinds, leaf text (exact), and all flags
6
+ // except TRIVIA (bit 0), NON_TERMINAL (bit 7), TOPLEVEL_SEMICOLONS on `toplevel` (`a; b` may
7
+ // become two lines) and the trailing-comma flag (bit 9) on call-like list kinds (`f(a, b,)` ≡
8
+ // `f(a, b)` as an Expr), and on tuples of more than one element. On a one-element `tuple` the
9
+ // flag is compared: `((x, y),) -> …` takes one argument, `((x, y)) -> …` two.
10
+ // Operator and identifier leaves compare by text alone (JuliaSyntax lexes `<:` as either).
11
+ // Comments must survive in order (compared with trailing whitespace trimmed).
12
+ import { parseTree, CLASS, hasErrors, isErrorDiagnostic } from './tree.js';
13
+
14
+ const LIST_KINDS = new Set(['call', 'dotcall', 'curly', 'ref', 'vect', 'braces', 'parameters', 'macrocall']);
15
+
16
+ function semFlags(kind, flags) {
17
+ let f = flags & ~(1 | (1 << 7));
18
+ if (kind === 'toplevel') f &= ~(1 << 8);
19
+ if (LIST_KINDS.has(kind)) f &= ~(1 << 9);
20
+ return f;
21
+ }
22
+
23
+ /** Normal form: arrays of { k, f, t?, c?, n } (n = the original node, for spans). */
24
+ export function semTree(root) {
25
+ const out = [];
26
+ for (const c of root.children) push(c, '', out);
27
+ return out;
28
+ }
29
+
30
+ function push(n, parent, out) {
31
+ if (n.text !== undefined) {
32
+ const err = n.cls & CLASS.ERROR;
33
+ if ((!err && n.cls & (CLASS.TRIVIA | CLASS.WHITESPACE)) || n.kind === 'TOMBSTONE') return;
34
+ const k = n.cls & (CLASS.OPERATOR | CLASS.IDENTIFIER) ? '#id' : n.kind;
35
+ out.push({ k, f: semFlags(n.kind, n.flags), t: n.text, n });
36
+ return;
37
+ }
38
+ if (n.kind === 'toplevel' && parent === 'toplevel') {
39
+ for (const c of n.children) push(c, parent, out);
40
+ return;
41
+ }
42
+ const c = [];
43
+ for (const ch of n.children) push(ch, n.kind, c);
44
+ let f = semFlags(n.kind, n.flags);
45
+ // A tuple's trailing comma only matters with one element: `((x, y),) -> …` takes one
46
+ // argument, `((x, y)) -> …` two; `(a, b,)` ≡ `(a, b)` and `(; a=1,)` ≡ `(; a=1)`.
47
+ if (n.kind === 'tuple' && !(c.length === 1 && c[0].k !== 'parameters')) f &= ~(1 << 9);
48
+ out.push({ k: n.kind, f, c, n });
49
+ }
50
+
51
+ const show = (s) =>
52
+ s.t !== undefined
53
+ ? `${s.k === '#id' ? s.n.kind : s.k} ${JSON.stringify(s.t)}`
54
+ : `(${s.k}${s.c.map((x) => ' ' + show(x)).join('')})`;
55
+ const brief = (s) => {
56
+ const t = show(s);
57
+ return t.length > 120 ? t.slice(0, 117) + '...' : t;
58
+ };
59
+
60
+ function same(x, y) {
61
+ if (x.k !== y.k || x.f !== y.f || x.t !== y.t) return false;
62
+ if (x.c === undefined) return y.c === undefined;
63
+ if (y.c === undefined || x.c.length !== y.c.length) return false;
64
+ for (let i = 0; i < x.c.length; i++) if (!same(x.c[i], y.c[i])) return false;
65
+ return true;
66
+ }
67
+
68
+ /** First difference: { message, node } with `node` the innermost differing input node. */
69
+ export function firstDifference(a, b, parentNode = null) {
70
+ for (let i = 0; i < Math.max(a.length, b.length); i++) {
71
+ const x = a[i],
72
+ y = b[i];
73
+ if (x && !y) return { message: `missing after formatting: ${brief(x)}`, node: x.n };
74
+ if (!x && y) return { message: `added by formatting: ${brief(y)}`, node: parentNode };
75
+ if (!same(x, y)) {
76
+ if (x.k === y.k && x.f === y.f && x.c && y.c) return firstDifference(x.c, y.c, x.n);
77
+ return { message: `${brief(x)} became ${brief(y)}`, node: x.n };
78
+ }
79
+ }
80
+ return null;
81
+ }
82
+
83
+ export function commentsOf(root) {
84
+ const out = [];
85
+ const walk = (n) => {
86
+ if (n.text !== undefined) {
87
+ if (n.kind === 'Comment') out.push({ text: n.text.trimEnd(), n });
88
+ } else for (const c of n.children) walk(c);
89
+ };
90
+ walk(root);
91
+ return out;
92
+ }
93
+
94
+ /**
95
+ * Check that `output` means the same as the already-parsed input.
96
+ * Returns null when equivalent, else { message, node } (node: input node to protect, may be null).
97
+ */
98
+ export function verify(input, output) {
99
+ const b = parseTree(output);
100
+ if (hasErrors(b)) {
101
+ const d = b.diagnostics.find(isErrorDiagnostic);
102
+ // Locate it in the input through the first difference of the (error-recovered) trees.
103
+ const diff = firstDifference(semTree(input.root), semTree(b.root), null);
104
+ return {
105
+ message: `output does not parse: ${d.message} (output ${d.loc.line}:${d.loc.column + 1})`,
106
+ node: diff?.node ?? null,
107
+ };
108
+ }
109
+ // Fast path: walk both trees in normal form side by side (no intermediate trees).
110
+ if (sameStream(input.root, b.root)) return null;
111
+ const d = firstDifference(semTree(input.root), semTree(b.root), null);
112
+ if (d) return d;
113
+ const ca = commentsOf(input.root),
114
+ cb = commentsOf(b.root);
115
+ for (let i = 0; i < Math.max(ca.length, cb.length); i++) {
116
+ if (ca[i]?.text !== cb[i]?.text) {
117
+ return {
118
+ message: `comment ${i + 1}: ${JSON.stringify(ca[i]?.text ?? '(none)')} became ${JSON.stringify(cb[i]?.text ?? '(none)')}`,
119
+ node: ca[i]?.n ?? null,
120
+ };
121
+ }
122
+ }
123
+ return null;
124
+ }
125
+
126
+ // ─── Streaming comparison (the common, equal case) ─────────────────────
127
+
128
+ const skipLeaf = (n) => (!(n.cls & CLASS.ERROR) && n.cls & (CLASS.TRIVIA | CLASS.WHITESPACE)) || n.kind === 'TOMBSTONE';
129
+
130
+ /** Normal-form events of a tree, one at a time: type 'o' (open) / 'l' (leaf) / 'c' (close) / 'e' (end). */
131
+ class Cursor {
132
+ constructor(root) {
133
+ this.stack = [{ n: root, i: 0, splice: true, parent: '' }];
134
+ this.comments = [];
135
+ this.type = '';
136
+ this.k = '';
137
+ this.f = 0;
138
+ this.t = undefined;
139
+ }
140
+ next() {
141
+ for (;;) {
142
+ const top = this.stack.at(-1);
143
+ if (!top) {
144
+ this.type = 'e';
145
+ return;
146
+ }
147
+ if (top.i >= top.n.children.length) {
148
+ this.stack.pop();
149
+ if (!top.splice) {
150
+ this.type = 'c';
151
+ return;
152
+ }
153
+ continue;
154
+ }
155
+ const c = top.n.children[top.i++];
156
+ const parent = top.splice ? top.parent : top.n.kind;
157
+ if (c.text !== undefined) {
158
+ if (c.kind === 'Comment') this.comments.push(c.text.trimEnd());
159
+ if (skipLeaf(c)) continue;
160
+ this.type = 'l';
161
+ this.k = c.cls & (CLASS.OPERATOR | CLASS.IDENTIFIER) ? '#id' : c.kind;
162
+ this.f = semFlags(c.kind, c.flags);
163
+ this.t = c.text;
164
+ return;
165
+ }
166
+ if (c.kind === 'toplevel' && parent === 'toplevel') {
167
+ this.stack.push({ n: c, i: 0, splice: true, parent });
168
+ continue;
169
+ }
170
+ let f = semFlags(c.kind, c.flags);
171
+ if (c.kind === 'tuple' && !singleNonParam(c)) f &= ~(1 << 9);
172
+ this.stack.push({ n: c, i: 0, splice: false, parent });
173
+ this.type = 'o';
174
+ this.k = c.kind;
175
+ this.f = f;
176
+ this.t = undefined;
177
+ return;
178
+ }
179
+ }
180
+ }
181
+
182
+ function singleNonParam(n) {
183
+ let count = 0,
184
+ last = null;
185
+ for (const c of n.children) {
186
+ if (c.text !== undefined && skipLeaf(c)) continue;
187
+ count++;
188
+ last = c;
189
+ if (count > 1) return false;
190
+ }
191
+ return count === 1 && last.kind !== 'parameters';
192
+ }
193
+
194
+ function sameStream(a, b) {
195
+ const x = new Cursor(a),
196
+ y = new Cursor(b);
197
+ for (;;) {
198
+ x.next();
199
+ y.next();
200
+ if (x.type !== y.type || x.k !== y.k || x.f !== y.f || x.t !== y.t) return false;
201
+ if (x.type === 'e') break;
202
+ }
203
+ if (x.comments.length !== y.comments.length) return false;
204
+ for (let i = 0; i < x.comments.length; i++) if (x.comments[i] !== y.comments[i]) return false;
205
+ return true;
206
+ }
@@ -0,0 +1,26 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2009-2025: Jeff Bezanson, Stefan Karpinski, Viral B. Shah, and other contributors: https://github.com/JuliaLang/julia/contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ end of terms and conditions
25
+
26
+ Please see [THIRDPARTY.md](./THIRDPARTY.md) for license information for other software used in this project.
@@ -0,0 +1,46 @@
1
+ The JuliaSyntax.jl package is licensed under the MIT "Expat" License:
2
+
3
+ > Copyright (c) 2021 Julia Computing and contributors
4
+ >
5
+ > Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ > of this software and associated documentation files (the "Software"), to deal
7
+ > in the Software without restriction, including without limitation the rights
8
+ > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ > copies of the Software, and to permit persons to whom the Software is
10
+ > furnished to do so, subject to the following conditions:
11
+ >
12
+ > The above copyright notice and this permission notice shall be included in all
13
+ > copies or substantial portions of the Software.
14
+ >
15
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ > SOFTWARE.
22
+
23
+ The code in src/tokenize*.jl and test/tokenize.jl is derived from the Tokenize.jl
24
+ package and is also licensed under the MIT "Expat" License:
25
+
26
+ > Copyright (c) 2016: Kristoffer Carlsson.
27
+ >
28
+ > Permission is hereby granted, free of charge, to any person obtaining a copy
29
+ > of this software and associated documentation files (the "Software"), to deal
30
+ > in the Software without restriction, including without limitation the rights
31
+ > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32
+ > copies of the Software, and to permit persons to whom the Software is
33
+ > furnished to do so, subject to the following conditions:
34
+ >
35
+ > The above copyright notice and this permission notice shall be included in all
36
+ > copies or substantial portions of the Software.
37
+ >
38
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40
+ > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41
+ > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42
+ > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44
+ > SOFTWARE.
45
+ >
46
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Gabriel Baraldi and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,44 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "sha256": "189b319e4bc7f338a5b1128e5822f08f730227b2050fab28aced3b95e1f17b5d",
4
+ "bytes": 739251,
5
+ "sources": {
6
+ "parser": "https://github.com/pankgeorg/topiary-julia/blob/jlparse/jlparse/parser.jl",
7
+ "build": "https://github.com/pankgeorg/topiary-julia/blob/jlparse/jlparse/build.jl",
8
+ "snapshot": "scripts/parser.jl and scripts/build-parser.jl (vendored artifact inputs)"
9
+ },
10
+ "versions": {
11
+ "julia": "1.13.2",
12
+ "juliaSyntax": "Base.JuliaSyntax shipped in Julia 1.13.2 (JuliaSyntax e8a07f5; the same in Julia 1.13.3)",
13
+ "wasmCodegenCommit": "98cad1f",
14
+ "wasmBuilder": "0.1.1 (main 0664daf)",
15
+ "wasmOpt": "-O3 (Binaryen_jll 0.130.0, WasmCodegen default)"
16
+ },
17
+ "abi": {
18
+ "version": 1,
19
+ "exports": [
20
+ "src_resize",
21
+ "src_set",
22
+ "src_set4",
23
+ "parse_src",
24
+ "out_kind",
25
+ "out_span",
26
+ "out_nodespan",
27
+ "out_flags",
28
+ "out_class",
29
+ "ndiag",
30
+ "diag_first",
31
+ "diag_last",
32
+ "diag_level",
33
+ "diag_msg_len",
34
+ "diag_msg_byte",
35
+ "kind_name_len",
36
+ "kind_name_byte"
37
+ ]
38
+ },
39
+ "reference": "tests/fixtures/jlparse_reference.json; 21 native JuliaSyntax sources, 76494 nodes",
40
+ "notes": [
41
+ "Requires Wasm GC and exception handling; no imports.",
42
+ "Built from the vendored parser.jl and build-parser.jl (sources.snapshot), unchanged since v0.1.0, in WasmCodegen.jl's examples/juliasyntax environment."
43
+ ]
44
+ }
Binary file