tool-ast-grep-rules
Skill337 starsChanged 10 months agoMITView on GitHub
Write AST-based code search and rewrite rules using ast-grep YAML. Create linting rules, code modernizations, and API migrations with auto-fix. Use when the user mentions ast-grep, tree-sitter patterns, code search rules, lint rules with YAML, AST matching, or code refactoring patterns.
In this repository40 other files it gives its agents.
- review-clean-codeSkill
- review-doc-consistencySkill
- review-merge-readinessSkill
- workflow-execute-plansSkill
- workflow-template-extractorSkill
- workflow-template-seederSkill
- cloudflareSkill
- deep-researchSkill
- mcp-cloudflareSkill
- mcp-stripeSkill
- mcp-supabaseSkill
- publish-x-articleSkill
- review-clean-codeSkill
- review-doc-consistencySkill
- review-merge-readinessSkill
- review-qualitySkill
- review-react-best-practicesSkill
- review-seo-auditSkill
- skill-creatorSkill
- skill-evolutionSkill
- skill-improverSkill
- stripeSkill
- supabaseSkill
- tool-better-authSkill
- tool-design-style-selectorSkill
- tool-hooks-doctorSkill
- tool-openclawSkill
- tool-programmatic-seoSkill
- tool-schema-markupSkill
- tool-systematic-debuggingSkill
- tool-ui-ux-pro-maxSkill
- tool-x-article-publisherSkill
- workflow-brainstormSkill
- workflow-creatorSkill
- workflow-execute-plansSkill
- workflow-feature-shipperSkill
- workflow-project-intakeSkill
- workflow-ship-fasterSkill
- workflow-template-extractorSkill
- workflow-template-seederSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 56
Check only without fixing, used for CI/editor hints:
- Very specific: line 76
Add fix when auto-fix is needed:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
ast-grep Rule Crafter
ast-grep uses tree-sitter to parse code into AST, enabling precise pattern matching. Rules are defined in YAML for linting, searching, and rewriting code.
Quick Start
id: no-console-log
language: JavaScript
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)
message: Replace console.log with loggerProject Configuration
Project-level scanning requires an sgconfig.yml configuration file:
# sgconfig.yml (project root)
ruleDirs:
- rules # Rule directory, recursively loads all .yml filesTypical project structure:
my-project/
├── sgconfig.yml
├── rules/
│ ├── no-console.yml
│ └── custom/
│ └── team-rules.yml
└── src/Running project scan:
ast-grep scan # Automatically finds sgconfig.yml
ast-grep scan --config path/to/sgconfig.yml # Specify configNote:
ast-grep scancommand requiressgconfig.yml, whileast-grep run -pcan be used standalone.
Rule Workflow
Lint Rule (Common)
Check only without fixing, used for CI/editor hints:
# rules/no-console-log.yml
id: no-console-log
language: JavaScript
severity: warning
message: Avoid console.log in production code
rule:
pattern: console.log($$$ARGS)Verify:
ast-grep scan -r rules/no-console-log.yml src/Rewrite Rule (Optional)
Add fix when auto-fix is needed:
id: no-console-log
language: JavaScript
severity: warning
message: Replace console.log with logger
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)Apply fix:
ast-grep scan -r rules/no-console-log.yml --update-all src/Development Workflow
- [ ] 1. Explore pattern with CLI: ast-grep -p 'pattern' src/
- [ ] 2. Create rule file (.yml)
- [ ] 3. Verify: ast-grep scan -r rule.yml src/
- [ ] 4. If false positives → add constraints → re-verifyDebug AST structure:
ast-grep -p 'console.log($ARG)' --debug-query astEssential Syntax
| Element | Syntax | Example |
| Single node | $VAR | console.log($MSG) |
| Multiple nodes | $$$ARGS | fn($$$ARGS) |
| Same content | Use same name | $A == $A |
| Non-capturing | $_VAR | $_FN($_FN) |
Core Rules Quick Reference
| Type | Purpose | Example |
pattern | Match code structure | pattern: if ($COND) {} |
kind | Match AST node type | kind: function_declaration |
all | Match ALL conditions | all: [pattern: X, kind: Y] |
any | Match ANY condition | any: [pattern: var $A, pattern: let $A] |
not | Exclude matches | not: {pattern: safe_call()} |
has | Must have child | has: {kind: return_statement} |
inside | Must be in ancestor | inside: {kind: class_body} |
Detailed References
Complete syntax guide: See rule-syntax.md
- Atomic rules (pattern, kind, regex, nthChild, range)
- Composite rules (all, any, not, matches)
- Relational rules (has, inside, follows, precedes)
- Transform and fixConfig
Language-specific patterns: See common-patterns.md
- JavaScript/TypeScript examples
- Python examples
- Go and Rust examples
Supported Languages
Bash, C, Cpp, CSharp, Css, Elixir, Go, Haskell, Hcl, Html, Java, JavaScript, Json, Kotlin, Lua, Nix, Php, Python, Ruby, Rust, Scala, Solidity, Swift, Tsx, TypeScript, Yaml
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

