/code-quality:ast-grep-patternsfrom code-quality
Provides ast-grep patterns for structural code search and refactoring by syntax tree. Useful for renaming calls, changing API signatures, migrating frameworks, or finding code by shape across large codebases.
Ships in code-quality. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
ast-grep matches code by syntax tree, so it survives formatting differences and needs no regex escaping. The patterns below were run against ast-grep 0.51.3 on small TypeScript and TSX samples. Test each one on a small file before running it across a repository.
brew install ast-grep # or: npm install --global @ast-grep/cli
ast-grep --version
The binary is ast-grep (alias sg). An optional agent skill is published at ast-grep/agent-skill (install command not verified here).
| Request | Tool |
|---|---|
| Find a string, a name, an import | Grep |
| Find code by meaning ("where is auth handled") | A semantic search tool such as grepai, or an LSP or symbol-aware MCP server |
| Find or rewrite code by structure | ast-grep |
| Rename every call, change an API signature, migrate a framework | ast-grep |
For a small project or a one-off lookup, Grep is enough. ast-grep pays off when the same structural change touches many files.
# One-off pattern ($X = one node, $$$X = zero or more nodes)
ast-grep run -p 'console.log($$$ARGS)' -l ts src/
# Rewrite (preview first, then add --update-all to apply)
ast-grep run -p 'oldApi($A)' -r 'newApi($A)' -l ts src/
# Run a YAML rule file
ast-grep scan -r rules/async-no-try.yml src/
# Machine-readable output
ast-grep run -p 'class $C extends React.Component { $$$ }' -l tsx --json=compact src/
# Print the syntax tree of a pattern when it does not match
ast-grep run --debug-query=ast -p 'foo($A)' -l ts
The language is required for run; use ts for .ts files and tsx for .tsx files, because a rule written for one does not scan the other.
Each recipe is saved as a rule file (id, language, rule) and run with ast-grep scan -r <file> <path>.
id: async-no-try
language: tsx
rule:
pattern: async function $F($$$P) { $$$B }
not:
has:
kind: try_statement
stopBy: end
stopBy: end makes has search the whole subtree instead of only direct children. Without it the rule reports functions that do have a nested try.
id: effect-no-cleanup
language: tsx
rule:
pattern: useEffect(() => { $$$B })
not:
has:
pattern: return () => { $$$C };
stopBy: end
Only effects with a block body and no dependency array match the pattern. Add a second pattern with , [$$$D] for effects that declare dependencies.
ast-grep run -p 'function $N($A, $B, $C, $D, $E, $F $$$R) { $$$ }' -l ts src/
There is no comma before $$$R, so exactly six parameters also match. This pattern covers function declarations without a return type annotation.
ast-grep run -p 'try { $$$T } catch ($E) {}' -l ts src/
A catch block that holds only a comment is also reported, because comments are not statements.
id: deprecated-lifecycle
language: tsx
rule:
kind: method_definition
has:
field: name
regex: "^(componentWillMount|componentWillReceiveProps|componentWillUpdate)$"
A bare componentWillMount() { $$$B } is not a valid pattern (it parses as several nodes), so match the node kind and filter its name instead.
id: sql-template
language: tsx
rule:
pattern: db.query($Q)
has:
kind: template_substitution
stopBy: end
Matches only queries built with ${...} interpolation, not static template strings.
id: no-return-type
language: tsx
rule:
kind: function_declaration
not:
has:
field: return_type
kind: type_annotation
ast-grep run -p '$E.innerHTML = $V' -l ts src/
id: secret-literal
language: tsx
rule:
kind: pair
has:
field: key
regex: "(?i)^(password|secret|api_?key|token)$"
all:
- has:
field: value
kind: string
A metavariable must be a whole node: password: "$P" does not bind inside the string, which is why this rule filters by key and value kind instead.
| Symptom | Fix |
|---|---|
| Pattern matches nothing | Print the tree with --debug-query=ast and compare it with the code. A pattern must be one complete syntax node. |
| "Multiple AST nodes are detected" | The pattern is a fragment. Match the enclosing node by kind and add has or regex. |
| Rule scans nothing | Check language against the file extension (typescript vs tsx). |
| "Rule must have one positive matcher" | A not or has with only field is not enough. Add kind or pattern. |
| Slow on a large repository | Narrow the path, set -l, or use --globs to skip generated files. |
| Claude reaches for Grep | Ask explicitly: "use ast-grep to find ...". |
Large refactor:
Use ast-grep to find every call to [OLD_API] in [DIRECTORY], list the affected files,
and propose a phased rewrite. Analysis only: wait for my approval before applying a rewrite.
Framework migration:
Use ast-grep to find deprecated [FRAMEWORK] APIs in [DIRECTORY], map each to its
replacement, and order the migration by risk. Do not apply changes.
What SKILL.md sets; muted values are Claude Code's defaults
lowThree short reads, in order
npx claudepluginhub florianbruniaux/claude-code-plugins --plugin code-quality18 skills in this plugin
/audit-codebaseCodebase health audit scoring 7 categories with progression plan/check-cache-bugsAudit Claude Code setup for cache bugs (CC#40524): sentinel, --resume/--continue, attribution header + ArkNill B3/B4/B5/design-patternsDetect, suggest, and evaluate GoF design patterns in TypeScript/JavaScript codebases. Use when refactoring code, applying singleton/factory/observer/strategy patterns, reviewing pattern quality, or finding stack-native alternatives for React, Angular, NestJS, and Vue./diagnoseInteractive troubleshooting assistant for Claude Code issues/eval-skillsAudit a project or library of skills for metadata, trigger boundaries, workflow completion criteria, local resource closure, instruction economy, tool scope, effort, and routing evidence. Use before shipping skills, after importing a skill collection, or when a skill triggers too often or not at all./explainExplain code, concepts, or system behavior with adjustable depth levelsOther plugins' skills for the same job