用途:借助 ast-grep(基于 tree-sitter 解析 AST)编写 YAML 规则,实现精确的代码搜索、lint 检查与重写,支持自动修复,适合代码现代化与 API 迁移。
主要能力:规则可只检查(lint)或带 fix 自动修复;支持 pattern、kind、all、any、not、has、inside 等原子与复合规则;支持 $VAR、$$$ARGS、$_VAR 等匹配语法;覆盖 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 等语言。
使用方式:项目级扫描需在根目录建 sgconfig.yml,用 ruleDirs 指定规则目录;运行 ast-grep scan 自动查找配置,或 ast-grep scan --config path/to/sgconfig.yml。单条规则验证:ast-grep scan -r rules/no-console-log.yml src/;应用修复:ast-grep scan -r rules/no-console-log.yml --update-all src/。开发流程:先用 ast-grep -p 'pattern' src/ 探索,再建 .yml,验证后按需加 constraints 减少误报;可用 ast-grep -p 'console.log($ARG)' --debug-query ast 调试 AST。
限制:ast-grep scan 必须有 sgconfig.yml,而 ast-grep run -p 可单独使用;其余细节暂未提供。