ast-grep-search

v2026.09.24

Find and replace code patterns structurally with ast-grep. Use when matching code by AST structure, finding functions with specific signatures, or detecting anti-patterns regex cannot match.

GitHub
安装命令
npx skhub add laurigates/ast-grep-search
Markdown
SKILL.md

ast-grep Structural Code Search & Refactoring

Structural code search and refactoring using ast-grep — matches code by its AST (abstract syntax tree) rather than text patterns.

When to Use This Skill

Use this skill when...Use something else instead when...
The pattern depends on AST structure (function shape, call form, argument count)Searching plain text or regex matches → tools-plugin:rg-code-search
Refactoring API call sites across many files while preserving captured variablesRunning a one-shot anti-pattern scan with a report → code-antipatterns
Hand-written structural search/replace for an ad-hoc migrationFunctional refactors of a known file/directory → code-refactor
Building a new ast-grep rule for a codebase-specific smellLooking up agentic flags for an existing linter → code-lint

When to Use ast-grep vs Grep/ripgrep

Use ast-grep when...Use grep/rg when...
Pattern depends on code structureSimple text or regex match
Need to match any number of argumentsSearching logs, docs, config
Refactoring across many filesOne-off literal string search
Finding anti-patterns (empty catch, etc.)Language doesn't matter
Replacing while preserving variablesQuick filename/line check

Decision rule: If your search pattern contains wildcards for "any expression," "any arguments," or "any function name," use ast-grep.

Find References With the Sharpest Instrument

Promoted from ~/.claude/rules/tool-use-patterns.md. Edit refuses on a file unread this session, so "read it first" is already a harness invariant. The uncovered half is checking what else depends on what you're changing, and the reflex there is grep, which cannot tell a call from a comment, a definition from a mention, or foo() from "foo" in a string. Measured across 282 local sessions: ~2,400 code-symbol searches went through grep/rg, against 6 ast-grep calls and 6 LSP calls.

Ladder, sharpest first. Falling back down it is normal; starting at the bottom for a code symbol is the miss.

Reach forWhen
LSP — findReferences, incomingCalls, goToDefinitionA language server is running for that filetype. Exact: no comment/string hits, and it follows imports and re-exports that no text pattern can.
ast-grep -p '$FN($$$ARGS)'No LSP, or the target is a shape rather than a name — a call with a given arity, a decorator, an un-awaited async call. Cross-language, bash included.
rgNon-code: markdown rules, chezmoi templates, config keys, justfile recipes, workflow YAML. The right tool there, not a fallback — most of a dotfiles or plugins repo is prose.

Two things that make the middle rung less automatic than it looks. A bare identifier pattern matches the definition too (ast-grep --lang bash -p 'foo' returns foo() { … } alongside every call), so an unshaped pattern is just grep with extra syntax. And a language server has to be on PATH, not merely installed — bash-language-server sitting in ~/.local/share/nvim/mason/bin is invisible to the LSP tool, which silently drops shell to the middle rung.

Structural tools carry their own empty-result trap: an ast-grep pattern that fails to parse returns no matches and exit 0, indistinguishable from a clean tree. Control-test the negative against a term you know is present, in the tool you are actually using (agent-patterns-plugin:tool-result-traps).

Pattern Syntax

PatternMatchesExample
$VARSingle AST nodeconsole.log($MSG)
$$$ARGSZero or more nodesfunc($$$ARGS)
$_Single node (no capture)$_ == $_
$A == $ASame node repeatedFinds x == x (not x == y)

Essential Commands

Search for Patterns

# Find structural patterns
ast-grep -p 'console.log($$$)' --lang js
ast-grep -p 'def $FUNC($$$): $$$' --lang py
ast-grep -p 'fn $NAME($$$) -> $RET { $$$ }' --lang rs

# Search in specific directory
ast-grep -p 'import $PKG' --lang js src/

# JSON output for parsing
ast-grep -p 'pattern' --lang js --json=compact

Search and Replace

# Preview changes (default - shows matches)
ast-grep -p 'var $V = $X' -r 'const $V = $X' --lang js

# Apply changes to all files
ast-grep -p 'var $V = $X' -r 'const $V = $X' --lang js -U

# Interactive review
ast-grep -p 'oldAPI($$$ARGS)' -r 'newAPI($$$ARGS)' --lang py -i

# Convert function syntax
ast-grep -p 'function($$$ARGS) { return $EXPR }' \
         -r '($$$ARGS) => $EXPR' --lang js -U

# Update import paths
ast-grep -p "import $NAME from '@old/$PATH'" \
         -r "import $NAME from '@new/$PATH'" --lang ts -U

Language Codes

CodeLanguageCodeLanguage
jsJavaScriptpyPython
tsTypeScriptrsRust
jsxJSXgoGo
tsxTSXjavaJava
cppC++rbRuby
cCphpPHP

