# Vendored ast-grep WASM engine

This directory holds a self-built WebAssembly build of the
[ast-grep](https://github.com/ast-grep/ast-grep) matching engine
(`@ast-grep/wasm`), used by `occ code pattern` and `occ code facts --rules`.

It contains **no grammars and no native code**: the engine delegates all
parsing to `web-tree-sitter` (a peer import resolved from `node_modules`) and
consumes the same grammar `.wasm` files vendored under `vendor/tree-sitter/`.
The published `@ast-grep/wasm` npm package targets bundlers, which plain Node
ESM cannot import; this build uses wasm-pack's `web` target instead, which is.

## Provenance

| Field | Value |
| --- | --- |
| Upstream | https://github.com/ast-grep/ast-grep |
| Tag | `0.45.1` |
| Commit | `dc3d655b9edf3b2bc266d9bc46eb60f18e66b818` |
| License | MIT (see `LICENSE`, copied from the upstream tag) |
| `wasm.js` sha256 | `5bd8bd4e999dd2f566c3f0b328ee61871918925820728411c2fac612084b50cb` |
| `wasm_bg.wasm` sha256 | `532c5fb8f1a5e41de6041edd12236db5f6f202361da47a462834585b5b3b5995` |

The same digests are pinned in `src/code/ast-grep.ts`; the loader re-hashes
both files before instantiating the engine and refuses to run on a mismatch.
Updating this directory therefore requires updating those constants in the
same change.

## Build recipe

```bash
# toolchain: rustc >= 1.88, wasm32-unknown-unknown target, wasm-pack
rustup target add wasm32-unknown-unknown
cargo install wasm-pack --locked

git clone https://github.com/ast-grep/ast-grep
cd ast-grep && git checkout 0.45.1
cd crates/wasm
wasm-pack build --scope ast-grep --release --target web --out-dir pkg-web

cp pkg-web/wasm.js pkg-web/wasm_bg.wasm pkg-web/wasm.d.ts <occ>/vendor/ast-grep/
cp ../../LICENSE <occ>/vendor/ast-grep/LICENSE
shasum -a 256 <occ>/vendor/ast-grep/wasm.js <occ>/vendor/ast-grep/wasm_bg.wasm
# then update the pinned digests in src/code/ast-grep.ts and this file
```

`wasm.d.ts` is kept for reference only; the typed surface OCC uses is declared
in `src/code/ast-grep.ts`.
