https://github.com/ast-grep/ast-grep Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} ast-grep / ast-grep Public * * Notifications * Fork 80 * Star 3.8k [?]A CLI tool for code structural search, lint and rewriting. Written in Rust ast-grep.github.io/ License MIT license 3.8k stars 80 forks Activity Star Notifications * Code * Issues 50 * Pull requests 12 * Discussions * Actions * Projects 2 * Wiki * Security * Insights Additional navigation options * Code * Issues * Pull requests * Discussions * Actions * Projects * Wiki * Security * Insights ast-grep/ast-grep This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 11 branches 73 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/a] Use Git or checkout with SVN using the web URL. [gh repo clone ast-gr] Work fast with our official CLI. Learn more about the CLI. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @HerringtonDarkholme HerringtonDarkholme feat: support golang generic syntax ... 3c1e4ba Dec 9, 2023 feat: support golang generic syntax update tree-sitter-golang's version 3c1e4ba Git stats * 1,739 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .cargo chore: add xtask scripts January 8, 2023 13:12 .github Revert "fix: try fix yarn version issue" November 20, 2023 01:25 benches chore(deps): update dependency @types/node to v20.10.3 December 3, 2023 20:55 crates feat: support golang generic syntax December 9, 2023 09:53 npm 0.14.2 December 2, 2023 23:15 schemas fix: obvious typo November 27, 2023 09:02 xtask fix: fix schema test December 1, 2023 21:58 .editorconfig style: remove final new line November 5, 2023 06:17 .gitignore fix: cleanup gitignore October 9, 2023 07:37 .pre-commit-config.yaml [chore] add pre-commit August 6, 2022 20:56 CHANGELOG.md 0.14.2 December 2, 2023 23:15 Cargo.lock feat: support golang generic syntax December 9, 2023 09:53 Cargo.toml 0.14.2 December 2, 2023 23:15 LICENSE [docs] add license September 8, 2022 12:15 README.md Update README.md June 30, 2023 16:16 pyproject.toml 0.14.2 December 2, 2023 23:15 renovate.json chore: update renovate.json November 15, 2022 16:44 rust-toolchain [fix] specify rust toolchain September 19, 2022 11:55 rustfmt.toml [feat] add napi support August 29, 2022 01:03 View code [ ] ast-grep(sg) Introduction Demo Installation Command line usage example Example Sponsor Feature Highlight CLI Screenshot Search README.md ast-grep coverage badge [6874747073] Discord Badge Badge GitHub Sponsors ast-grep(sg) ast-grep(sg) is a CLI tool for code structural search, lint, and rewriting. Introduction ast-grep is a AST-based tool to search code by pattern code. Think it as your old-friend grep but it matches AST nodes instead of text. You can write patterns as if you are writing ordinary code. It will match all code that has the same syntactical structure. You can use $ sign + upper case letters as wildcard, e.g. $MATCH, to match any single AST node. Think it as REGEX dot ., except it is not textual. Try the online playground for a taste! Demo output Installation You can install it from npm, pip, cargo, homebrew or scoop! npm install --global @ast-grep/cli pip install ast-grep-cli cargo install ast-grep # install via homebrew, thank @henryhchchc brew install ast-grep # install via scoop, thank @brian6932 scoop install main/ast-grep Or you can build ast-grep from source. You need install rustup, clone the repository and then cargo install --path ./crates/cli Packages are available on other platforms too. Command line usage example ast-grep has following form. sg --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts Example * Rewrite code in null coalescing operator sg -p '$A && $A()' -l ts -r '$A?.()' * Rewrite Zodios sg -p 'new Zodios($URL, $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i * Implement eslint rule using YAML. Sponsor Sponsors If you find ast-grep interesting and useful for your work, please buy me a coffee so I can spend more time on the project! Feature Highlight ast-grep's core is an algorithm to search and replace code based on abstract syntax tree produced by tree-sitter. It can help you to do lightweight static analysis and massive scale code manipulation in an intuitive way. Key highlights: * An intuitive pattern to find and replace AST. ast-grep's pattern looks like ordinary code you would write every day. (You can call the pattern is isomorphic to code). * jQuery like API for AST traversal and manipulation. * YAML configuration to write new linting rules or code modification. * Written in compiled language, with tree-sitter based parsing and utilizing multiple cores. * Beautiful command line interface :) ast-grep's vision is to democratize abstract syntax tree magic and to liberate one from cumbersome AST programming! * If you are an open source library author, ast-grep can help your library users adopt breaking changes more easily. * if you are a tech lead in your team, ast-grep can help you enforce code best practice tailored to your business need. * If you are a security researcher, ast-grep can help you write rules much faster. CLI Screenshot Search Feature Command Screenshot Search sg -p 'Some($A)' -l rs image Rewrite sg -p '$F && $F($$$ARGS)' -r '$F?.($$$ARGS)' -l ts image Report sg scan image About [?]A CLI tool for code structural search, lint and rewriting. Written in Rust ast-grep.github.io/ Topics refactoring search rust tree-sitter babel typescript command-line linter static-analysis ast codemod command-line-tool grep codereview structural-search Resources Readme License MIT license Activity Stars 3.8k stars Watchers 11 watching Forks 80 forks Report repository Releases 70 0.14.2 Latest Dec 3, 2023 + 69 releases Sponsor this project Sponsor Learn more about GitHub Sponsors Packages 0 No packages published Contributors 29 * @HerringtonDarkholme * @renovate[bot] * @SigureMo * @pd4d10 * @akx * @suyanhanx * @obreitwi * @huygn * @IWANABETHATGUY * @SSShuai1999 * @liby * @luismeyer95 * @bbannier * @hpmaxi + 15 contributors Languages * Rust 95.4% * Python 2.7% * TypeScript 1.7% * JavaScript 0.2% Footer (c) 2023 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * You can't perform that action at this time.