/dstack-code:ast-grepfrom DStack Code
Runs AST-based structural search, lint, and rewrite with ast-grep when regex is too fragile. Validates patterns and dry-runs rewrites before applying.
Ships in DStack Code. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Use this when the task is about code shape (a call, function, class, or import that looks a certain way) rather than about text. ast-grep matches syntax trees, not bytes: if you catch yourself reaching for |, .*, \w, or [...], the task is lexical and you want grep instead. If you need types, references, or callers resolved semantically, you want the language server or compiler instead. Only structural work belongs here.
Everything here is local and reversible. Searching reads nothing but the tree; rewriting touches only version-controlled source files, and only through the helper's dry-run-then-apply flow, so git is always your rollback. Nothing here mutates remotes, credentials, or published artifacts.
A pattern, always single-quoted in the shell so $VAR reaches ast-grep unexpanded. A language (--lang) or a target path whose extension detects it (stdin patterns always need --lang). For a rewrite, a rewrite template plus one or more target paths (current directory when omitted). Optional extras: repeatable include/exclude globs (! excludes), context lines, JSON output.
Pattern syntax in brief: $VAR matches any single node, $$$ARGS matches zero or more nodes, $_ matches any node without capturing. A pattern is code, not regex.
One more naming rule, because it bites: invoke ast-grep, never sg (sg collides with the setgroups binary on many systems).
Never search with an unvalidated pattern. Run:
python3 scripts/ast_grep_helper.py validate '<pattern>' --lang <L>
Exit 0 means ast-grep parses it cleanly and you may proceed. Exit 2 means malformed, and the helper prints the parsed pattern tree with the ERROR node marked, so fix the pattern and re-validate. If the ast-grep binary is absent, validation degrades: it checks only for regex smell, warns, and replace will exit 2 without running, so install ast-grep before going further.
If a search unexpectedly matches nothing, work this ladder in order: re-validate the pattern; check the dialect (tsx is not ts, and the wrong one silently matches nothing); run ast-grep run -p '<pattern>' -l <L> --debug-query=pattern and look for ERROR; inspect the target's real tree with --debug-query=ast on a snippet you know should match; reproduce in the online playground. Touch no files until a correct match is confirmed.
A rewrite is a dry run first, eyes on the diff, and only then an apply:
python3 scripts/ast_grep_helper.py replace '<pattern>' '<rewrite>' --lang <L> <paths>
python3 scripts/ast_grep_helper.py replace '<pattern>' '<rewrite>' --lang <L> <paths> --apply
Read the dry-run diff and the N matches across M files count. If the blast radius is wrong (too wide, too narrow, the wrong shape), stop. Tighten meta-variables, add --lang, add context, and re-run the dry run. Never --apply a diff you have not approved. The helper writes through a separate --update-all pass; keep it that way, because combining --json and --update-all makes --json silently win and the write is dropped with no error.
Before a structural migration, inventory the distinct syntactic roles carrying the changed contract: declarations, construction expressions, destructuring patterns, reconstruction paths, accessors. A pattern matching one role does not cover the others, so review an example from every affected role and record how each migrates. Match counts never prove semantic completeness.
If the apply pass fails partway, whatever ast-grep wrote stays written: never report done. Revert the version-controlled targets and re-run the full dry-run/apply sequence.
Complex tasks graduate to YAML rule files (sgconfig.yml, ast-grep new project) with rule, fix, inside, any, and matches fields; drive those with ast-grep scan / run / test directly rather than the helper.
validate exits 0 for a clean pattern, 2 for a malformed one (with the pattern debug tree), plus advisory regex-smell hints on stderr. A dry-run replace prints a compact unified diff and an N matches across M files summary and modifies nothing. replace --apply updates the version-controlled files via --update-all and prints a confirmation line. A direct ast-grep search reports matched code locations.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
ast-grep, structural-search, code-rewrite, pattern-matching, lint
5 skills in this plugin
/deps-upgradeUse when dependency upgrades need batching for CVEs, a major release, forced compatibility, a scheduled or vulnerability-triggered sweep./dimensional-analysisUse when code mixes units, fixed-point precisions, scaling factors, rates, prices, shares, or conversions. Not for type-level unit modeling: use type-driven./first-load-byte-reductionUse when a first screen needs lower transfer bytes without visual or behavioral change. Not for visual redesign or behavioral changes./slicing-code-contextUse when an exact symbol, path, entrypoint, or line range can bound a focused code question or patch proposal under a fixed source budget. Not for source changes or broad repository exploration.npx claudepluginhub gosuda/dstack-skills --plugin dstack-codeOther plugins' skills for the same job