A JavaScript syntax tree transformer that preserves original formatting and generates source maps automatically.
Recast is a JavaScript syntax tree transformer and pretty-printer that enables code manipulation while preserving original formatting. It parses JavaScript code into an abstract syntax tree (AST), allows developers to modify the AST, and reprints it with minimal changes to unmodified parts, maintaining code style and automatically generating source maps.
JavaScript and TypeScript developers building code transformation tools, such as codemods, linters, formatters, or compilers, who need to manipulate source code while preserving its original formatting and generating accurate source maps.
Developers choose Recast over alternatives because it reprints only modified parts of the syntax tree, preserving formatting for unmodified code, and automatically generates high-resolution source maps. Its parser-agnostic design supports multiple parsers like Esprima, Acorn, TypeScript, Flow, and Babel, offering flexibility for different syntax needs.
JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Reprints only modified AST nodes, keeping original formatting for unmodified code, as shown in the example where comments and indentation are retained after transforming a function declaration.
Generates high-resolution source maps automatically during printing, enabling precise debugging by tracking character-level changes between original and generated code without manual configuration.
Supports multiple parsers including Esprima, Acorn, TypeScript, Flow, and Babel, allowing flexibility to handle different JavaScript syntaxes and extensions through configurable parse options.
Provides builders and type-checkers from the ast-types module for constructing and validating AST nodes, ensuring manipulations adhere to the Mozilla Parser API and reducing errors.
Using parsers other than the default Esprima requires installing separate npm packages (e.g., @babel/parser), adding setup complexity and increasing project dependency management overhead.
Creates character-by-character source maps that do not include identifier names, resulting in larger file sizes compared to token-based maps, which can impact debugging performance in browsers.
When unable to preserve original formatting, Recast defaults to a generic pretty printer, potentially introducing stylistic inconsistencies in the output code, as mentioned in the fallback mechanism.