ast-grep
ما الذي تفعله
ast-grep أداة تبحث في الكود وتعدّله عبر لغات كثيرة. تفهم بنية الكود، لا النص فقط، لتجد أنماطًا محددة أو تجري تعديلات.
متى تستخدمها
- ✓للعثور على كل الدوال التي تأخذ معاملًا محددًا.
- ✓لاستبدال جزء من الكود بجزء آخر عبر عدة ملفات.
- ✓للتحقق من أن الكود يتبع قواعد بنيوية معينة.
رابط المهارة
https://github.com/code-yeongyu/oh-my-openagent/blob/dev/packages/shared-skills/skills/ast-grep/SKILL.mdانسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.
طريقة الاستخدام
أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.
رسالة جاهزة للصق
لـ Claude Code أو Codex أو Cursor: شغّله داخل مجلد مشروعك وستُثبَّت المهارة.
نزّل ملف SKILL.md وضعه في مجلد مهارات وكيلك.
مثال على ما تطلبه من الذكاء الاصطناعي
“ابحث عن كل استدعاءات `console.log` واستبدلها بـ `logger.info` في مشروع TypeScript الخاص بي.”
5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.
كيف تعمل
تصف ما تريد إيجاده أو تغييره باستخدام لغة تفهم بنية الكود. تحلل هذه المهارة الكود كما لو كان كودًا حقيقيًا، وتجري التعديلات اللازمة.
المحتوى التقني (ملف SKILL.md الأصلي)
النص الأصلي من المؤلف، مباشرة من GitHub.
ast-grep
sg (also installed as ast-grep) 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.
OmO native: the ast-grep MCP tools (fastest path, no CLI needed)
OmO native also registers a bundled ast-grep MCP server. Its three tools are deferred: they are absent from the resident tool list and activate automatically the first time you call them by name — no tool_search round trip needed.
mcp__ast_grep_search({ pattern, language, paths })— AST-shape search, the MCP equivalent ofsg run -p. Prefer this over shelling out tosgfor one-off lookups: no install, no PATH issues.mcp__ast_grep_rewrite({ pattern, rewrite, language, paths, apply })— preview matches (apply: false, the default) or apply the rewrite (apply: true). Same two-pass discipline assg run -r: preview first, then apply.mcp__ast_grep_scan({ paths })— run project YAML rules (sgconfig.yml,rules/), the MCP equivalent ofsg scan.
Same pattern language as the CLI: $VAR matches one AST node, $$$VAR matches zero or more, and the pattern is code, not regex. paths accepts a file or directory list; language is required for patterns that are ambiguous across languages.
When to use this skill
Use it whenever the user's question is about code structure, not bytes:
- "Find every function that takes a
Requestparameter." - "Rewrite every
console.log(x)tologger.info(x)." - "Strip every
as anycast." - "Replace
require(...)withimportacross the repo." - "Find empty catch blocks."
- "Migrate
Optional[X]toX | None." - "Apply this codemod across these 200 files."
- "Run our YAML lint rules and surface violations."
Switch to plain grep / 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, grep.
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
python3 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:
…
الأسئلة الشائعة
ما الفرق بين ast-grep وgrep (أو التعابير النمطية)؟
يتعامل grep والتعابير النمطية مع النص كسلسلة من الحروف. أما ast-grep فيفهم بنية الكود (كالدوال والمتغيرات)، ما يسمح ببحث وتعديل أدق بناءً على شكل الكود.
ما المقصود بالرمزين `$VAR` و`$$$`؟
`$VAR` يلتقط جزءًا من بنية الكود، مثل اسم متغير أو دالة. و`$$$` يلتقط صفرًا أو أكثر من أجزاء البنية.
كيف أطبّق التغييرات التي وجدتها؟
يمكنك أولًا رؤية معاينة للتغييرات (dry-run)، ثم تشغيل الأمر مرة أخرى مع خيار التطبيق لتعديل الملفات فعليًا.
التفاصيل
الوصف الأصلي من المؤلف
Searches and rewrites code by AST shape across 25 languages. Use when the target is a syntax pattern (every call/class/import shaped like X, a codemod, a YAML rule) rather than literal text; for plain strings, comments, or filenames, use rg.
الشعبية
- نجوم GitHub
- 70k
- الترتيب
- #3128
- الترخيص
- NOASSERTION
- آخر تحديث
- 15/10/2026
التعليقات
- …
المزيد من المهارات من هذا المستودع
comment-checker
70kيساعد وكيلك على فهم الملاحظات التي تصدرها أدوات فحص التعليقات الآلية والرد عليها. مفيد لمعالجة نتائج المراجعات الآلية.
lcx-contribute-bug-fix
70kيساعد وكيلك على المساهمة بإصلاحات الأخطاء في مشاريع LazyCodex. ينشئ بلاغات أو طلبات دمج لضمان جودة الكود.
lcx-doctor
70kيتيح لوكيلك فحص سلامة تثبيتات LazyCodex وCodex CLI. مفيد عندما يتصرف النظام بشكل غريب بعد التحديثات أو التغييرات.
lcx-report-bug
70kيساعد وكيلك على الإبلاغ عن الأخطاء بالتفصيل في مشاريع LazyCodex. ينشئ بلاغات تتضمن معلومات مفيدة للمطورين لإصلاح المشكلة.
مهارات مشابهة
diffs
394kيتيح لوكيلك إنتاج فرق تعديلات حقيقي يمكن مشاركته، كرابط عرض أو ملف صورة، بدلاً من ملخص مكتوب يدوياً.
coding-agent
394kيساعد وكيلك على تسليم مهمة برمجة كبيرة إلى Codex أو Claude Code أو OpenCode كعامل في الخلفية، ثم متابعة تقدمها.
gh-issues
394kيساعد وكيلك على إيجاد مشكلات GitHub واختيار ما ستعمل عليه، وفتح طلبات سحب بالإصلاحات، ومتابعة تعليقات المراجعة.