@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 tree-sitter-javascript.wasm for
@willbooster/web-tree-sitter, which runs in Node.js, Bun,
browsers, and Cloudflare Workers. In Node.js and Bun, load it from the package:
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 both .wasm files and pass their URLs:
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 both .wasm files as modules, with or
without Node.js compatibility:
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, and grammar.js and the queries in queries/ for
grammars that extend this one (e.g. require('@willbooster/tree-sitter-javascript/grammar')).
In Rust, depend on the crate and on
willbooster-tree-sitter, the runtime this package is tested and
fuzzed with (the grammar also loads in the upstream tree-sitter crate 0.27, whose error recovery never ends on some
malformed input):
[]
= { = "willbooster-tree-sitter", = "1" }
= { = "willbooster-tree-sitter-javascript", = "1" }
let mut parser = new;
parser.set_language?;
Development
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):tree-sitter fuzzedits 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 of 10,000 lines takes linear 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.
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).