Установка
SKILL.md
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.
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:
--lang ts(or any of the 25 languages; aliases likejs,py,rs,ktaccepted)--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)
python3 scripts/ast_grep_helper.py replace 'console.log($MSG)' 'logger.info($MSG)' --lang ts src/
# Actually apply
python3 scripts/ast_grep_helper.py replace 'console.log($MSG)' 'logger.info($MSG)' --lang ts src/ --apply
The helper:
- Validates both
patternandrewritefor hint-detectable mistakes. - Runs pass 1 with
--json=compactto collect matches and show a preview. - If
--applyis set, runs pass 2 with--update-allto mutate files.
scan — run YAML rules
# Discover sgconfig.yml from cwd and run all rules
python3 scripts/ast_grep_helper.py scan src/
# Run a single rule file
python3 scripts/ast_grep_helper.py scan -r rules/no-console.yml src/
# Apply auto-fixes
python3 scripts/ast_grep_helper.py scan -U src/
# CI-friendly GitHub annotations
python3 scripts/ast_grep_helper.py scan --report-style short src/
validate — offline pattern check (no sg call)
Useful for CI lints, pre-commit hooks, and quick sanity checks:
python3 scripts/ast_grep_helper.py validate '\w+' --lang ts
# → exit 2: regex \w not supported. Use $VAR for identifiers.
python3 scripts/ast_grep_helper.py validate 'console.log($MSG)' --lang ts
# → exit 0: pattern looks plausible for ast-grep.
langs / doctor / install
python3 scripts/ast_grep_helper.py langs # list 25 supported languages and aliases
python3 scripts/ast_grep_helper.py doctor # check ast-grep binary availability
python3 scripts/ast_grep_helper.py install # delegate to install.sh / install.ps1
new and test subcommands proxy directly to sg new and sg test.
Direct sg use (when the helper isn't enough)
The helper is opinionated. For full control, drop to sg. The skill ships a CLI cheat sheet in references/cli.md. The minimal idioms:
# Search
sg run -p 'console.log($MSG)' --lang ts src/
# Search with JSON for scripting
sg run -p 'console.log($MSG)' --lang ts --json=compact src/ | jq '.[] | .file'
# Rewrite, dry-run
sg run -p 'console.log($MSG)' -r 'logger.info($MSG)' --lang ts --json=compact src/
# Rewrite, apply
sg run -p 'console.log($MSG)' -r 'logger.info($MSG)' --lang ts --update-all src/
# Pattern from stdin (great for ad-hoc experiments)
echo 'console.log("hi")' | sg run -p 'console.log($MSG)' --lang js --stdin
# Debug a pattern that returns 0 matches
sg run -p '<your pattern>' --lang <lang> --debug-query=ast --stdin <<< '<sample-code>'
# Run YAML rules
sg scan src/
# Inline YAML rule (one-off)
sg scan --inline-rules '
id: no-todo
language: TypeScript
severity: warning
rule: { pattern: TODO }' src/
When using sg directly in a shell, always single-quote patterns so $VAR is not expanded by the shell.
Decision tree — what to use, when
USER asks for "find/rewrite/codemod"
│
├─ structural pattern (function shape, call, class, import, control flow)
│ └→ ast-grep (this skill)
│
├─ text pattern (regex, alternation, character classes, file names)
│ └→ rg / grep
│
├─ semantic question (what variable does this refer to? does this throw?)
│ └→ LSP tools, TypeScript compiler, Pyright, Semgrep with type inference
│
└─ multiple repos / federated search
└→ a search engine + then ast-grep / rg / LSP per-repo
If the user says "find all" or "every", default to ast-grep when the target is shaped (function, class, call, import, statement). Default to rg when the target is text (string content, comment, license header, file name, identifier substring).
Always run dry-run first when rewriting
A bad pattern silently rewrites the wrong thing. The helper's replace defaults to dry-run for this reason. The flow is:
- Search to confirm matches:
helper search '<pattern>' --lang X . - Dry-run rewrite:
helper replace '<pattern>' '<rewrite>' --lang X .(no--apply) - Inspect the dry-run summary: number of matches, files affected, the per-location preview.
- If wrong: refine pattern, go back to step 1.
- If right:
helper replace '<pattern>' '<rewrite>' --lang X . --apply.
Never apply a rewrite that you have not first dry-run.
When sg returns 0 matches but you know the code is there
In priority order:
- Run
helper validate '<pattern>' --lang <lang>— catches regex misuse, missing function bodies, Python trailing colons. - Check
--lang—sginfers from extension; if you pass a.tsxfile with--lang ts(nottsx), JSX won't parse. - Inspect the parsed pattern:
sg run -p '<pattern>' --lang <lang> --debug-query=ast --stdin <<< '<sample>'. If it showsERRORnodes, the pattern is malformed. - Check the AST of the target file:
sg run -p '$_' --lang <lang> --debug-query=cst path/to/file | head -40— find thekindyou're trying to match. - Try the playground: https://ast-grep.github.io/playground.html — paste code + pattern, see what's happening.
Do not blindly retry with variations. Each failure has a reason; surface it.
When to use YAML rules vs inline -p patterns
Use inline -p when:
- One-off ad-hoc query.
- The pattern is simple (no constraints, no fix template).
- You're exploring.
Use YAML rules (file under rules/, run via sg scan) when:
- The pattern is reused (lint rule, codemod that runs in CI).
- You need
constraints,transform, complexinside/has, or composite logic. - You want auto-fix (
fix:field). - You want to test the rule (snapshot tests via
sg test).
The full YAML rule schema is in references/yaml-rules.md. Project setup (sgconfig.yml, ruleDirs, utilDirs) is in references/sgconfig.md.
Output discipline
sg run --json=compactproduces an array of match objects:{ file, range: {start, end}, text, replacement?, lines, language, ... }. Pipe throughjqfor further processing.- Without
--json,sgproduces human-readable colored output suitable for terminals. - The helper's default output is human-readable (file:line:column + match preview). Pass
--json-outfor raw JSON. - The helper's
replacealways summarizes: number of matches, number of files, per-location preview.
When summarizing for the user, always include the count of files affected, not just the count of matches. Users care about blast radius.
Required reading (in order of priority)
references/patterns.md— meta-variables, naming rules, strictness levels. Read when you're unsure why a pattern doesn't match.references/pitfalls.md— the failure-mode field guide. Read when 0 matches surprises you.references/recipes.md— copy-paste patterns by language. Read first when you start a new task.references/cli.md—sg run,sg scan,sg test,sg new,sg lsp. Read when the helper isn't enough.references/yaml-rules.md— YAML rule schema. Read when you outgrow inline patterns.references/sgconfig.md— project-level configuration. Read when you set upsg scanfor a real project.references/install.md— per-OS install methods. Read only ifinstall.sh/install.ps1fail.
Invariants (do not break)
- Validate before searching. When emitting a pattern programmatically, call
helper validatefirst. It catches the regex-misuse class of mistakes that account for ~70% of "0 matches" debug sessions. - Dry-run before applying. Never run
sg run -r ... --update-allwithout first inspecting the matches. The helper'sreplaceenforces this by default. - Two-pass writes. When using
sgdirectly to both preview and apply, run two invocations —--jsonignores--update-all. - Single-quote patterns in shell.
'$VAR'not"$VAR". The shell expands$VARto the empty string in double quotes, breaking the pattern. - Pattern is code, not regex. When the pattern would need
|,.*,\w, or[a-z], switch torginstead. Don't try to force ast-grep into a regex shape. --langis required for stdin. When piping with--stdin, set--langexplicitly;sgcannot infer from extension.- Linux: prefer
ast-grepoversgbecausesgcollides withsetgroups. The helper handles this; if you callsgdirectly, alias it:alias sg=ast-grep.
Вставить в README
Ссылка на карточку для вашего README — без картинок и счётчиков.
[DarkPlay: ast-grep](https://darkplay.ru/ai/skills/code-yeongyu-oh-my-openagent-ast-grep)
Описание
Используйте ast-grep (sg) для AST-осознанного поиска и переписывания кода на 25 языках. Применяйте для структурного сопоставления кода или детерминированных кодмодов: найти все функции/вызовы/классы/импорты вида X, переписать console.log на logger.info, убрать `as any`, мигрировать require() на import, найти пустые блоки catch или пропущенный await, а также сканировать и применять YAML-правила. Предпочитайте этот инструмент rg/grep, когда цель — синтаксическая структура.
Из того же репозитория
Похожие скиллы и другие разделы каталога
Самые востребованные скиллов раздела по установкам и звёздам.
find-skillsvercel-labs · Помогает находить и устанавливать навыки агентов по запросам вроде «как сделать X»…
grill-memattpocock · Настойчивое интервью для доработки плана или дизайна.
hyperframesheygen-com · Обязательная точка входа для любых задач по видео, анимации и motion-графике в…
hyperframes-cliheygen-com · Цикл разработки HyperFrames CLI: init, add, render, preview, publish и другие команды.
agent-browservercel-labs · CLI для автоматизации браузера для ИИ-агентов: навигация по страницам, заполнение форм…
media-useheygen-com · Agent Media OS: единый навык для медиа в HyperFrames — BGM, SFX, изображения, голос, LUT.
hyperframes-registryheygen-com · Установка и подключение блоков реестра в композиции HyperFrames через add и catalog.
grill-with-docsmattpocock · Настойчивое интервью для доработки плана или дизайна, попутно создающее документацию (ADR…
improve-codebase-architecturemattpocock · Поиск в кодовой базе возможностей для углубления, визуальный HTML-отчёт о них и детальный…
tddmattpocock · Разработка через тестирование (TDD): red-green-refactor и интеграционные тесты.Соседние разделы каталога: возможно, нужный инструмент живёт там.
greptileanthropics · Поиск и понимание кодовой базы на основе AI. Запросы к репозиториям на естественном языке…
semgrepanthropics · Semgrep выявляет уязвимости безопасности в реальном времени и помогает Claude писать…
semgrep-rule-creatortrailofbits · Создание пользовательских правил Semgrep для обнаружения шаблонов багов и уязвимостей…
semgrep-rule-variant-creatortrailofbits · Создаёт языковые варианты существующих правил Semgrep с анализом применимости и проверкой…
static-analysistrailofbits · Набор инструментов статического анализа с CodeQL, Semgrep и разбором SARIF для поиска…
Semgrep Scannertrailofbits · Запускает сканирование Semgrep CLI для конкретной языковой категории и формирует вывод…
trailofbits:semgrep-ruletrailofbits · Создаёт правила Semgrep по методологии test-first.
chiselanthropics · Плагин Claude Code для доработки PR на основе автоматического ревью: главным критиком…
claude-crapanthropics · Плагин детерминированного контроля качества для Claude Code. CRAP (Change Risk…
claude-xcindexanthropics · MCP-мост к индексу символов SourceKit в Xcode для Claude Code: субмиллисекундный поиск…Лучшие ИИ-инструменты для программирования
Подборка нейросетей и ИИ-ассистентов для разработки и автоматизации кода. Сервисы, которые ускоряют написание, ревью и отладку, экономят часы рутины и снижают порог входа в сложные
GenAPIGenapi
Notepad++ AINotepad-plus-plus
Creatium MaxCremax
Emacs AIGnu
KiteKite
Amazon Q DeveloperAmazonЛучшие ИИ-инструменты для генерации видео
Подборка нейросетей для генерации и монтажа видео. Инструменты, которые превращают текст в готовые ролики, экономят часы на съемках и постпродакшне. Подходит для SMM-специалистов,
VEED.IOVeed
MootionMootion
Sora (OpenAI)OpenAI
viralpulse.appViralpulse
Viral Maker AIViral-makerЛучшие ИИ-инструменты для генерации текста и копирайтинга
Подборка нейросетей для генерации текстов и копирайтинга. Сервисы, которые помогают создавать статьи, посты и рекламные объявления с нуля или по черновику, экономя часы рутинной ра
NeuroscribeNeuroscribe
GoGPTGogpt
НейроТекстерNeuro-texter
ChatGPTOpenAI
Cosmo ScribeCosmoscribe
Memo HelperCopilot33
LibraChatLibrachat