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.
Standardmäßig ist der Prompt ausgewählt, der zuerst die Quelle prüft. Sie können zu einem direkten Befehl wechseln oder eine lokale Kopie herunterladen.
Quelldateien prüfen
Lesen Sie SKILL.md und alle von SkillsMP angezeigten Begleitdateien, bevor Sie sich für eine Installation entscheiden.
Mit Codex oder Claude installieren Kopieren Sie diesen Prompt, fügen Sie ihn in Codex, Claude oder einen anderen Assistant ein und lassen Sie die Skill-Seite prüfen und installieren.
Ein direkter Befehl überspringt den Prüf-Prompt. Prüfen Sie die Quelle, bevor Sie ihn ausführen.
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.
ast-grep Code Search
Overview
This skill helps translate natural language queries into ast-grep rules for structural code search. ast-grep uses Abstract Syntax Tree (AST) patterns to match code based on its structure rather than just text, enabling powerful and precise code search across large codebases.
When to Use This Skill
Use this skill when users:
Need to search for code patterns using structural matching (e.g., "find all async functions that don't have error handling")
Want to locate specific language constructs (e.g., "find all function calls with specific parameters")
Request searches that require understanding code structure rather than just text
Ask to search for code with particular AST characteristics
Need to perform complex code queries that traditional text search cannot handle
General Workflow
Follow this process to help users write effective ast-grep rules:
Step 1: Understand the Query
Clearly understand what the user wants to find. Ask clarifying questions if needed:
What specific code pattern or structure are they looking for?
Which programming language?
Are there specific edge cases or variations to consider?
What should be included or excluded from matches?
Step 2: Create Example Code
Write a simple code snippet that represents what the user wants to match. Save this to a temporary file for testing.
Example:
If searching for "async functions that use await", create a test file:
// test_example.jsasyncfunctionexample() {
const result = awaitfetchData();
return result;
}
Step 3: Write the ast-grep Rule
Translate the pattern into an ast-grep rule. Start simple and add complexity as needed.
Key principles:
Always use stopBy: end for relational rules (inside, has) to ensure search goes to the end of the direction
Use pattern for simple structures
Use kind with has/inside for complex structures
Break complex queries into smaller sub-rules using all, any, or not
Dump the AST structure to understand how code is parsed:
ast-grep run --pattern 'async function example() { await fetch(); }' \
--lang javascript \
--debug-query=cst
Available formats:
cst: Concrete Syntax Tree (shows all nodes including punctuation)
ast: Abstract Syntax Tree (shows only named nodes)
pattern: Shows how ast-grep interprets your pattern
Use this to:
Find the correct kind values for nodes
Understand the structure of code you want to match
Debug why patterns aren't matching
Example:
# See the structure of your target code
ast-grep run --pattern 'class User { constructor() {} }' \
--lang javascript \
--debug-query=cst
# See how ast-grep interprets your pattern
ast-grep run --pattern 'class $NAME { $$$BODY }' \
--lang javascript \
--debug-query=pattern
Test Rules (scan with --stdin)
Test a rule against code snippet without creating files:
echo"const x = await fetch();" | ast-grep scan --inline-rules "id: test
language: javascript
rule:
pattern: await \$EXPR" --stdin