tool-ast-grep-rules
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 logger
Project Configuration
Project-level scanning requires an sgconfig.yml configuration file:
# sgconfig.yml (project root)
ruleDirs:
- rules # Rule directory, recursively loads all .yml files
Typical 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 config
Note:
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-verify
Debug AST structure:
ast-grep -p 'console.log($ARG)' --debug-query ast
Essential 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
More from heyvhuang/ship-faster
mcp-supabase
Execute database operations via Supabase MCP (query/write/migration/logs/type generation). Triggers: query/statistics/export/insert/update/delete/fix/backfill/migrate/logs/alerts/type generation. Does not trigger for: pure architecture discussion or code planning. Write operations require confirmation; UPDATE/DELETE without WHERE is refused.
76tool-openclaw
Help users install, configure, and operate OpenClaw (gateway, channels, nodes, plugins). Use when answering OpenClaw setup/debug questions; use the local docs snapshot bundled with this skill as the source of truth. Triggers: openclaw, clawdbot, clawd, clawdhub, gateway, onboard, channels login, whatsapp, telegram, discord, mattermost, pairing, nodes, sandboxing, tailscale.
62skill-evolution
Global evolution system for ship-faster skills. Uses hooks to capture context, failures, and session summaries, then generates patch suggestions (no auto edits) via skill-improver. Use when you want the skills to self-improve safely and continuously.
53review-doc-consistency
Documentation consistency reviewer that checks alignment between code implementation and documentation. Use when user requests reviewing documentation vs code consistency, checking if README/docs are outdated, verifying API documentation accuracy. Applicable for (1) reviewing README vs implementation consistency (2) checking if docs/ directory content is outdated (3) verifying API/config documentation accuracy (4) generating documentation consistency reports. Trigger words include doc review, documentation consistency, check outdated docs, verify docs.
53stripe
Billing and payment operations for Stripe: customers, products, prices, invoices, payment links, subscriptions, refunds, disputes, balance. Triggers: create customer, create product, create invoice, generate payment link, query transactions, process refunds, manage subscriptions, view disputes, check balance. Money operations require confirmation. MCP is optional — works with Dashboard/CLI too.
53tool-design-style-selector
Use when you need to define or converge a project's visual direction. Scan project documentation to identify intent, then produce a design-system.md (either preserve existing style or pick from 30 presets). Triggers: design system, design spec, UI style, visual style, design tokens, color palette, typography, layout. Flow: scan → intent → (gate) preserve vs preset → deploy design-system.md after confirmation → (default) implement UI/UX per design-system.md (plan first, then execute).
52