Run ast-grep in the browser
Search, lint and rewrite code by syntax-tree pattern instead of regex.
Try it live
Nothing is downloaded until you press Run.
Find console.log calls by syntax-tree pattern
Installs ast-grep 0.44.0 from the signed catalog (every chunk is verified before it is written), then runs the commands below in the VM's shell.
ast-grep --version ast-grep run --pattern 'console.log($A)' --lang js --stdin < app.js
$ ast-grep --version ast-grep 0.44.0 $ ast-grep run --pattern 'console.log($A)' --lang js --stdin < app.js STDIN:2:console.log(a); STDIN:5:console.log( a+1 );
What it is
ast-grep matches code against patterns written in the target language itself (console.log($MSG)), parsed with tree-sitter, so it finds structure rather than text: formatting, comments and string contents never cause false hits. The same pattern can drive a rewrite. Running it in the browser lets a playground, a code-review tool or a course page run real structural queries over files a visitor pastes in, without shipping their code to a server.
Package facts
- version
0.44.0- runs on
- RISC-V VM (
elf-app,riscv64gc-linux-musl) - download
- 7.9 MB compressed · 1 file
- command
ast-grep- requires
- nothing else — a static binary
- license
MIT(upstream)- upstream
- github.com/ast-grep/ast-grep
- categories
- Search, Text
- catalog
- generation 12, Ed25519-signed
Install and run it from your page
With the SDK, installApp resolves ast-grep in the signed index, fetches its content-addressed chunks from the CDN, verifies each one and writes the files into the VM. Details in Installing catalog apps.
import { createNano } from "@userland-run/nano-sdk";
const nano = await createNano({ image: { wasm: "/nano/console.wasm" }, ramMB: 512 });
await nano.installApp("ast-grep"); // signature-verified, cached in OPFS
const r = await nano.shExec("ast-grep --version");
console.log(r.stdout);Typical usage of the upstream command-line tool:
ast-grep run --pattern 'console.log($$$ARGS)' --rewrite 'logger.info($$$ARGS)' src/
Caveats
- The pattern
console.log($A)matches calls with exactly one argument, so the two-argument call and the commented-out line are not reported. - Searching a directory (
ast-grep run … src/) walks files on several threads; on NanoVM v0.2.1 that did not finish within the live sample's instruction budget in our tests, so the sample reads the file from stdin (--stdin).
Related apps
- ripgrep14.1.0Search file contents recursively with a regex, fast, respecting .gitignore.RISC-V VM9.2 MB
- sd1.0.0Find and replace text with familiar regex syntax — a simpler sed for substitutions.RISC-V VM956 kB
- fd10.2.0Find files by name with a simple syntax, smart case and sensible defaults — an alternative to find.RISC-V VM1.4 MB