Common Patterns by Language

JavaScript/TypeScript

# Find React hooks
ast-grep -p 'const [$STATE, $SETTER] = useState($INIT)' --lang jsx

# Find async functions
ast-grep -p 'async function $NAME($$$) { $$$ }' --lang js

# Find type assertions
ast-grep -p '$EXPR as $TYPE' --lang ts

# Find empty catch blocks
ast-grep -p 'try { $$$ } catch ($E) { }' --lang js

# Find eval usage (security)
ast-grep -p 'eval($$$)' --lang js

# Find innerHTML (XSS risk)
ast-grep -p '$ELEM.innerHTML = $$$' --lang js

# Find specific imports
ast-grep -p "import { $$$IMPORTS } from '$PKG'" --lang js

Python

# Find class definitions
ast-grep -p 'class $NAME($$$BASES): $$$' --lang py

# Find decorated functions
ast-grep -p '@$DECORATOR\ndef $FUNC($$$): $$$' --lang py

# Find dangerous shell calls
ast-grep -p 'os.system($$$)' --lang py

# Find SQL concatenation (injection risk)
ast-grep -p '"SELECT * FROM " + $VAR' --lang py

Rust

# Find unsafe blocks
ast-grep -p 'unsafe { $$$ }' --lang rs

# Find impl blocks
ast-grep -p 'impl $TRAIT for $TYPE { $$$ }' --lang rs

# Find public functions
ast-grep -p 'pub fn $NAME($$$) { $$$ }' --lang rs

Go

# Find goroutines
ast-grep -p 'go $FUNC($$$)' --lang go

# Find defer statements
ast-grep -p 'defer $FUNC($$$)' --lang go

# Find error handling
ast-grep -p 'if err != nil { $$$ }' --lang go

Common Refactoring Recipes

# Replace deprecated API calls
ast-grep -p 'oldAPI.$METHOD($$$)' -r 'newAPI.$METHOD($$$)' --lang js -U

# Rename a function across files
ast-grep -p 'oldName($$$ARGS)' -r 'newName($$$ARGS)' --lang py -U

# Remove console.log statements
ast-grep -p 'console.log($$$)' -r '' --lang js -U

# Convert require to import
ast-grep -p 'const $NAME = require($PKG)' \
         -r 'import $NAME from $PKG' --lang js -i

# Add error handling wrapper
ast-grep -p 'await $EXPR' \
         -r 'await $EXPR.catch(handleError)' --lang ts -i

Command-Line Flags

FlagPurpose
-p, --patternSearch pattern
-r, --rewriteReplacement pattern
-l, --langTarget language
-i, --interactiveReview changes one by one
-U, --update-allApply all changes
--jsonJSON output (compact, stream, pretty)
-A NLines after match
-B NLines before match
-C NLines around match
--debug-queryDebug pattern parsing

YAML Rules (Scan Mode)

For reusable rules, use ast-grep scan with YAML configuration:

# Scan with config
ast-grep scan -c sgconfig.yml

# Run specific rule
ast-grep scan -r rule-name

# Initialize a rules project
ast-grep new project my-linter
ast-grep new rule no-console-log

Minimal rule file:

id: no-empty-catch
language: JavaScript
severity: warning
message: Empty catch block hides errors
rule:
  pattern: try { $$$ } catch ($E) { }
fix: |
  try { $$$ } catch ($E) { console.error($E) }

For comprehensive YAML rule syntax, constraints, transformations, and testing, see REFERENCE.md.

Agentic Optimizations

ContextCommand
Quick structural searchast-grep -p 'pattern' --lang js --json=compact
Count matchesast-grep -p 'pattern' --lang js --json=stream | wc -l
File list onlyast-grep -p 'pattern' --json=stream | jq -r '.file' | sort -u
Batch refactorast-grep -p 'old' -r 'new' --lang js -U
Scan with rulesast-grep scan --json
Debug patternast-grep -p 'pattern' --debug-query --lang js

Quick Reference

# 1. Search for pattern
ast-grep -p 'pattern' --lang js src/

# 2. Preview rewrite
ast-grep -p 'old' -r 'new' --lang js

# 3. Apply rewrite
ast-grep -p 'old' -r 'new' --lang js -U

# 4. Scan with rules
ast-grep scan -c sgconfig.yml

# 5. Debug pattern
ast-grep -p 'pattern' --debug-query --lang js
发现
标签

此技能尚未发布标签。

版本
最新版本元数据

版本

v2026.09.24

发布时间

2026年9月24日

分类

未分类

许可证

MIT

源路径

code-quality-plugin/skills/ast-grep-search

默认分支

main

最新提交

1668324

Tree SHA

b2d4cc3