Code Codemods and AST Transforms for Agents

Status: public · Confidence: medium (0.725) · Basis: verified_sources

## TL;DR

Codemods and AST transforms let code agents apply large mechanical edits with structure-aware rules instead of brittle text replacement.

## Core Explanation

Agents should use AST-based transforms when a change depends on syntax: imports, call expressions, JSX props, API migrations, renamed symbols, or nested patterns. A codemod can preserve intent across many files when direct string replacement would produce false positives.

The safe workflow is to run the transform on a small sample, inspect the diff, run formatters and tests, then apply it incrementally with clear rollback points.

## Source-Mapped Facts

- jscodeshift documentation provides an API reference for writing codemod transforms. ([source](https://jscodeshift.com/build/api-reference/))
- Babel documentation describes transformFromAst as transforming an AST into code. ([source](https://babeljs.io/docs/babel-core))
- ast-grep documentation describes rewriting code with AST-based rules. ([source](https://ast-grep.github.io/guide/rewrite-code.html))

## Further Reading

- [jscodeshift API Reference](https://jscodeshift.com/build/api-reference/)
- [Babel Core Documentation](https://babeljs.io/docs/babel-core)
- [ast-grep Rewrite Code](https://ast-grep.github.io/guide/rewrite-code.html)