ast-grep-rule-crafterlisted
Install: claude install-skill aiskillstore/marketplace
# 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
```yaml
id: no-console-log
language: JavaScript
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)
message: Replace console.log with logger
```
## Project Configuration
项目级扫描需要 `sgconfig.yml` 配置文件:
```yaml
# sgconfig.yml (项目根目录)
ruleDirs:
- rules # 规则目录,递归加载所有 .yml 文件
```
典型项目结构:
```
my-project/
├── sgconfig.yml
├── rules/
│ ├── no-console.yml
│ └── custom/
│ └── team-rules.yml
└── src/
```
运行项目扫描:
```bash
ast-grep scan # 自动查找 sgconfig.yml
ast-grep scan --config path/to/sgconfig.yml # 指定配置
```
> **注意**: `ast-grep scan` 命令必须有 `sgconfig.yml`,而 `ast-grep run -p` 可单独使用。
## Rule Workflow
### Lint Rule (常见)
只检查不修复,用于 CI/编辑器提示:
```yaml
# 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)
```
验证:
```bash
ast-grep scan -r rules/no-console-log.yml src/
```
### Rewrite Rule (可选)
需要自动修复时添加 `fix`:
```yaml
id: no-console-log
language: JavaScript
severity: warning
message: Replace console.log with logger
rule:
pattern: console.log($$$ARGS)
fix: logger.log($$$ARGS)
```
应用修复:
```bash
ast-grep scan -r rules/no-console-log.yml --update-all src/
```
### 开发流程
```
- [ ] 1. 用 CLI 探索 pattern: ast-grep -p 'pattern' src/
- [