ast_grep
Search, replace, or count code patterns using syntax-aware AST matching across 20+ programming languages.
Instructions
A powerful AST-based code search and refactoring tool that understands code structure across 20+ programming languages. Performs syntax-aware pattern matching and transformations.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| glob | No | Glob pattern to filter files (e.g. '*.js', '**/*.{ts,tsx}') | |
| mode | No | Operation mode | search |
| path | No | File or directory to search in. Defaults to current working directory. | |
| context | No | Number of lines to show around matches (like grep -C) | |
| dry_run | No | Preview changes without modifying files (default: true for replace mode) | |
| pattern | Yes | AST pattern to search for using ast-grep syntax (e.g., 'console.log($MSG)', 'function $NAME($ARGS) { $$$BODY }') | |
| language | No | Target language for parsing. Auto-detected if not specified. | |
| head_limit | No | Limit output to first N matches | |
| replacement | No | Replacement pattern (optional). If provided, performs replacement instead of search. Uses same variable syntax as pattern. |