From code-please
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.
How this skill is triggered — by the user, by Claude, or both
Slash command
/code-please:ast-grepThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Guide for writing ast-grep rules to perform structural code search and analysis
Guide for writing ast-grep rules to perform structural code search and analysis
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.
Use this skill when users:
Follow this process to help users write effective ast-grep rules:
Clearly understand what the user wants to find. Ask clarifying questions if needed:
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.js
async function example() {
const result = await fetchData()
return result
}
Translate the pattern into an ast-grep rule. Start simple and add complexity as needed.
Key principles:
stopBy: end for relational rules (inside, has) to ensure search goes to the end of the directionpattern for simple structureskind with has/inside for complex structuresall, any, or notExample rule file (test_rule.yml):
id: async-with-await
language: javascript
rule:
kind: function_declaration
has:
pattern: await $EXPR
stopBy: end
See references/rule_reference.md for comprehensive rule documentation.
Use ast-grep CLI to verify the rule matches the example code.
Option A: Test with inline rules (for quick iterations)
echo "async function test() { await fetch(); }" | ast-grep scan --inline-rules "id: test
language: javascript
rule:
kind: function_declaration
has:
pattern: await \$EXPR
stopBy: end" --stdin
Option B: Test with rule files (recommended for complex rules)
ast-grep scan --rule test_rule.yml test_example.js
Debugging if no matches:
stopBy: end to relational rules if not present--debug-query to understand the AST structurekind values are correct for the languageOnce the rule matches the example code correctly, search the actual codebase:
For simple pattern searches:
ast-grep run --pattern 'console.log($ARG)' --lang javascript /path/to/project
For complex rule-based searches:
ast-grep scan --rule my_rule.yml /path/to/project
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 patternTest 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
Simple pattern-based search for single AST node matches:
# Basic pattern search
ast-grep run --pattern 'console.log($ARG)' --lang javascript .
# JSON output for programmatic use
ast-grep run --pattern 'function $NAME($$$)' --lang javascript --json .
YAML rule-based search for complex structural queries:
# With rule file
ast-grep scan --rule my_rule.yml /path/to/project
# With inline rules
ast-grep scan --inline-rules "id: find-async
language: javascript
rule:
kind: function_declaration" .
For relational rules (inside, has), always include stopBy: end:
has:
pattern: await $EXPR
stopBy: end
This ensures the search traverses the entire subtree rather than stopping at the first non-matching node.
pattern firstkind to match the node typehas, inside) as neededall, any, not) for complex logicconsole.log($ARG))When rules don't match:
--debug-query=cst to see the actual AST structurekind matches what you expectWhen using --inline-rules, escape metavariables in shell commands:
\$VAR instead of $VAR (shell interprets $ as variable)'$VAR' works in most shellsFind async functions that use await:
ast-grep scan --inline-rules "id: async-await
language: javascript
rule:
all:
- kind: function_declaration
- has:
pattern: await \$EXPR
stopBy: end" /path/to/project
Find console.log inside class methods:
ast-grep scan --inline-rules "id: console-in-class
language: javascript
rule:
pattern: console.log(\$\$\$)
inside:
kind: method_definition
stopBy: end" /path/to/project
Find async functions without try-catch:
ast-grep scan --inline-rules "id: async-no-trycatch
language: javascript
rule:
all:
- kind: function_declaration
- has:
pattern: await \$EXPR
stopBy: end
- not:
has:
pattern: try { \$\$\$ } catch (\$E) { \$\$\$ }
stopBy: end" /path/to/project
bash, c, cpp, csharp, css, elixir, go, haskell, html, java, javascript, json, kotlin, lua, nix, php, python, ruby, rust, scala, solidity, swift, typescript, tsx, yaml
On Linux, sg is a system command for setgroups. Always use the full command name ast-grep instead of the sg alias. If you prefer the shorter alias, add this to your shell profile:
alias sg=ast-grep
See references/rule_reference.md for detailed rule syntax documentation.
For using ast-grep with dora MCP tools, see the code-please:ast-grep agent definition.
Provides behavioral guidelines to reduce common LLM coding mistakes, focusing on simplicity, surgical changes, assumption surfacing, and verifiable success criteria.
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Creates, edits, and optimizes skills for Claude Code, including drafting, evaluating with test prompts, iterating on performance, and improving skill descriptions for better triggering accuracy.
npx claudepluginhub pleaseai/code-intelligence --plugin code-intelligence-lsp