@willbooster/tree-sitter-dart 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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kasper Overgård Nielsen (kasper@byolimit.com)
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.
package/README.md CHANGED
@@ -1,3 +1,143 @@
1
- # Temporary Holding Version
1
+ # @willbooster/tree-sitter-dart
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-dart.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-dart)
4
+ [![license](https://img.shields.io/npm/l/@willbooster/tree-sitter-dart.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-dart)
5
+ [![Test](https://github.com/WillBooster/tree-sitter-dart/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-dart/actions/workflows/test.yml)
6
+ [![Test rust](https://github.com/WillBooster/tree-sitter-dart/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-dart/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-dart.svg)](https://crates.io/crates/willbooster-tree-sitter-dart)
10
+
11
+ Dart grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
12
+ [nielsenko/tree-sitter-dart](https://github.com/nielsenko/tree-sitter-dart). We are grateful
13
+ to its authors and contributors. This is not an official release of that project.
14
+
15
+ This fork keeps the grammar and the external scanner of upstream's version 0.2.0 and generates the parser with the
16
+ [WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter) generator. The parser carries that fork's own ABI number (`0x57420001`), so it loads only with the fork's runtime,
17
+ version 2.0.0 or later; upstream's runtime rejects it, and the fork's runtime rejects parsers generated by upstream's CLI.
18
+
19
+ ## Differences from upstream's parser
20
+
21
+ Valid Dart parses to the same trees as with upstream's 0.2.0 parser. Two differences are known:
22
+
23
+ - Error recovery on invalid input can differ, because the WillBooster/tree-sitter generator extracts keywords per
24
+ lexer state. Of 13,595 real-world files (flutter/flutter, dart-lang/core, rrousselGit/riverpod, and part of the Dart
25
+ SDK), 3 files that are invalid on purpose recover into a different tree, as do 2 of 10,197 truncated files; the error
26
+ flag is the same in every case. Their minimal inputs are `void main() {\n print(\n "foo" !==\n null);`,
27
+ `test_nullAssert_insideCast(x) {\n switch (x) {\n case _! as num?:`, and
28
+ ` final v3 = (buffer.(p) {\n return p == buffer;`.
29
+ - A NUL character in a line comment or between tokens parses as ordinary text, where upstream's lexer reports an error.
30
+
31
+ ## Usage
32
+
33
+ The npm package ships `tree-sitter-dart.wasm` for
34
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 2.0.0 or later, which runs in Node.js, Bun, browsers, and Cloudflare Workers. In Node.js and Bun, load the grammar from its path:
35
+
36
+ ```js
37
+ import { fileURLToPath } from 'node:url';
38
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
39
+
40
+ await Parser.init();
41
+ const parser = new Parser();
42
+ const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-dart/tree-sitter-dart.wasm'));
43
+ parser.setLanguage(await Language.load(wasmPath));
44
+ const tree = parser.parse('void main() {}\n');
45
+ ```
46
+
47
+ In browsers, serve both `.wasm` files and load them by URL (the example uses Vite's `?url` imports):
48
+
49
+ ```js
50
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
51
+ import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
52
+ import grammarUrl from '@willbooster/tree-sitter-dart/tree-sitter-dart.wasm?url';
53
+
54
+ await Parser.init({ locateFile: () => runtimeUrl });
55
+ const parser = new Parser();
56
+ parser.setLanguage(await Language.load(grammarUrl));
57
+ ```
58
+
59
+ Cloudflare Workers do not allow compiling Wasm at run time, so import both `.wasm` files as modules, with or without
60
+ Node.js compatibility:
61
+
62
+ ```js
63
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
64
+ import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
65
+ import grammar from '@willbooster/tree-sitter-dart/tree-sitter-dart.wasm';
66
+
67
+ await Parser.init({ wasmModule: runtime });
68
+ const parser = new Parser();
69
+ parser.setLanguage(await Language.load(grammar));
70
+ ```
71
+
72
+ The package also ships `grammar.js`, the queries in `queries/`, and the node types in `src/node-types.json`.
73
+
74
+ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-dart) and on
75
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
76
+ fuzzed with:
77
+
78
+ ```toml
79
+ [dependencies]
80
+ tree-sitter = { package = "willbooster-tree-sitter", version = "2.0.0" }
81
+ tree-sitter-dart = { package = "willbooster-tree-sitter-dart", version = "1" }
82
+ ```
83
+
84
+ ```rust
85
+ let mut parser = tree_sitter::Parser::new();
86
+ parser.set_language(&tree_sitter_dart::LANGUAGE.into())?;
87
+ ```
88
+
89
+ ## Development
90
+
91
+ ```sh
92
+ mise install
93
+ bun install --frozen-lockfile
94
+ bun run test/ci-setup # installs Chromium for the browser test
95
+ bun run build/ci
96
+ bun run test
97
+ script/parse-examples
98
+ cargo test
99
+ ```
100
+
101
+ The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
102
+ WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
103
+ not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
104
+ 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
105
+ well (e.g. `script/tree-sitter parse --grammar-path . file.dart`).
106
+
107
+ `bun run generate` records a fresh generation profile from the applicable `test/corpus` cases, then generates
108
+ compact parser tables. It would also profile files tracked in `examples/`, but this repository tracks none: the
109
+ directory holds only the clones `script/parse-examples` makes. The parser also embeds metadata from `tree-sitter.json`.
110
+ After changing a grammar, `tree-sitter.json`, or a corpus case, regenerate and commit `src/`.
111
+ Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`, `bun run build/ci`,
112
+ and the release build regenerate the parsers before compiling them.
113
+
114
+ `bun run test` runs:
115
+
116
+ - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
117
+ - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
118
+ edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
119
+ `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits;
120
+ - a check that the real-world Dart files in `examples/` fail to parse exactly as listed in
121
+ `script/known-failures.txt`, which is empty now. The first run clones the repositories. The example repositories are pinned to commits in
122
+ `script/parse-examples`. After a grammar change or a moved pin alters that list, `script/parse-examples` rewrites it;
123
+ review its diff before committing;
124
+ - a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear
125
+ time, since consumers parse files while they are being edited. It loads the Wasm build through
126
+ @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser;
127
+ - a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
128
+ `willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
129
+ tests, the fuzzer, and the CLI on the latter;
130
+ - tests that load the Wasm build with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
131
+ Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
132
+
133
+ The tests and `script/parse-examples` compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared
134
+ by every checkout; `script/fuzz-corpus` builds a parser of its own in `.tmp/fuzz` for each run and deletes it
135
+ afterwards.
136
+
137
+ CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parser and the scanner with the platform's
138
+ C compiler as the crate does for its users, and fuzzes the parser with libFuzzer and sanitizers
139
+ (`.github/workflows/robustness.yml`).
140
+
141
+ ### References
142
+
143
+ - [Dart language specification](https://dart.dev/guides/language/spec)