mcpField
A
ast-grep

ast-grep

github.com/pproenca开发220335 次安装11 周前更新
代码仓库

关于 ast-grep

ast-grep 是由社区维护的实践指南,面向编写和使用 ast-grep 规则的开发者。内容指导规则模式设计、组合、改写、性能优化与验证。

主要功能

  1. 01涵盖 8 类的 46 条规则
  2. 02模式与元变量使用指南
  3. 03规则组合与约束设计
  4. 04改写正确性实践
  5. 05性能优化与调试建议

适用场景

  1. 01编写代码搜索或 lint 规则
  2. 02调试未按预期匹配的模式
  3. 03优化大型代码库扫描
  4. 04组织并测试规则项目

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/pproenca/dot-skills --skill ast-grep

安装后在 Claude Code 中输入/ast-grep确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

AGENTS.md
metadata.json
README.md
assets/templates/
_template.md
references/
_sections.md
compose-all-for-and-logic.md
compose-any-for-or-logic.md
compose-field-targeting.md
compose-has-for-children.md
compose-inside-for-context.md
compose-matches-for-reuse.md
compose-not-for-exclusion.md
compose-precedes-follows.md
const-kind-filter.md
const-not-inside-not.md
const-pattern-constraint.md
const-post-match-timing.md
const-regex-filter.md
meta-multi-match-lazy.md
meta-named-vs-unnamed.md
meta-naming-convention.md
meta-reuse-binding.md
meta-single-node.md
meta-underscore-noncapture.md
org-file-filtering.md
org-message-clarity.md
org-project-structure.md
org-severity-levels.md
org-unique-rule-ids.md
pattern-avoid-comments-strings.md

详细介绍

ast-grep 社区最佳实践

这是一份由 ast-grep 社区维护的全面指南,介绍 ast-grep 规则编写和使用的最佳实践。指南涵盖 8 个类别中的 46 条规则,并按影响程度排序,以指导自动化规则生成和代码转换。

适用场景

以下情况请参考这些指南:

  • 为 lint 检查或搜索编写新的 ast-grep 规则
  • 调试与预期代码不匹配的模式
  • 优化规则在大型代码库中的性能
  • 以规范的组织方式设置 ast-grep 项目
  • 检查 ast-grep 规则的正确性和可维护性

通用工作流

创建 ast-grep 代码搜索规则时,请遵循以下工作流:

第 1 步:理解查询

明确你要查找的内容:

  • 目标编程语言
  • 要处理的边界情况
  • 要包含和排除的内容
第 2 步:创建示例代码

编写一段示例代码,表示预期的匹配模式。

第 3 步:编写 ast-grep 规则

选择合适的方法:

  • 简单结构使用 pattern
  • 复杂结构使用 kind 和 has/inside
  • 复合查询结合使用 all、any 或 not
  • 关系规则(inside、has)始终使用 stopBy: end,确保完成完整搜索
第 4 步:测试规则
# Inspect AST structure
ast-grep run --pattern '[code]' --lang [language] --debug-query=ast

# Test inline rule
echo "[code]" | ast-grep scan --inline-rules "[rule]" --stdin

# Test from file
ast-grep scan --rule [file.yml] [path]
第 5 步:搜索代码库

部署已验证的规则:

# Search with pattern (simple matches)
ast-grep run --pattern '[pattern]' --lang [language] [path]

# Search with rule file (complex queries)
ast-grep scan --rule [file.yml] [path]

# Apply fixes interactively
ast-grep scan --rule [file.yml] --interactive [path]
快速提示
  1. 始终使用 stopBy: end - 确保关系规则遍历完整子树
  2. 从简单开始,逐步增加复杂度 - 先从模式入手,再尝试 kind,最后使用关系规则
  3. 通过 AST 检查进行调试 - 使用 --debug-query=ast 验证结构匹配
  4. 在内联规则中转义 - 对 Shell 命令使用 \$VAR 或单引号
  5. 先在 playground 中测试 - 使用 https://ast-grep.github.io/playground.html 快速迭代

按优先级划分的规则类别

优先级类别影响前缀
1模式正确性CRITICALpattern-
2元变量用法CRITICALmeta-
3规则组合HIGHcompose-
4约束设计HIGHconst-
5重写正确性MEDIUM-HIGHrewrite-
6项目组织MEDIUMorg-
7性能优化MEDIUMperf-
8测试与调试LOW-MEDIUMtest-

快速参考

1. 模式正确性(CRITICAL)
2. 元变量用法(关键)
3. 规则组合(高)
4. 约束设计(高)
5. 重写正确性(中高)
6. 项目组织(中)
7. 性能优化(中)
8. 测试与调试(低至中等)

如何使用

阅读各个参考文件,了解详细说明和代码示例:

完整编译文档

  • AGENTS.md - 包含所有规则的完整编译指南

参考文件

文件描述
AGENTS.md包含所有规则的完整编译指南
references/_sections.md类别定义和排序
assets/templates/_template.md新规则模板
metadata.json版本和参考信息

评论

支持 Markdown