ast-grep
यह क्या करती है
ast-grep एक ऐसा टूल है जो कई भाषाओं के कोड में खोज और बदलाव करता है। यह सिर्फ टेक्स्ट नहीं, बल्कि कोड की संरचना को समझता है, ताकि खास पैटर्न ढूँढ सके या बदलाव कर सके।
कब इस्तेमाल करें
- ✓किसी खास पैरामीटर लेने वाले सभी फंक्शन ढूँढने के लिए।
- ✓कई फाइलों में एक कोड को दूसरे कोड से बदलने के लिए।
- ✓जाँचने के लिए कि कोड कुछ संरचनात्मक नियमों का पालन करता है या नहीं।
Skill का लिंक
https://github.com/code-yeongyu/oh-my-openagent/blob/dev/packages/shared-skills/skills/ast-grep/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“मेरे TypeScript प्रोजेक्ट में सभी `console.log` कॉल ढूँढो और उन्हें `logger.info` से बदल दो।”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
आप एक ऐसी भाषा में बताते हैं कि क्या ढूँढना या बदलना है, जो कोड की संरचना समझती है। टूल आपके कोड का असली कोड की तरह विश्लेषण करता है और ज़रूरी बदलाव कर देता है।
तकनीकी सामग्री (मूल 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 (या regex) में क्या फर्क है?
grep और regex टेक्स्ट को अक्षरों की लंबी श्रृंखला मानकर देखते हैं। ast-grep कोड की संरचना (जैसे फंक्शन, वेरिएबल आदि) समझता है, इसलिए वह कोड के आकार के आधार पर ज़्यादा सटीक खोज और बदलाव कर सकता है।
`$VAR` और `$$$` चिह्न क्या हैं?
`$VAR` कोड की संरचना के किसी हिस्से (जैसे वेरिएबल या फंक्शन का नाम) को पकड़ने के लिए है। `$$$` शून्य या उससे ज़्यादा हिस्सों को पकड़ने के लिए है।
मिले बदलावों को कैसे लागू करूँ?
आप पहले बदलावों का पूर्वावलोकन (dry-run) देख सकते हैं, और फिर apply विकल्प के साथ कमांड दोबारा चलाकर फाइलों में सचमुच बदलाव कर सकते हैं।
जानकारी
लेखक का मूल विवरण
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
कमेंट
- …
इस repository की और skills
comment-checker
70kयह आपके एजेंट को automatic comment checkers के feedback को समझने और उस पर जवाब देने में मदद करता है। automated code या content reviews के नतीजों को प्रोसेस करने में उपयोगी है।
lcx-contribute-bug-fix
70kयह आपके एजेंट को LazyCodex से जुड़े projects में bug fixes देने में मदद करता है। code की गुणवत्ता बनाए रखने के लिए यह issues या pull requests बनाता है।
lcx-doctor
70kयह आपके एजेंट को LazyCodex और Codex CLI installations की स्थिति जांचने देता है। updates या बदलावों के बाद system अजीब व्यवहार करे, तब यह उपयोगी है।
lcx-report-bug
70kयह आपके एजेंट को LazyCodex projects के bugs विस्तार से रिपोर्ट करने में मदद करता है। यह ऐसे issues बनाता है जिनमें developers के लिए समस्या ठीक करने की काम की जानकारी होती है।
मिलती-जुलती skills
diffs
394kयह आपके एजेंट को बदलावों का असली diff बनाने देती है, जिसे व्यूअर लिंक या इमेज फ़ाइल के रूप में साझा किया जा सकता है, हाथ से लिखे सारांश के बजाय।
coding-agent
394kयह आपके एजेंट को बड़े कोडिंग काम Codex, Claude Code या OpenCode को बैकग्राउंड वर्कर के रूप में सौंपने, फिर उसकी प्रगति जांचने में मदद करती है।
gh-issues
394kयह आपके एजेंट को GitHub issues खोजने, उनमें से काम करने लायक चुनने, और सुधार के साथ pull requests खोलने में मदद करती है, जिसमें समीक्षा टिप्पणियों का फ़ॉलो-अप भी शामिल है।