Chuyển đến nội dung
Skillpedia
Lập trình chung260k · #569

ast-grep

Skill này làm gì

Skill này giúp AI của bạn tìm và thay đổi code dựa trên cách nó được xây dựng, không chỉ khớp chữ. Phù hợp với lập trình viên cần các chỉnh sửa trên toàn bộ code đáng tin cậy.

Khi nào nên dùng

  • ✓Khi bạn muốn tìm code có một cấu trúc cụ thể, như mọi hàm có một tham số nhất định.
  • ✓Khi bạn muốn viết lại một mẫu trên nhiều file, như thay một lệnh ghi log bằng lệnh khác.
  • ✓Khi bạn cần di chuyển một codebase hoặc chạy các quy tắc kiểu lint trên toàn dự án.

Liên kết skill

https://github.com/NousResearch/hermes-agent/blob/main/optional-skills/software-development/ast-grep/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/NousResearch/hermes-agent/blob/main/optional-skills/software-development/ast-grep/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Tìm mọi nơi trong dự án này gọi console.log và thay bằng logger.info.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill dùng ast-grep, công cụ đọc code giống cách một trình biên dịch làm và khớp mẫu theo hình dạng. Nó có một script trợ giúp và các script cài đặt cho Windows và các hệ thống khác.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

ast-grep

ast-grep (binary also named sg) is an AST-aware search and rewrite tool across 25 languages. It treats your pattern as code, parses it the same way it parses your project, and matches structurally. It is the right tool whenever your question depends on code shape rather than text bytes.

This skill ships a Python wrapper at scripts/ast_grep_helper.py and platform install scripts at install.sh (POSIX) and install.ps1 (Windows). The helper adds offline pattern validation, the two-pass write trick, and binary auto-resolution. Use it as your default entry point.

Upstream source: vendored from code-yeongyu/ast-grep-skill (MIT), as shipped in oh-my-openagent's shared-skills bundle.


When to use this skill

Use it whenever the question is about code structure, not bytes:

  • "Find every function that takes a Request parameter."
  • "Rewrite every console.log(x) to logger.info(x)."
  • "Strip every as any cast."
  • "Replace require(...) with import across the repo."
  • "Find empty catch blocks."
  • "Migrate Optional[X] to X | None."
  • "Apply this codemod across these 200 files."
  • "Run our YAML lint rules and surface violations."

Switch to search_files (or plain rg) when the question is text-shaped (string literal contents, comments, license headers, file names, cross-language regex). When in doubt, ask: "does the answer depend on the language's syntax tree, or just on the file's bytes?" If the former, ast-grep. If the latter, search_files.

Hermes integration notes:

  • Run the helper and sg through the terminal tool. Single-quote every pattern so the shell never expands $VAR.
  • For find→read chains around matches, use --json-out and process with execute_code rather than piping through interpreters.
  • This complements (does not replace) Hermes's patch tool: patch is for targeted edits you author; ast-grep is for pattern-driven bulk rewrites across many sites.

Three things the agent must internalize

1. ast-grep is NOT regex

The wildcards are $VAR (one AST node) and $$$ (zero or more nodes). Regex syntax fails silently:

You wrote What ast-grep saw What you wanted
foo|bar bitwise-or of foo and bar run two separate searches
.*foo not parseable $$$ foo (if $$$ is a list of nodes) or use rg
\w+ not parseable $VAR to capture any identifier
[a-z] character class, not parseable switch to rg

The full anti-pattern table is in references/pitfalls.md §1. The helper's validate subcommand catches these mechanically — call it before debugging "no matches" by hand.

2. Patterns must be valid code

The pattern itself must parse. def $FN($$$): fails because the trailing : makes it incomplete; use def $FN($$$). function $NAME without params/body fails; use function $NAME($$$) { $$$ }. Full table per language in references/pitfalls.md §2.

3. --update-all and --json are mutually exclusive (silently)

This is the single biggest gotcha when scripting. sg run -p P -r R --json --update-all returns the JSON but does not mutate files. To both preview AND apply, run two passes:

sg run -p P -r R --json=compact .   # pass 1: see what would change
sg run -p P -r R --update-all .     # pass 2: actually apply

The helper does this automatically when you call replace --apply. Read references/pitfalls.md §9.


The helper script — scripts/ast_grep_helper.py

A single-file Python 3 stdlib wrapper. Same on every OS. The agent's default entry point.

search — find all matches of a pattern

python scripts/ast_grep_helper.py search 'console.log($MSG)' --lang ts src/

Validates the pattern offline first. If the pattern looks like regex (\w, .*, |, etc.) the helper exits with a hint and never calls sg — saves a round-trip. Pass --force to skip validation.

Flags:

  • --lang ts (or any of the 25 languages; aliases like js, py, rs, kt accepted)
  • --globs '!**/*.test.ts' (repeatable; prefix ! to exclude)
  • -C 3 (context lines)
  • --json-out (raw JSON instead of human format)

replace — rewrite by pattern, dry-run by default

# Dry-run preview (default — no files mutated)
python scripts/ast_grep_helper.py replace 'console.log($MSG)' 'logger.info($MSG)' --lang ts src/

# Actually apply
python scripts/ast_grep_helper.py replace 'console.log($MSG
Câu hỏi thường gặp

Khi nào nên dùng tìm kiếm văn bản thông thường?

Skill nói tìm kiếm văn bản tốt hơn cho nội dung chuỗi, chú thích, tên file và regex xuyên ngôn ngữ.

Nó có tự động thay đổi file không?

Skill nhắc đến phương pháp ghi hai lượt, nên các thay đổi được kiểm tra trước khi áp dụng.

Nó hỗ trợ những ngôn ngữ nào?

Skill nói ast-grep bao quát 25 ngôn ngữ.

Chi tiết

Mô tả gốc của tác giả

AST-aware structural code search and rewrite via ast-grep.

repo: NousResearch/hermes-agent

Độ phổ biến

Sao GitHub
260k
Vị trí xếp hạng
#569
Giấy phép
MIT
Cập nhật
2/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự