@willbooster/tree-sitter-javascript
JavaScript and JSX grammar for tree-sitter, forked from tree-sitter/tree-sitter-javascript. We are grateful to its authors and contributors. This is not an official release of that project.
This fork fixes parsing bugs and raises conformance with ECMAScript (ECMA-262) and JSX.
Usage
The npm package ships two languages for
@willbooster/web-tree-sitter, which runs in Node.js, Bun,
browsers, and Cloudflare Workers. The parser carries the ABI number of WillBooster/tree-sitter (0x57420006), so it
loads only with that runtime, version 7.0.5 or newer within major 7; the upstream runtime rejects it. In Node.js and Bun, load it from
the package. Choose tree-sitter-javascript.wasm for the Module goal, or
tree-sitter-javascript_script.wasm for the Script goal. The source goal comes from the host or file type;
.mjs uses Module and .cjs uses Script, while .js depends on the host. For example, await\nvalue is one
await expression in a Module and two identifier statements in a Script. Both languages preserve the same node
schema and query names. As with other tree-sitter grammars, parsing does not enforce every ECMAScript early error.
For the native CLI, select Script with tree-sitter parse --grammar-path grammars/script.
import from 'node:url';
import from '@willbooster/web-tree-sitter';
await ;
const parser = ;
const wasmPath = ;
parser.;
const tree = parser.;
In browsers, let your bundler serve the runtime and the selected language .wasm files, then pass their URLs.
For the Script goal, replace the language import below with
@willbooster/tree-sitter-javascript/tree-sitter-javascript_script.wasm?url:
import from '@willbooster/web-tree-sitter';
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url'; // Vite
import javascriptUrl from '@willbooster/tree-sitter-javascript/tree-sitter-javascript.wasm?url';
await ;
const parser = ;
parser.;
In Cloudflare Workers, which do not allow compiling Wasm at run time, import the runtime and the selected language
.wasm files as modules, with or without Node.js compatibility. For the Script goal, replace the language import
below with @willbooster/tree-sitter-javascript/tree-sitter-javascript_script.wasm:
import from '@willbooster/web-tree-sitter';
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
import javascript from '@willbooster/tree-sitter-javascript/tree-sitter-javascript.wasm';
await ;
const parser = ;
parser.;
The package also ships the node types in src/node-types.json, the highlight, injection, locals, and tags queries in
queries/ that tree-sitter.json lists, and grammar.js for grammars that extend this one (e.g.
require('@willbooster/tree-sitter-javascript/grammar')). The crate ships tree-sitter.json and queries/ too.
In Rust, depend on the crate and the willbooster-tree-sitter runtime, 7.0.5 or newer within major 7:
[]
= { = "willbooster-tree-sitter", = "7.0.5" }
= { = "willbooster-tree-sitter-javascript", = "7" }
let mut parser = new;
parser.set_language?;
// Use LANGUAGE_SCRIPT for the Script goal.
Development
Every tree-sitter command, from generate to the tests, runs the CLI of the WillBooster/tree-sitter runtime version
locked in Cargo.lock (script/tree-sitter), whose generator and runtime have fixes that the upstream CLI lacks.
script/fork-cli downloads that CLI into .tmp/ from its GitHub Release on first use, or builds it with cargo when
the download fails or the release has no binary that runs here.
bun run generate records a fresh generation profile from the applicable test/corpus cases and Git-tracked
files in examples/, then generates compact parser tables. The parser also embeds metadata from tree-sitter.json. After changing a grammar,
tree-sitter.json, a corpus case, or a tracked example,
regenerate and commit the affected grammar's src/ directory. Stage added or removed examples with git add -A examples before generation so the profile uses the intended file list.
Profiles in .tmp/generation-profiles/ are temporary and must not be committed. bun run build-wasm, bun run build/ci,
and the release build regenerate the parsers before compiling them.
bun run test runs:
- the corpus in
test/corpus, with the native build and with the Wasm build (the first run downloads the WASI SDK); - an incremental-parsing check (
test/unit/incremental.test.ts):script/fuzz-corpusrunstree-sitter fuzz, which edits each corpus case at random, reparses it, undoes the edits, and reparses again.TREE_SITTER_SEED,TREE_SITTER_ITERATIONS, andTREE_SITTER_EDITSrun other or more edits; - a check that the real-world JavaScript files in
examples/, the checked-in ones and those of the cloned repository, fail to parse exactly as listed inscript/known-failures.txt. The first run clones the repository. The example repository is pinned to a commit inscript/parse-examples. After a grammar change or a moved pin alters that list,script/parse-examplesrewrites it; review its diff before committing; - a performance check (
test/unit/performance.test.ts) that recovering from an error on each line takes linear time (ten times the lines take about ten times the CPU time), since consumers parse files while they are being edited. It loads the Wasm build through @willbooster/web-tree-sitter, whichbun run build/cirebuilds after regenerating the parser; - checks that the Wasm build parses in Chromium (
test/unit/browser/) and in Cloudflare Workers with and without Node.js compatibility (test/unit/workers.test.ts). Runbun run test/ci-setuponce before the first run to install Chromium and, on Linux, the system libraries it needs; - a check that
package.jsonandCargo.locklock the same WillBooster/tree-sitter runtime version (test/unit/runtimeVersion.test.ts); - a check that the queries
tree-sitter.jsonlists compile against the Wasm build and are in both the npm package and the crate (test/unit/queries.test.ts).
The tests and script/parse-examples compile the parser into .tmp/tree-sitter-lib instead of the CLI's cache shared
by every checkout, and mise.toml sets TREE_SITTER_LIBDIR to it for any other command run in the checkout;
script/fuzz-corpus builds a parser of its own in .tmp/fuzz for each run and deletes it afterwards.
CI also runs these tests on Linux arm64 and macOS, where the Rust binding compiles the parser natively, and fuzzes the
parser with libFuzzer and sanitizers (.github/workflows/robustness.yml).