Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. ES6 Tools
  3. Recast

Recast

MITTypeScriptv0.21.1

A JavaScript syntax tree transformer that preserves original formatting and generates source maps automatically.

GitHubGitHub
5.3k stars361 forks0 contributors

What is Recast?

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.

Target Audience

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.

Value Proposition

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.

Overview

JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator

Use Cases

Best For

  • Writing codemods that refactor code while maintaining existing formatting and comments.
  • Building custom linters or formatters that need to analyze and modify JavaScript or TypeScript code with minimal style disruption.
  • Creating source code transformation tools that require accurate source map generation for debugging.
  • Experimenting with AST manipulations in a safe, reversible way using version control to revert changes.
  • Integrating multiple parsers (e.g., Babel, TypeScript) into a single code processing pipeline without losing original formatting.
  • Developing educational or analysis tools that visualize code changes by comparing original and modified syntax trees.

Not Ideal For

  • Projects performing simple text replacements or regex-based edits where AST parsing is unnecessary overhead
  • Teams enforcing a strict, uniform code style across the entire codebase, as Recast's fallback to generic formatting can introduce inconsistencies
  • Environments with strict dependency constraints, since using parsers like Babel or TypeScript requires additional npm package installations

Pros & Cons

Pros

Formatting Preservation

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.

Automatic Source Maps

Generates high-resolution source maps automatically during printing, enabling precise debugging by tracking character-level changes between original and generated code without manual configuration.

Parser Agnostic Design

Supports multiple parsers including Esprima, Acorn, TypeScript, Flow, and Babel, allowing flexibility to handle different JavaScript syntaxes and extensions through configurable parse options.

AST Type Safety

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.

Cons

External Parser Dependencies

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.

Large Source Map Files

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.

Inconsistent Formatting Fallback

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.

Frequently Asked Questions

Quick Stats

Stars5,252
Forks361
Contributors0
Open Issues169
Last commit27 days ago
CreatedSince 2012

Tags

#syntax-tree#javascript#code-transformation#code-formatting#ast-manipulation#source-maps#pretty-printer

Built With

E
Esprima
B
Babel
J
JavaScript
T
TypeScript
F
Flow

Included in

ES6 Tools4.0k
Auto-fetched 3 hours ago

Related Projects

IspartaIsparta

:skull: A code coverage tool for ES6 (babel/6to5)

Stars644
Forks45
Last commit6 years ago
Paws on ES6Paws on ES6

Minimalist examples of ES6 functionalities.

Stars329
Forks29
Last commit9 years ago
ES.next showcaseES.next showcase

Showcasing real-world usage of ECMAScript 6 (the next JavaScript version) features

Stars299
Forks17
Last commit9 years ago
let-erlet-er

DEPRECATED: Transpile non-ES6 let-blocks into ES6 (or ES3)

Stars187
Forks21
Last commit11 years ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub