Recast is a JavaScript syntax tree transformer that preserves original formatting when reprinting modified code and automatically generates source maps.
The tool solves the problem of code transformation while maintaining readability and style. When you parse JavaScript code with Recast, it creates a shadow copy of the abstract syntax tree and tracks the original source through a special property on each node. As you modify the tree, Recast's print function detects which nodes changed and reprints only those parts using a generic pretty printer, leaving everything else exactly as it was in the original source. If no modifications touch a section of code, that section is guaranteed to remain byte-for-byte identical to the input.
Recast suits projects that need to transform code while preserving developer intent and formatting choices—particularly useful for code generators, linters, refactoring tools, and automated transformations where maintaining the original style matters. The tool works with any parser that implements a parse method; while it defaults to Esprima for standard JavaScript, you can configure it to use parsers that support TypeScript, Flow, or other syntax extensions by passing a custom parser to the parse function. The key requirement is calling Recast's parse method rather than parsing separately, since that's what enables the original-tracking mechanism that powers the conservative reprinting.
Development activity shows consistent maintenance with regular CI workflows validating changes. The project maintains active community engagement through a chat channel for discussion and questions. Documentation is thorough, with detailed guidance on the AST API through the ast-types module and clear examples demonstrating both the conservative reprinting behavior and the fallback pretty-printer for cases where modifications cannot use original source text.