@willbooster/tree-sitter-ruby 0.0.0-stage → 1.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,129 @@
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
+ [![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)
4
+ [![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)
5
+ [![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)
6
+ [![wbfy](https://img.shields.io/badge/wbfy-20.36.6-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
7
+ [![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-ruby.svg)](https://crates.io/crates/willbooster-tree-sitter-ruby)
8
+
9
+ Ruby grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
10
+ [tree-sitter/tree-sitter-ruby](https://github.com/tree-sitter/tree-sitter-ruby). We are grateful
11
+ to its authors and contributors. This is not an official release of that project.
12
+
13
+ This fork ships upstream's grammar with a parser generated by
14
+ [WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter), whose compact ABI 16 parse tables make the
15
+ parser smaller. The scanner differs from upstream's only where upstream's code aborts the process: when a heredoc word is
16
+ longer than 255 bytes, or when the scanner state with the open heredocs would not fit the runtime's serialization
17
+ buffer, the open heredocs are not recognized and their bodies are parsed as code.
18
+
19
+ ## Usage
20
+
21
+ The npm package ships `tree-sitter-ruby.wasm` for
22
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
23
+ browsers, and Cloudflare Workers. The compact ABI 16 parser requires runtime 1.3.0 or later.
24
+
25
+ In Node.js and Bun, load the grammar from its path:
26
+
27
+ ```js
28
+ import { fileURLToPath } from 'node:url';
29
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
30
+
31
+ await Parser.init();
32
+ const parser = new Parser();
33
+ const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-ruby/tree-sitter-ruby.wasm'));
34
+ parser.setLanguage(await Language.load(wasmPath));
35
+ const tree = parser.parse('puts "Hello"\n');
36
+ ```
37
+
38
+ In browsers, load the grammar from its URL. With Vite:
39
+
40
+ ```js
41
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
42
+ import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
43
+ import rubyUrl from '@willbooster/tree-sitter-ruby/tree-sitter-ruby.wasm?url';
44
+
45
+ await Parser.init({ locateFile: () => runtimeUrl });
46
+ const parser = new Parser();
47
+ parser.setLanguage(await Language.load(rubyUrl));
48
+ ```
49
+
50
+ In Cloudflare Workers, which do not allow compiling Wasm at run time, import both `.wasm` files as modules:
51
+
52
+ ```js
53
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
54
+ import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
55
+ import ruby from '@willbooster/tree-sitter-ruby/tree-sitter-ruby.wasm';
56
+
57
+ await Parser.init({ wasmModule: runtime });
58
+ const parser = new Parser();
59
+ parser.setLanguage(await Language.load(ruby));
60
+ ```
61
+
62
+ The package also ships the queries in `queries/` and the node types in `src/node-types.json`.
63
+
64
+ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-ruby) and on
65
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
66
+ fuzzed with. The compact ABI 16 parser requires runtime 1.3.0 or later:
67
+
68
+ ```toml
69
+ [dependencies]
70
+ tree-sitter = { package = "willbooster-tree-sitter", version = "1.3.0" }
71
+ tree-sitter-ruby = { package = "willbooster-tree-sitter-ruby", version = "1" }
72
+ ```
73
+
74
+ ```rust
75
+ let mut parser = tree_sitter::Parser::new();
76
+ parser.set_language(&tree_sitter_ruby::LANGUAGE.into())?;
77
+ ```
78
+
79
+ ## Development
80
+
81
+ ```sh
82
+ mise install
83
+ bun install --frozen-lockfile
84
+ bun run build/ci
85
+ bun run test
86
+ script/parse-examples
87
+ cargo test
88
+ ```
89
+
90
+ The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
91
+ WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
92
+ not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
93
+ with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary
94
+ that runs here. Run other CLI commands through it as well (e.g. `script/tree-sitter parse file.rb`).
95
+
96
+ `bun run generate` records a fresh ABI 16 generation profile from the applicable `test/corpus` cases and Git-tracked
97
+ files in `examples/`, then generates compact parser tables. The parser also embeds metadata from `tree-sitter.json`.
98
+ After changing the grammar, `tree-sitter.json`, a corpus case, or a tracked example, regenerate and commit `src/`. Stage
99
+ added or removed examples with `git add -A examples` before generation so the profile uses the intended file list.
100
+ Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`,
101
+ `bun run build/ci`, and the release build regenerate the parser before compiling it.
102
+
103
+ `bun run test` runs:
104
+
105
+ - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
106
+ - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
107
+ edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
108
+ `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits;
109
+ - a check that the real-world Ruby files of [ruby/spec](https://github.com/ruby/spec) cloned into `examples/` fail to
110
+ parse exactly as listed in `script/known-failures.txt`. The first run clones the repository, which is pinned to a
111
+ commit in `script/parse-examples`. After a grammar change or a moved pin alters that list, `script/parse-examples`
112
+ rewrites it; review its diff before committing;
113
+ - a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear time
114
+ (ten times the lines take about ten times the CPU time, under a ceiling), since consumers parse files while they are
115
+ being edited. It loads the Wasm build through @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after
116
+ regenerating the parser;
117
+ - a check that `package.json` and `Cargo.lock` test the same runtime version (`test/unit/runtimeVersion.test.ts`);
118
+ - checks that the Wasm build parses in Chromium (`test/unit/browser/`) and in Cloudflare Workers with and without
119
+ Node.js compatibility (`test/unit/workerd.test.ts`). Run `bun run test/ci-setup` once to install Chromium.
120
+
121
+ The tests and `script/parse-examples` compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared
122
+ by every checkout; `script/fuzz-corpus` builds a per-run parser in `.tmp/fuzz` and deletes it afterwards.
123
+
124
+ CI also runs these tests on Linux arm64 and macOS, where the Rust binding compiles the parser natively, and fuzzes the
125
+ parser with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
126
+
127
+ ### References
128
+
129
+ - [AST Format of the Whitequark parser](https://github.com/whitequark/parser/blob/master/doc/AST_FORMAT.md)