ast-grep
Guide for writing ast-grep rules to perform structural code search and analysis. Use when users need to search codebases using Abstract Syntax Tree (AST) patterns, find specific code structures, or perform complex code queries that go beyond simple text search. This skill should be used when users ask to search for code patterns, find specific language constructs, or locate code with particular structural characteristics.
Security Assessment
About ast-grep
The ast-grep skill enables users to perform advanced structural code searches by leveraging Abstract Syntax Tree (AST) patterns. Unlike traditional text-based search tools, ast-grep allows precise matching of code structures, making it possible to locate specific constructs, patterns, or behaviors across large codebases. This skill is designed to translate natural language queries into ast-grep rules, helping users define search criteria that reflect the actual structure of the code rather than relying solely on textual matches. It is particularly useful for developers, code reviewers, and analysts who need to understand code patterns in depth or identify potential issues that are not easily detectable through simple search methods.
Key capabilities of this skill include the ability to create example code snippets to model desired matches, define simple or complex ast-grep rules using patterns, kinds, and relational operators, and test these rules either inline or via rule files. Users can construct hierarchical queries using sub-rules with logical operators such as all, any, and not. The skill also provides guidance on debugging and refining rules, including the use of the `stopBy: end` directive to ensure accurate relational matching. This structured approach ensures that rules are both robust and adaptable to different programming languages and code structures.
Typical use cases for ast-grep include searching for all async functions that lack error handling, locating specific function calls with particular arguments, and performing complex queries that require understanding the underlying AST of code. The target users are developers, software engineers, and code auditors who need advanced code search capabilities, as well as teams performing code analysis or refactoring. This skill is suitable for projects where precise and structural code search can save time, improve code quality, and enhance maintainability.
FAQ
Which programming languages are supported by ast-grep?
ast-grep supports multiple programming languages that can be parsed into an AST. Users should specify the language in their rules using the 'language' field to ensure correct parsing and matching.
How do I test an ast-grep rule before running it on the full codebase?
You can test rules either inline using echo and stdin for quick iteration or by saving them to a rule file and scanning a test file. This ensures that your rules match the intended code patterns before applying them to larger projects.
Can ast-grep handle complex code patterns with nested structures?
Yes, ast-grep allows breaking down complex queries into smaller sub-rules using 'all', 'any', or 'not'. Relational rules like 'has' and 'inside' can be used to match nested structures effectively.
What should I do if my rule doesn't match any code?
First, simplify the rule to isolate issues, ensure 'stopBy: end' is used for relational rules, verify 'kind' values for the language, and use '--debug-query' to inspect the AST and adjust the rule accordingly.
Is it possible to search the codebase without creating separate rule files?
Yes, inline rules can be used directly in the command line for searches, allowing quick execution without creating additional files. However, rule files are recommended for complex or reusable rules.
Install ast-grep
Quick Setup:
- Copy the skill folder to
.claude/skills/ - Claude will automatically detect and use the skill
Repository
ast-grep/agent-skill