@willbooster/tree-sitter-java 0.0.0-placeholder.0 → 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,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Ayman Nadeem
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.
package/README.md CHANGED
@@ -1,3 +1,129 @@
1
1
  # @willbooster/tree-sitter-java
2
2
 
3
- This version is a placeholder that reserves the package name so that releases can be published from GitHub Actions with trusted publishing. Use 1.0.0 or later.
3
+ [![Test](https://github.com/WillBooster/tree-sitter-java/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-java/actions/workflows/test.yml)
4
+ [![Test rust](https://github.com/WillBooster/tree-sitter-java/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-java/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-java.svg)](https://crates.io/crates/willbooster-tree-sitter-java)
8
+
9
+ Java grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
10
+ [tree-sitter/tree-sitter-java](https://github.com/tree-sitter/tree-sitter-java). We are grateful
11
+ to its authors and contributors. This is not an official release of that project.
12
+
13
+ This fork keeps the grammar rules, the queries, and the corpus of tree-sitter-java 0.23.5, so it parses the same
14
+ language into the same node types. It ships the parser as the ABI 16 compact tables that the
15
+ [WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter) generator emits, which only that runtime loads.
16
+
17
+ ## Usage
18
+
19
+ The npm package ships `tree-sitter-java.wasm` for
20
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 1.4.3 or later, which runs in Node.js, Bun, browsers, and Cloudflare Workers. In Node.js and Bun, load the grammar from its path:
21
+
22
+ ```js
23
+ import { fileURLToPath } from 'node:url';
24
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
25
+
26
+ await Parser.init();
27
+ const parser = new Parser();
28
+ const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-java/tree-sitter-java.wasm'));
29
+ parser.setLanguage(await Language.load(wasmPath));
30
+ const tree = parser.parse('class Main { int answer() { return 42; } }\n');
31
+ ```
32
+
33
+ In browsers, serve both `.wasm` files and load them by URL (the example uses Vite's `?url` imports):
34
+
35
+ ```js
36
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
37
+ import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
38
+ import javaUrl from '@willbooster/tree-sitter-java/tree-sitter-java.wasm?url';
39
+
40
+ await Parser.init({ locateFile: () => runtimeUrl });
41
+ const parser = new Parser();
42
+ parser.setLanguage(await Language.load(javaUrl));
43
+ ```
44
+
45
+ Cloudflare Workers do not allow compiling Wasm at run time, so import both `.wasm` files as modules, with or without
46
+ Node.js compatibility:
47
+
48
+ ```js
49
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
50
+ import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
51
+ import java from '@willbooster/tree-sitter-java/tree-sitter-java.wasm';
52
+
53
+ await Parser.init({ wasmModule: runtime });
54
+ const parser = new Parser();
55
+ parser.setLanguage(await Language.load(java));
56
+ ```
57
+
58
+ The package also ships `grammar.js`, the queries in `queries/`, and the node types in `src/node-types.json`.
59
+
60
+ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-java) and on
61
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
62
+ fuzzed with:
63
+
64
+ ```toml
65
+ [dependencies]
66
+ tree-sitter = { package = "willbooster-tree-sitter", version = "1.4.3" }
67
+ tree-sitter-java = { package = "willbooster-tree-sitter-java", version = "1" }
68
+ ```
69
+
70
+ ```rust
71
+ let mut parser = tree_sitter::Parser::new();
72
+ parser.set_language(&tree_sitter_java::LANGUAGE.into())?;
73
+ ```
74
+
75
+ ## Development
76
+
77
+ ```sh
78
+ mise install
79
+ bun install --frozen-lockfile
80
+ bun run test/ci-setup # installs Chromium for the browser test
81
+ bun run build/ci
82
+ bun run test
83
+ script/parse-examples
84
+ cargo test
85
+ ```
86
+
87
+ The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
88
+ WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
89
+ not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
90
+ with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary that runs here. Run other CLI commands through it as
91
+ well (e.g. `script/tree-sitter parse File.java`).
92
+
93
+ `grammar.js` stays as upstream wrote and formatted it, apart from rule changes, so that it can be compared with
94
+ upstream's grammar; the linter and the formatter skip it.
95
+
96
+ `bun run generate` records a fresh ABI 16 generation profile from the applicable `test/corpus` cases and any Git-tracked
97
+ files in `examples/`, then generates compact parser tables. The parser also embeds metadata from `tree-sitter.json`. After changing a grammar,
98
+ `tree-sitter.json`, a corpus case, a tracked example, or the `willbooster-tree-sitter` version in `Cargo.lock` (whose
99
+ CLI generates the parser),
100
+ regenerate and commit `src/`. Stage added or removed examples with `git add -A examples` before generation so the profile uses the intended file list.
101
+ Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`, `bun run build/ci`,
102
+ and the release build regenerate the parsers before compiling them.
103
+
104
+ `bun run test` runs:
105
+
106
+ - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
107
+ - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
108
+ edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
109
+ `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits. It skips the one case that upstream's own
110
+ parser also reparses differently from a fresh parse (see `script/fuzz-corpus`);
111
+ - a check that the real-world Java files of the repositories cloned into `examples/` fail to parse exactly as listed in
112
+ `script/known-failures.txt`. The first run clones the repositories. The example
113
+ repositories are pinned to commits in `script/parse-examples`. After a grammar change or a moved pin alters that
114
+ list, `script/parse-examples` rewrites it; review its diff before committing;
115
+ - a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear
116
+ time, since consumers parse files while they are being edited. It loads the Wasm build through
117
+ @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser;
118
+ - a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
119
+ `willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
120
+ tests, the fuzzer, and the CLI on the latter;
121
+ - tests that load the Wasm build with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
122
+ Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
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 parser of its own in `.tmp/fuzz` for each run and deletes it
126
+ afterwards.
127
+
128
+ CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parser with each platform's C compiler,
129
+ and fuzzes the parser with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).