skills/developer-tools-integrations/ast-grep/SKILL.md
Write, debug, and validate ast-grep structural code search rules. Use this skill when the user needs syntax-aware code search, AST pattern matching, structural refactor discovery, language-construct queries, or searches that plain text tools like rg can miss, such as finding functions with particular descendants, calls inside specific contexts, missing error handling, React hook shapes, decorators, or other Tree-sitter-backed code structures.
npx skillsauth add bahayonghang/my-claude-code-settings ast-grepInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Use ast-grep when the user needs code structure, not just text. The useful outcome is a validated rule plus a clear statement of what it matches and what it may miss.
Start with the smallest tool that can answer the question.
rg for exact names, strings, comments, or simple text.If ast-grep is not installed, say so and offer an rg fallback only when the
fallback will not pretend to be structural.
Identify the language and target files.
Write a tiny fixture before searching the real repository.
Start with the simplest pattern.
ast-grep run --pattern ... --lang ... for a single-node search.has, inside, not, any,
all, regex, nthChild, or reusable metadata.Debug the parsed structure when the first rule misses.
ast-grep run --pattern '<sample code>' --lang <lang> --debug-query=ast.--debug-query prints diagnostic output and may exit non-zero; treat the
printed tree as useful debug output, not as a failed repository search.Validate on the fixture before running on the repository.
Report the final rule, command, and caveats.
Rule files avoid most shell quoting problems and are the preferred form for complex searches.
On Windows, save rule files as UTF-8 without BOM. If ast-grep scan --rule
reports missing field language even though the YAML has language, check for
a BOM at the start of the file.
# async-await.yml
id: async-await
language: javascript
rule:
any:
- kind: function_declaration
- kind: arrow_function
- kind: method_definition
has:
pattern: await $EXPR
stopBy: end
$rule = @'
id: async-await
language: javascript
rule:
any:
- kind: function_declaration
- kind: arrow_function
- kind: method_definition
has:
pattern: await $EXPR
stopBy: end
'@
[System.IO.File]::WriteAllText(
"async-await.yml",
$rule,
[System.Text.UTF8Encoding]::new($false)
)
ast-grep scan --rule async-await.yml .\src
ast-grep scan --rule async-await.yml .\src --json
ast-grep scan --rule async-await.yml ./src
ast-grep scan --rule async-await.yml ./src --json
Use inline rules only for quick checks. In PowerShell, single-quoted here-strings
preserve $META variables without escaping.
$rule = @'
id: console-call
language: javascript
rule:
pattern: console.log($$$ARGS)
'@
"console.log('debug', value);" | ast-grep scan --inline-rules $rule --stdin
In POSIX shells, quote the YAML with single quotes or escape $.
printf '%s\n' "console.log('debug', value);" |
ast-grep scan --inline-rules 'id: console-call
language: javascript
rule:
pattern: console.log($$$ARGS)' --stdin
Every scan --inline-rules example must include id, language, and rule.
When a rule produces no matches:
--lang javascript is different from --lang tsx.--debug-query=ast or --debug-query=cst.pattern or one kind.stopBy: end to deep has / inside searches.Do not assume one node kind covers a user concept.
For JavaScript and TypeScript, "function" may include:
function_declarationfunctionarrow_functionmethod_definitionFor React, decide whether the user means .js, .jsx, .ts, or .tsx.
For Python, inspect the parsed shape before relying on indentation-sensitive multi-line patterns.
When answering an ast-grep task, include:
Rule: the YAML rule or inline pattern.Validation: the fixture command you used or recommend.Repository command: the command for the user's target path.Caveats: likely false positives, false negatives, and language forms not
covered.Load references/rule_reference.md when the task needs detailed syntax for
atomic rules, relational rules, composite rules, metavariables, or troubleshooting.
development
Turn vague or complex Codex tasks into strong `/goal` commands with outcome, verification, constraints, boundaries, iteration policy, completion evidence, and pause/block conditions. Use when the user asks for Codex goal instructions, Goal 指令, 目标指令, `/goal` prompts, 中文 Goal 模板, plan-to-goal interviews, success criteria, verification commands, or bounded agent work definitions.
development
Use when the user asks to ground an ambitious proposal, avoid over-grand designs, make a bold direction executable, pressure-test feasibility, prevent "too much vision and too little landing", or turn a strategy/refactor/product idea into the smallest verifiable first move with stop rules. Trigger for requests such as 落地, 先落地, 别太飘, 收一收, 可执行, 可验证, 止损, and for follow-ups after geju-style big-picture thinking. Do not trigger for ordinary code review or implementation unless the user explicitly asks to ground or shrink the plan first.
development
Use when the user explicitly asks to think bigger, open up the design space, challenge conservative design, avoid over-indexing on backward compatibility, escape local-detail fixation, or make a bold high-level product or architecture direction call. Use for strategic reframing, not for ordinary code review, PRD writing, implementation planning, or adversarial risk review.
development
Implement safe, behavior-preserving code refactors after inspecting the existing project. Use when the user asks to refactor code, split large files or modules, extract functions or methods, reduce duplicated logic, rename confusing classes/functions/variables, improve code comments, remove unused or dead code, or says 重构代码, 拆分模块, 提取方法, 减少重复代码, 优化命名, 优化注释, 删除未调用代码. For broad refactor requests, plan safe slices and wait for approval; for narrow scoped requests, directly implement the smallest verifiable slice.