| name | ast-grep-explore |
| description | Use when you need to explore, search, or understand codebases using structural AST patterns. Ideal for finding specific code patterns, understanding code architecture, discovering API usage, or locating code smells across multiple files. |
ast-grep Explorer Skill
Overview
This skill enables systematic exploration of codebases using ast-grep's structural search capabilities. Unlike text-based grep, ast-grep matches code based on Abstract Syntax Tree (AST) structure, allowing precise pattern matching that understands code semantics.
When to Use
Invoke this skill when you need to:
- Understand codebase architecture - Find class hierarchies, module structures, or component relationships
- Discover API usage patterns - Locate how specific functions, methods, or libraries are used
- Find code patterns - Identify recurring patterns like specific control flows, error handling styles, or design patterns
- Locate specific constructs - Find all async functions, React hooks, database queries, etc.
- Analyze code quality - Discover potential issues, deprecated patterns, or inconsistent implementations
- Explore specific focus areas - Start from a known file or pattern and expand exploration outward
Core Concepts
Metavariables
Use $ followed by uppercase letters as wildcards that match any single AST node:
$VAR - matches any variable name
$EXPR - matches any expression
$FUNC - matches any function
$TYPE - matches any type annotation
Example: $X && $X() matches obj.val && obj.val(), user && user(), etc.
Structural vs Textual Matching
- Structural: Matches based on syntax tree (understands code meaning)
- Textual: Use
--regex for text-based matching within nodes
- Field matching: Match specific node fields like
.name, .body, .params
Languages
ast-grep supports 20+ languages including: JavaScript/TypeScript, Python, Rust, Go, Java, C/C++, C#, Ruby, PHP, and more.
Essential Commands
Basic Search
ast-grep --pattern 'console.log($X)' --lang js
sg -p 'console.log($X)' -l js
sg -p 'class $NAME { $$$BODY }' -l ts ./src
Debug Pattern Understanding
sg -p 'function $NAME($$$PARAMS) { $$$BODY }' --debug-query -l js
sg -p '$X.map($F)' -l ts --json | jq '.[] | {file: .file, match: .text}'
Language Auto-detection
sg -p 'import $X from "$MOD"' ./src
Exploration Workflows
Workflow 1: General Codebase Exploration
-
Start with high-level patterns
sg -p 'class $NAME { $$$BODY }' -l ts ./src
sg -p 'export function $NAME($$$PARAMS)' -l ts ./src
sg -p 'async function $NAME($$$PARAMS)' -l ts ./src
-
Drill down into specific areas
sg -p 'try { $$$TRY } catch ($ERR) { $$$CATCH }' -l ts ./src
sg -p 'fetch($URL, $CONFIG)' -l ts ./src
sg -p 'use$HOOK($$$ARGS)' -l ts ./src/components
-
Analyze relationships
sg -p 'class $CHILD extends $PARENT { $$$BODY }' -l ts ./src
sg -p 'import { $$$ITEMS } from "$MODULE"' -l ts ./src | head -20
Workflow 2: Focused Exploration from Starting Point
When you have a specific file, function, or pattern to start from:
-
Start from a known file
sg -p '$FUNC($$$ARGS)' -l ts ./src/services/user.ts
sg -p 'userService.$METHOD($$$ARGS)' -l ts ./src/services/
-
Expand to related code
sg -p 'function fetchUserData($$$PARAMS)' -l ts ./src
sg -p 'fetchUserData($$$ARGS)' -l ts ./src
sg -p 'const $X = fetchUserData($$$ARGS)' -l ts ./src
-
Cross-reference patterns
sg -p '$DB.$OP($$$ARGS)' -l ts ./src
sg -p 'db.collection($COLL).$METHOD($$$ARGS)' -l ts ./src
Workflow 3: Code Smell and Pattern Discovery
-
Find potential issues
sg -p 'console.log($$$ARGS)' -l ts ./src
sg -p '// $COMMENT' --regex '(TODO|FIXME|HACK|XXX)' -l ts ./src
sg -p '$STATE.$PROP = $VAL' -l ts ./src
sg -p 'eval($$$ARGS)' -l js ./src
sg -p 'new Function($$$ARGS)' -l js ./src
-
Discover inconsistent patterns
sg -p '.then($$$ARGS)' -l ts ./src
sg -p 'await $EXPR' -l ts ./src
sg -p '.catch($$$ARGS)' -l ts ./src
sg -p 'try { $$$BODY } catch' -l ts ./src
Advanced Exploration Techniques
Using Comby-style Matching
sg -p 'function $NAME($$$PARAMS) { $$$BODY }' -l ts ./src
sg -p 'const $VAR = $$$EXPRS;' -l ts ./src
Extracting Specific Information
sg -p 'TODO' -l ts --json ./src | jq -r '.[].file' | sort | uniq
sg -p 'console.log($$$ARGS)' -l ts --json ./src | jq -r '.[].file' | sort | uniq -c
sg -p 'function $NAME($$$PARAMS)' -l ts --json ./src | jq -r '.[].text' | head -20
Multi-language Exploration
sg -p 'class $NAME($$$BASES):' -l py ./src
sg -p 'class $NAME { $$$BODY }' -l js ./src
sg -p 'struct $NAME { $$$FIELDS }' -l rs ./src
Exploration Examples
Example 1: Understanding a React Component Structure
sg -p 'function $NAME($$$PROPS) { $$$BODY }' -l ts ./src/components
sg -p 'use$HOOK($$$ARGS)' -l ts ./src/components
sg -p 'useState($$$ARGS)' -l ts ./src/components
sg -p 'useEffect($$$ARGS)' -l ts ./src/components
sg -p 'props.$PROP' -l ts ./src/components
Example 2: Analyzing API Client Usage
sg -p 'api.$METHOD($URL, $$$CONFIG)' -l ts ./src
sg -p '$CLIENT.get("$ENDPOINT")' -l ts ./src
sg -p 'try { $CLIENT.$METHOD($$$ARGS) } catch ($ERR) { $$$HANDLER }' -l ts ./src
Example 3: Exploring Type Definitions
sg -p 'interface $NAME { $$$BODY }' -l ts ./src
sg -p 'type $NAME = $$$DEF' -l ts ./src
sg -p 'export interface $NAME { $$$BODY }' -l ts ./src
sg -p '$TYPE<$GENERIC>' -l ts ./src
Tips for Effective Exploration
- Start broad, then narrow: Begin with general patterns, then add specificity
- Use --debug-query: Understand how ast-grep interprets your patterns
- Inspect JSON output: Use
--json | jq for programmatic analysis
- Chain patterns: Use multiple searches to build understanding layer by layer
- Save useful patterns: Document patterns you use frequently
- Learn tree-sitter basics: Understanding AST nodes helps write better patterns
- Use field names: Reference specific AST node fields (e.g.,
.name, .params)
Common Pattern Templates
sg -p 'function $NAME($$$PARAMS) { $$$BODY }' -l ts
sg -p 'const $NAME = ($$$PARAMS) => $$$BODY' -l ts
sg -p '$OBJ.$METHOD($$$ARGS)' -l ts
sg -p 'import { $$$ITEMS } from "$MODULE"' -l ts
sg -p 'import $DEFAULT from "$MODULE"' -l ts
sg -p 'export { $$$ITEMS }' -l ts
sg -p 'export default $DEFAULT' -l ts
sg -p '$METHOD($$$PARAMS) { $$$BODY }' -l ts
sg -p 'const $NAME = $VALUE' -l ts
sg -p 'let $NAME = $VALUE' -l ts
sg -p 'if ($COND) { $$$THEN } else { $$$ELSE }' -l ts
sg -p 'for ($$$INIT; $COND; $$$UPDATE) { $$$BODY }' -l ts
sg -p 'while ($COND) { $$$BODY }' -l ts
sg -p 'try { $$$TRY } catch ($ERR) { $$$CATCH }' -l ts
sg -p 'throw new $ERROR($$$ARGS)' -l ts
Output Formats
sg -p 'console.log($$$ARGS)' -l ts ./src
sg -p 'console.log($$$ARGS)' -l ts --json ./src | jq '.'
sg -p 'console.log($$$ARGS)' -l ts --quiet ./src && echo "Found matches"
Limitations
- Requires valid syntax (cannot parse files with syntax errors)
- Language must be specified or inferable from file extension
- Pattern must be valid code in the target language
- Complex multi-file patterns require rule files (see ast-grep-refactor skill)
Integration
This skill is often used before the ast-grep-refactor skill:
- Use this skill to explore and understand patterns
- Use the refactor skill to transform those patterns
Remember: Exploration is iterative. Start with a hypothesis, search, refine your understanding, and search again with more specific patterns.