Loading...
Loading...
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.
npx skill4agent add heyvhuang/ship-faster tool-ast-grep-rulesid: no-console-log
language: JavaScript
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)
message: Replace console.log with loggersgconfig.yml# sgconfig.yml (project root)
ruleDirs:
- rules # Rule directory, recursively loads all .yml filesmy-project/
├── sgconfig.yml
├── rules/
│ ├── no-console.yml
│ └── custom/
│ └── team-rules.yml
└── src/ast-grep scan # Automatically finds sgconfig.yml
ast-grep scan --config path/to/sgconfig.yml # Specify configNote:command requiresast-grep scan, whilesgconfig.ymlcan be used standalone.ast-grep run -p
# 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)ast-grep scan -r rules/no-console-log.yml src/fixid: no-console-log
language: JavaScript
severity: warning
message: Replace console.log with logger
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)ast-grep scan -r rules/no-console-log.yml --update-all src/- [ ] 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-verifyast-grep -p 'console.log($ARG)' --debug-query ast| Element | Syntax | Example |
|---|---|---|
| Single node | | |
| Multiple nodes | | |
| Same content | Use same name | |
| Non-capturing | | |
| Type | Purpose | Example |
|---|---|---|
| Match code structure | |
| Match AST node type | |
| Match ALL conditions | |
| Match ANY condition | |
| Exclude matches | |
| Must have child | |
| Must be in ancestor | |