Skillpedia
برمجة عامة260k · #569

ast-grep

ما الذي تفعله

تساعد هذه المهارة وكيلك على البحث في الشيفرة وتعديلها بناءً على بنيتها، لا على مطابقة النص فقط. وهي للمطورين الذين يحتاجون إلى تعديلات موثوقة على مستوى المشروع كله.

متى تستخدمها

  • ✓عندما تريد إيجاد شيفرة لها بنية محددة، مثل كل دالة لها معامل معيّن.
  • ✓عندما تريد إعادة كتابة نمط عبر ملفات كثيرة، مثل استبدال استدعاء تسجيل بآخر.
  • ✓عندما تحتاج إلى ترحيل قاعدة شيفرة أو تشغيل قواعد من نوع lint على المشروع كله.

رابط المهارة

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

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/NousResearch/hermes-agent/blob/main/optional-skills/software-development/ast-grep/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“ابحث عن كل موضع في هذا المشروع يُستدعى فيه console.log، واستبدله بـ logger.info.”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

تستخدم المهارة ast-grep، الذي يقرأ الشيفرة كما يقرؤها المترجم، ويطابق الأنماط حسب شكلها. وتتضمن سكربت مساعد وسكربتات تثبيت لـ Windows وأنظمة أخرى.

المحتوى التقني (ملف SKILL.md الأصلي)

النص الأصلي من المؤلف، مباشرة من GitHub.

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
الأسئلة الشائعة

متى أستخدم البحث النصي العادي بدلًا منه؟

تقول المهارة إن البحث النصي أنسب لمحتويات السلاسل والتعليقات وأسماء الملفات وتعبيرات regex المشتركة بين اللغات.

هل يغيّر الملفات تلقائيًا؟

تذكر المهارة طريقة كتابة من مرحلتين، فتُفحص التغييرات قبل تطبيقها.

ما اللغات المدعومة؟

تقول المهارة إن ast-grep يغطي 25 لغة.

التفاصيل

الوصف الأصلي من المؤلف

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

repo: NousResearch/hermes-agent

الشعبية

نجوم GitHub
260k
الترتيب
#569
الترخيص
MIT
آخر تحديث
2‏/11‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة