| name | ast-grep |
| description | AST-based code search and transformation tool using tree-sitter. Use for structural pattern matching, refactoring deprecated APIs, detecting anti-patterns, migrating code between library versions, and enforcing coding standards. |
ast-grep - AST-based Code Search and Transformation
ast-grep is an AST-based tool for searching, linting, and refactoring code across multiple languages. It uses tree-sitter to parse code into Abstract Syntax Trees, enabling precise structural pattern matching.
Key Pattern Syntax
$META- Matches a single AST node (e.g.,$VAR,$FUNC,$ARG)$$$- Matches zero or more AST nodes (useful for function arguments, statements)$_- Non-capturing metavariable (matches but doesn't capture)- Patterns must be syntactically valid code in the target language
Triggers
AST pattern, structural search, code refactoring, find anti-patterns, migrate code, console.log, deprecated API, function definition, class search, method signature, unused variables, security vulnerabilities, code transformation, pattern matching
When to Use
- Finding specific code patterns across large codebases
- Refactoring code (replacing deprecated APIs, updating syntax)
- Detecting anti-patterns or potential bugs
- Migrating between library versions
- Enforcing coding standards
- Interactive code rewriting