ast-grep | Skill Performance & Reviews | TopRankSkills

TopRank Skills

Home / Skills / tools / ast-grep

ast-grep

maintained by onbjerg

star 4 account_tree 1 verified_user MIT License
bolt View GitHub

name: ast-grep description: 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.

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

  1. Start with a simple pattern and refine
  2. Use $_ when you don't care about a specific part
  3. Use $$$ for variadic matches (arguments, statements)
  4. Test patterns interactively with ast-grep --interactive

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!

Skill Details

GitHub Stars 4
GitHub Forks 1
Created Jan 2026
Last Updated il y a 3 mois
tools tools automation tools

Related Skills

fabric
chevron_right
specs-gen
chevron_right
typescript-expert
chevron_right
break-loop
chevron_right
burp-suite
chevron_right

Build your own?

Join 12,000+ developers contributing to the Claude ecosystem.