Recast parses JavaScript into mutable ASTs and reprints code so only changed nodes lose original formatting. It can emit high-resolution source maps alongside generic pretty printing.
Overview
Developers use Recast to run codemods and refactors by parsing with recast.parse, transforming nodes via ast-types helpers, and printing with recast.print. Shadow copies retain .original links that let the printer reuse untouched source text.
When no original text exists for a changed node, Recast falls back to a generic pretty printer with configurable options. Alternate parsers such as Babel, TypeScript, Flow, or acorn plug in through a parser option while preserving formatting metadata.
Source map generation tracks which output characters came from which inputs, enabling line-accurate debugging after automated edits. Motivation docs emphasize large-scale, repeatable codebase updates driven from syntax trees instead of brittle text patches.
The npm package supports modern ES module imports and CommonJS require patterns. Options are documented for fine-grained print behavior.
Key features
- Parse JavaScript to mutable ASTs
- Conservative reprinting of unchanged code
- Pluggable parser backends
- Automatic source map generation
- ast-types structural helpers
- Generic pretty-printer fallback
- TypeScript and Flow parser paths
- Codemod-oriented workflow
Use cases
- Large-scale JavaScript refactors
- Automated API migrations
- ESLint-style autofix pipelines
- Build-time codemods in CI
- Exploratory AST manipulation scripts