name: ast-grep-codemods
description: Search and refactor code structurally with ast-grep (Tree-sitter AST rules). Replace multi-line call sites without corrupting comments or strings, preview diffs first, and verify YAML rules.
category: Document Processing
source: antigravity
tags: [typescript, node, api, ai, agent, document, cro]
url: https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/ast-grep-codemods
ast-grep Codemods
When to Use
- Migrate deprecated APIs, rename call signatures, or enforce structural lint rules across many files where regex search-and-replace breaks on multi-line arguments, comments, or string literals.
- Author reusable YAML
ast-grep rules with pattern, fix, not, or inside constraints.
- Preview and apply repeatable repository-wide codemods safely with dry-run diffs before writing files.
Prerequisites
Verify ast-grep (sg or ast-grep) is installed and check its version (ast-grep --version), target languages, and git working-tree cleanliness. Keep edits in a clean git branch so codemod diffs can be reviewed and reverted cleanly.
Author structural patterns instead of regexes
- Use Tree-sitter metavariables:
$VAR matches a single AST node (an identifier, expression, or literal), $$$ARGS matches zero or more sibling nodes (such as variadic argument lists), and $_ matches an anonymous node without capturing.
- When writing YAML rule files (
rule.yml), quote any pattern or fix value that contains : or braces (for example fix: "apiClient.request({ url: $URL, options: $OPTS })"). Unquoted colons inside object literals cause YAML parse errors.
- Narrow broad matches with relational clauses (
inside, has, not) and constraints so only genuine call sites match (for example excluding the helper's own definition or test mocks).
- Inspect ambiguous syntax with
ast-grep run --debug-query=ast -l <lang> -p '<snippet>' when a pattern fails to match the expected AST node kind.
Preview, apply, and verify codemods
- Always run a read-only preview first:
ast-grep scan --rule rule.yml <paths> (or --json=compact to audit match counts) and inspect the proposed diff before modifying source files.
- Apply changes only after verifying the preview (
--update-all, or -i for interactive review on risky refactors).
- Follow every applied codemod with the project's formatter, type checker, and test suite.
ast-grep rewrites syntax structurally; it does not resolve cross-file types or imports automatically unless your rule explicitly handles them.
Example
Migrate multi-line legacyFetch(url, opts) calls in TypeScript to apiClient.request({ url, options }) without altering comments or string literals that mention legacyFetch.
Run the worked TypeScript structural rewrite to verify the quoted YAML rule, dry-run diff output, and --update-all result.
Limitations
ast-grep matches syntax trees per file, not semantic symbol tables or resolved TypeScript/Rust types; a different function with the same local name can match unless constrained by context or import checks.
- Rewritten multi-line expressions may need a follow-up pass from the project formatter (such as Biome, Prettier, or
rustfmt).
Safety and Side Effects
Always start with a clean git working tree and run ast-grep scan without --update-all first. Limit target paths to authorized source directories so generated bundles, vendored dependencies, or fixtures are not rewritten unintentionally.
Related Skills and Documentation
1---2name: ast-grep-codemods-23description: Search and refactor code structurally with ast-grep (Tree-sitter AST rules). Replace multi-line call sites without corrupting comments or strings, preview diffs4---5---6name: ast-grep-codemods7description: Search and refactor code structurally with ast-grep (Tree-sitter AST rules). Replace multi-line call sites without corrupting comments or strings, preview diffs first, and verify YAML rules. 8category: Document Processing9source: antigravity10tags: [typescript, node, api, ai, agent, document, cro]11url: https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/ast-grep-codemods12---131415# ast-grep Codemods1617## When to Use1819- Migrate deprecated APIs, rename call signatures, or enforce structural lint rules across many files where regex search-and-replace breaks on multi-line arguments, comments, or string literals.20- Author reusable YAML `ast-grep` rules with `pattern`, `fix`, `not`, or `inside` constraints.21- Preview and apply repeatable repository-wide codemods safely with dry-run diffs before writing files.2223## Prerequisites2425Verify `ast-grep` (`sg` or `ast-grep`) is installed and check its version (`ast-grep --version`), target languages, and git working-tree cleanliness. Keep edits in a clean git branch so codemod diffs can be reviewed and reverted cleanly.2627## Author structural patterns instead of regexes28291. Use Tree-sitter metavariables: `$VAR` matches a single AST node (an identifier, expression, or literal), `$$$ARGS` matches zero or more sibling nodes (such as variadic argument lists), and `$_` matches an anonymous node without capturing.302. When writing YAML rule files (`rule.yml`), **quote any `pattern` or `fix` value that contains `: ` or braces** (for example `fix: "apiClient.request({ url: $URL, options: $OPTS })"`). Unquoted colons inside object literals cause YAML parse errors.313. Narrow broad matches with relational clauses (`inside`, `has`, `not`) and `constraints` so only genuine call sites match (for example excluding the helper's own definition or test mocks).324. Inspect ambiguous syntax with `ast-grep run --debug-query=ast -l <lang> -p '<snippet>'` when a pattern fails to match the expected AST node kind.3334## Preview, apply, and verify codemods3536- Always run a read-only preview first: `ast-grep scan --rule rule.yml <paths>` (or `--json=compact` to audit match counts) and inspect the proposed diff before modifying source files.37- Apply changes only after verifying the preview (`--update-all`, or `-i` for interactive review on risky refactors).38- Follow every applied codemod with the project's formatter, type checker, and test suite. `ast-grep` rewrites syntax structurally; it does not resolve cross-file types or imports automatically unless your rule explicitly handles them.3940## Example4142> Migrate multi-line `legacyFetch(url, opts)` calls in TypeScript to `apiClient.request({ url, options })` without altering comments or string literals that mention `legacyFetch`.4344Run the [worked TypeScript structural rewrite](references/structural-rewrite.md) to verify the quoted YAML rule, dry-run diff output, and `--update-all` result.4546## Limitations4748- `ast-grep` matches syntax trees per file, not semantic symbol tables or resolved TypeScript/Rust types; a different function with the same local name can match unless constrained by context or import checks.49- Rewritten multi-line expressions may need a follow-up pass from the project formatter (such as Biome, Prettier, or `rustfmt`).5051## Safety and Side Effects5253Always start with a clean git working tree and run `ast-grep scan` without `--update-all` first. Limit target paths to authorized source directories so generated bundles, vendored dependencies, or fixtures are not rewritten unintentionally.5455## Related Skills and Documentation5657- For deciding refactor scope, risk tiers, and domain boundaries before editing, see [Repo-Native Refactor](https://github.com/sickn33/agentic-awesome-skills/blob/main/skills/repo-native-refactor/SKILL.md).58- For behavior-preservation checklists during cleanup, see [Codebase Cleanup Refactor](https://github.com/sickn33/agentic-awesome-skills/blob/main/skills/codebase-cleanup-refactor-clean/SKILL.md).59- See the official [ast-grep documentation](https://ast-grep.github.io/), [rule configuration guide](https://ast-grep.github.io/guide/rule-config.html), and [metavariable reference](https://ast-grep.github.io/guide/pattern-syntax.html).