Write and debug ast-grep rules for structural code search and rewrite. Use when tasks require AST-aware matching (not plain text), such as finding specific syntax shapes, relational patterns (inside/has), composite logic (all/any/not), or precise code refactors across a codebase.
80
100%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
View guide
Low
Low-risk findings worth noting
Translate user intent into sg patterns/rules, validate quickly, then run against target scope.
Pin down:
Use sg run --pattern first.
sg run --pattern 'console.log($ARG)' --lang javascript srcFor relational/composite logic (inside / has / precedes / follows / all / any / not / matches), use sg scan --rule.
# rule.yml
id: async-with-await
language: javascript
rule:
kind: function_declaration
has:
pattern: await $EXPR
stopBy: endsg scan --rule rule.yml srcTest on a tiny sample first, then search the project.
echo "async function f(){ await g() }" | sg scan --rule rule.yml --stdin
sg scan --rule rule.yml src --json=compactUse when kind/pattern mismatch is unclear.
sg run --pattern 'class $NAME { $$$BODY }' \
--lang javascript \
--debug-query=patternecho "const x = await fetch();" | sg scan --inline-rules "id: test
language: javascript
rule:
pattern: await \$EXPR" --stdin --json=compactsg run --pattern 'function $NAME($$$)' --lang javascript src --json=compactsg scan --rule rule.yml src
sg scan --inline-rules "id: find-async
language: javascript
rule:
kind: function_declaration
has:
pattern: await \$EXPR
stopBy: end" srcFor relational rules (inside, has), default to:
has:
pattern: await $EXPR
stopBy: endThis ensures the search traverses the entire subtree rather than stopping at the first non-matching node.
patternkindinside/hasall/any/not$A, $$OP, $$$ARGS.all to guarantee match order.--json=compact.[], treat as no match and refine pattern/scope.rule_reference.md: Full rule syntax (atomic, relational, composite, metavariables)windows.md: PowerShell quoting/escaping tips and Vue SFC script-block workflowLoad only the reference needed for the current task.
72669fb
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.