@willbooster/tree-sitter-ruby 0.0.0-stage → 2.0.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.
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Rob Rix
4
+ Copyright (c) 2026 WillBooster Inc.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+
package/README.md CHANGED
@@ -1,3 +1,132 @@
1
- # Temporary Holding Version
1
+ # @willbooster/tree-sitter-ruby
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm version](https://img.shields.io/npm/v/@willbooster/tree-sitter-ruby.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-ruby)
4
+ [![license](https://img.shields.io/npm/l/@willbooster/tree-sitter-ruby.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-ruby)
5
+ [![Test](https://github.com/WillBooster/tree-sitter-ruby/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-ruby/actions/workflows/test.yml)
6
+ [![Test rust](https://github.com/WillBooster/tree-sitter-ruby/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-ruby/actions/workflows/test-rust.yml)
7
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
8
+ [![wbfy](https://img.shields.io/badge/wbfy-20.36.11-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
9
+ [![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-ruby.svg)](https://crates.io/crates/willbooster-tree-sitter-ruby)
10
+
11
+ Ruby grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
12
+ [tree-sitter/tree-sitter-ruby](https://github.com/tree-sitter/tree-sitter-ruby). We are grateful
13
+ to its authors and contributors. This is not an official release of that project.
14
+
15
+ This fork ships upstream's grammar with a parser generated by
16
+ [WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter), whose compact parse tables make the
17
+ parser smaller. The parser carries that fork's own ABI number (`0x57420001`), so it loads only with the WillBooster
18
+ runtime, version 2.0.0 or later; upstream's tree-sitter runtime rejects it. The scanner differs from upstream's only where upstream's code aborts the process: when a heredoc word is
19
+ longer than 255 bytes, or when the scanner state with the open heredocs would not fit the runtime's serialization
20
+ buffer, the open heredocs are not recognized and their bodies are parsed as code.
21
+
22
+ ## Usage
23
+
24
+ The npm package ships `tree-sitter-ruby.wasm` for
25
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
26
+ browsers, and Cloudflare Workers. The parser requires runtime 2.0.0 or later.
27
+
28
+ In Node.js and Bun, load the grammar from its path:
29
+
30
+ ```js
31
+ import { fileURLToPath } from 'node:url';
32
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
33
+
34
+ await Parser.init();
35
+ const parser = new Parser();
36
+ const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-ruby/tree-sitter-ruby.wasm'));
37
+ parser.setLanguage(await Language.load(wasmPath));
38
+ const tree = parser.parse('puts "Hello"\n');
39
+ ```
40
+
41
+ In browsers, load the grammar from its URL. With Vite:
42
+
43
+ ```js
44
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
45
+ import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
46
+ import rubyUrl from '@willbooster/tree-sitter-ruby/tree-sitter-ruby.wasm?url';
47
+
48
+ await Parser.init({ locateFile: () => runtimeUrl });
49
+ const parser = new Parser();
50
+ parser.setLanguage(await Language.load(rubyUrl));
51
+ ```
52
+
53
+ In Cloudflare Workers, which do not allow compiling Wasm at run time, import both `.wasm` files as modules:
54
+
55
+ ```js
56
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
57
+ import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
58
+ import ruby from '@willbooster/tree-sitter-ruby/tree-sitter-ruby.wasm';
59
+
60
+ await Parser.init({ wasmModule: runtime });
61
+ const parser = new Parser();
62
+ parser.setLanguage(await Language.load(ruby));
63
+ ```
64
+
65
+ The package also ships the queries in `queries/` and the node types in `src/node-types.json`.
66
+
67
+ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-ruby) and on
68
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
69
+ fuzzed with. The parser requires runtime 2.0.0 or later:
70
+
71
+ ```toml
72
+ [dependencies]
73
+ tree-sitter = { package = "willbooster-tree-sitter", version = "2.0.0" }
74
+ tree-sitter-ruby = { package = "willbooster-tree-sitter-ruby", version = "2" }
75
+ ```
76
+
77
+ ```rust
78
+ let mut parser = tree_sitter::Parser::new();
79
+ parser.set_language(&tree_sitter_ruby::LANGUAGE.into())?;
80
+ ```
81
+
82
+ ## Development
83
+
84
+ ```sh
85
+ mise install
86
+ bun install --frozen-lockfile
87
+ bun run build/ci
88
+ bun run test
89
+ script/parse-examples
90
+ cargo test
91
+ ```
92
+
93
+ The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
94
+ WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
95
+ not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
96
+ with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary
97
+ that runs here. Run other CLI commands through it as well (e.g. `script/tree-sitter parse file.rb`).
98
+
99
+ `bun run generate` records a fresh generation profile from the applicable `test/corpus` cases and Git-tracked
100
+ files in `examples/`, then generates compact parser tables. The parser also embeds metadata from `tree-sitter.json`.
101
+ After changing the grammar, `tree-sitter.json`, a corpus case, or a tracked example, regenerate and commit `src/`. Stage
102
+ added or removed examples with `git add -A examples` before generation so the profile uses the intended file list.
103
+ Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`,
104
+ `bun run build/ci`, and the release build regenerate the parser before compiling it.
105
+
106
+ `bun run test` runs:
107
+
108
+ - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
109
+ - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
110
+ edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
111
+ `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits;
112
+ - a check that the real-world Ruby files of [ruby/spec](https://github.com/ruby/spec) cloned into `examples/` fail to
113
+ parse exactly as listed in `script/known-failures.txt`. The first run clones the repository, which is pinned to a
114
+ commit in `script/parse-examples`. After a grammar change or a moved pin alters that list, `script/parse-examples`
115
+ rewrites it; review its diff before committing;
116
+ - a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear time
117
+ (ten times the lines take about ten times the CPU time, under a ceiling), since consumers parse files while they are
118
+ being edited. It loads the Wasm build through @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after
119
+ regenerating the parser;
120
+ - a check that `package.json` and `Cargo.lock` test the same runtime version (`test/unit/runtimeVersion.test.ts`);
121
+ - checks that the Wasm build parses in Chromium (`test/unit/browser/`) and in Cloudflare Workers with and without
122
+ Node.js compatibility (`test/unit/workerd.test.ts`). Run `bun run test/ci-setup` once to install Chromium.
123
+
124
+ The tests and `script/parse-examples` compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared
125
+ by every checkout; `script/fuzz-corpus` builds a per-run parser in `.tmp/fuzz` and deletes it afterwards.
126
+
127
+ CI also runs these tests on Linux arm64 and macOS, where the Rust binding compiles the parser natively, and fuzzes the
128
+ parser with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
129
+
130
+ ### References
131
+
132
+ - [AST Format of the Whitequark parser](https://github.com/whitequark/parser/blob/master/doc/AST_FORMAT.md)