skills/ast-grep/SKILL.md
Reference guide for ast-grep (sg) — a syntax-aware CLI tool for code search, linting, and rewriting using AST patterns. Use when writing ast-grep patterns, creating YAML lint rules, rewriting code with --rewrite or fix:, setting up an ast-grep project (sgconfig.yml), writing sg test cases, or asking "how do I search/lint/rewrite code with ast-grep". Do NOT use for non-ast-grep code search tools (ripgrep, grep, semgrep).
npx skillsauth add armandli/get-skilled 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.
# Search: find all eval() calls in JavaScript
sg run -p 'eval($X)' -l js src/
# Rewrite: migrate optional chaining (interactive)
sg run -p '$PROP && $PROP()' -r '$PROP?.()' -l ts -i src/
# Lint: run all project rules
sg scan
| Command | Purpose |
|---------|---------|
| sg run -p PATTERN -l LANG [PATH] | One-shot search or rewrite |
| sg scan | Run all YAML rules in the project |
| sg test | Validate rules against test cases |
| sg new | Scaffold project, rule, or test |
| sg lsp | Start language server for editor integration |
Key sg run flags:
| Flag | Description |
|------|-------------|
| -p, --pattern PATTERN | AST pattern to match |
| -l, --lang LANG | Target language (js, ts, py, go, rust, java, …) |
| -r, --rewrite REWRITE | Replacement string (uses captured metavariables) |
| -k, --kind KIND | Match by AST node type only |
| -i, --interactive | Review each change before applying |
| -U, --update-all | Apply all changes without prompting |
| --json | Machine-readable JSON output |
For complete flag reference, see references/cli.md.
Patterns are valid code snippets with $-prefixed metavariable holes:
| Syntax | Matches | Example |
|--------|---------|---------|
| $NAME | Any single AST node | $A == $A (both sides identical) |
| $$$NAME | Zero or more nodes (sequence) | fn($$$ARGS) matches any arg list |
| $$NAME | Any unnamed tree-sitter node | Internal parser nodes |
| $_NAME | Single node, not captured | Use when match but don't need the value |
| $_ | Anonymous wildcard | Discard a single node |
Rules:
$A + $A only matches when both sides are equal$$$ matches zero or more siblings — use for argument lists, statement sequencesFor full pattern syntax, see references/patterns.md.
id: no-eval
language: javascript
rule:
pattern: eval($CODE)
severity: error
message: "eval() is a security risk"
fix: "/* eval removed */"
files:
- "src/**/*.js"
ignores:
- "**/*.test.js"
Rule operators at a glance:
| Category | Operators |
|----------|-----------|
| Atomic | pattern, kind, regex, nthChild, range |
| Relational | inside, has, precedes, follows |
| Composite | all, any, not, matches |
For the full YAML schema, see references/yaml-rules.md. For all rule operators with examples, see references/rule-operators.md.
js / ts / tsx / jsx · py · go · rust · java · kotlin · scala
c / cpp · cs · rb · php · swift · bash · html · css · json
tools
--- name: update-readme description: Updates a project README.md with build instructions, unit test instructions, and a mermaid architecture diagram. Use when a project README needs to be created or refreshed. Trigger phrases: "update readme", "generate readme", "create readme", "refresh readme docs". Emphasizes project interfaces, extension points, and customization hooks in the diagram — not concrete implementations. Do NOT use for documentation sites, wikis, or non-project READMEs. argument-h
business
--- name: skill-stat description: Records skill usage statistics and issue reports into .claude/skill-stats.md. Increments the Uses count for a skill name, and optionally logs an issue report that increments the Issues count and appends a row to the Issue Reports table. Use when tracking how often a skill is invoked, when a user reports a problem with a skill, or when another skill needs to log its own usage. Trigger phrases: "record skill stat", "log skill usage", "report skill issue". Do NOT u
testing
--- name: revert description: Reverts ALL git changes in the working directory: staged changes, unstaged modifications, and new untracked files. Use when user asks to "revert all changes", "undo all changes", "discard all changes", "reset all git changes", or "clean working directory". Do NOT use for reverting a specific file or a specific commit — those need targeted git commands. disable-model-invocation: true --- Revert all git changes in the working directory. This is destructive and cannot
tools
Scans a Python codebase for duplicate or near-duplicate logic patterns across functions, classes, and files, then extracts those patterns into typed utility classes in a shared module. Use when the user asks to "refactor this Python code", "find duplicate logic", "extract shared utilities", "apply DRY to Python", "deduplicate Python code", or "find repeated patterns in Python". Groups extracted helpers by the object type they operate on (strings, numbers, dates, collections, etc.). Do NOT use for performance optimization (use optimize-python), for debugging logic errors, or for explaining code. Do NOT extract code that appears only once. Run this skill before optimize-python.