ast-grep
Structural code search using ast-grep. Use for syntax-aware searches, finding patterns, refactoring, or when you need to understand code structure beyond plain text matching.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
ast-grep
A CLI tool for structural code search using AST patterns.
When to Use
Use ast-grep instead of text-based search (grep, ripgrep) when:
- You need syntax-aware matching (not just text)
- Searching for code patterns regardless of formatting
- Finding function definitions, calls, or specific constructs
- Refactoring or analyzing code structure
Basic Usage
ast-grep --lang <language> -p '<pattern>'
Supported Languages
Common languages: rust, typescript, javascript, python, go, c, cpp, java, kotlin, swift, ruby, lua, html, css
Pattern Syntax
Metavariables
-
$NAME- matches a single AST node (identifier, expression, etc.) -
$$$ARGS- matches zero or more nodes (for argument lists, etc.) -
$_- matches any single node (wildcard)
Examples
Find all function calls to a specific function:
ast-grep --lang rust -p 'println!($$$ARGS)'
Find all struct definitions:
ast-grep --lang rust -p 'struct $NAME { $$$FIELDS }'
Find all async functions:
ast-grep --lang rust -p 'async fn $NAME($$$ARGS) $BODY'
Find method calls on a type:
ast-grep --lang rust -p '$EXPR.unwrap()'
Find if-let patterns:
ast-grep --lang rust -p 'if let $PATTERN = $EXPR { $$$BODY }'
Find React component definitions (TypeScript):
ast-grep --lang typescript -p 'function $NAME($PROPS): JSX.Element { $$$BODY }'
Find Python class definitions:
ast-grep --lang python -p 'class $NAME: $$$BODY'
Useful Options
-
--json- Output results as JSON for parsing -
--color=never- Disable color (useful for piping) -
-i, --interactive- Interactive mode for reviewing matches -
--rewrite <pattern>- Replace matches with a new pattern
Tips
- Start with a simple pattern and refine
- Use
$_when you don't care about a specific part - Use
$$$for variadic matches (arguments, statements) - Test patterns interactively with
ast-grep --interactive
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-03-03
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!