r/rust • u/HerringtonDarkholme • May 11 '23
Meet ast-grep: a Rust-based tool for code searching, linting, rewriting using AST
Hey folks! I'm writing a tool like a hybrid of grep, eslint and codemod, for almost all tree-sitter compatile languages. The front page is https://ast-grep.github.io/.
It is basically a tool to find and replace code with abstract syntax trees, like `sed` on steroids. If you need to rewrite your codebase in a mechanical manner, like upgrading a library or API breaking change, it will be very helpful.
I know there are plenty of similar products around but ast-grep focuses on being lightweight and user-friendly. You can write a pattern for search/replace in almost no time. ast-grep also provides an interactive editing experience for you to review code changes and refine patterns. The website also has a playground for trying out.
I implemented it in Rust and it is quite a breeze to write a command line tool in the language. Thanks to the Rust community, I used a lot of high-quality crates like ignore, codespan-reporting and inquire. These crates helped me to build high-performance and beautiful software. I attached some screenshots of ast-grep cli.



If you are interested the code is here: https://github.com/ast-grep/ast-grep
67
u/lebensterben May 11 '23
this has steeper learning curve because pattern must be valid lexical item.
more examples are needed for users to truly understand how powerful it could be.
17
u/Soft_Donkey_1045 May 11 '23
What programming languages are supported?
9
u/ravnmads May 11 '23
This looks pretty cool. Def gonna try it out.
9
u/polazarusphd May 11 '23
How does it relate to the much older semgrep?
Even if the made in Rust is a plus, a comparison to the state of the art would be appreciated.
9
u/Strum355 May 11 '23
How does it compare to https://github.com/comby-tools/comby, which can work on languages it doesnt even have grammars for (to a degree)
6
u/KillcoDer May 11 '23
This is incredible. I'm a big believer in using AST based tools to provide migrations during breaking changes where possible.
I see that there's a playground, is there a WASM based release available for use in web environments when NodeJS isn't appropriate?
Can the playground show replacement previews?
Does it support JSX / TSX?
4
u/anxxa May 11 '23
I use weggli quite frequently for security analysis. Are there any plans for friendly competition in terms of equivalent features (
not:, subexpressions, etc.).4
u/Totally_Joking May 11 '23
Awesome project!
I hope the rust community takes advantage of code mods / automated API updates.
An example that comes to mind is applying ast-grep to bevy migrations. https://bevyengine.org/learn/migration-guides/0.9-0.10/
2
u/[deleted] May 11 '23
Love command line tools that are language aware. This is a great utility to have!
2
u/joachimmartensson May 11 '23 edited May 11 '23
This looks really nice,
I have been building a more specific tool for a type migration problem (Java) written using tree-sitter (python-bindings). But this looks like it might solve my problems .
I read the guide.
Basically I want to wrap and unwrap certain types like:
Both of these seems to be solvable by this tool.
I cannot find if I can "chain" rule-fix combinations.
A lookup table
Is this possible somehow? Also is there a debug mode, when writing my tool I found it very useful to see the tree-sitter output that the queries generated.
26
u/John0x1c May 11 '23
Thanks for the great work :)
Looks awesome
A proper integration for vs code would be extremely appreciated. Something like the find&replace but with ast-grep
3
u/wholesome_hug_bot May 11 '23
Is this partly grep + tree-sitter? Because I was planning to write a ripgrep fork with TS-backed parsing.
1
u/HerringtonDarkholme May 11 '23
Thank you, Reddit! You are giving me valuable suggestions and feedback! The rust subreddit is the most active community 👍🏻
5
u/pielover928 May 11 '23
This looks awesome; I would definitely recommend changing the command to something other than
sg, though. Honestly I thinkastgrepwould be great.4
u/kredditacc96 May 11 '23
I wanted to try out your tool. Unfortunately,
sgis owned byshadowwhich is required byadduser,git, andutil-linux.4
u/[deleted] May 11 '23
What's the elevator pitch of why someone would use this instead of existing tools that do similar things?
1
u/nav_s15 May 12 '23
Like semgrep?
1
u/jackerhack Nov 23 '23
I'm trying this out in the playground and finding it hard to write a pattern that matches two adjacent lines of Python code. For example, this candidate for use of the walrus operator:
This pattern doesn't work to match both lines, but each individual line works in isolation:
What am I doing wrong? The YAML syntax has the same problem. It only matches if given a single pattern.
2
u/JoshTriplett rust · lang · libs · cargo May 11 '23
This is incredible. I've wanted a tool like this for years, just to do search or search-and-replace. And it has lints and fixes, as a bonus!
0
u/One808 May 11 '23
Perhaps the error reporting could be improved a little to tell me what's actually missing?