How JSCodeShift and Recast Automate Codemods

Automated refactoring across large JavaScript codebases relies heavily on codemods powered by Recast and jscodeshift. These tools operate by parsing raw source code into an Abstract Syntax Tree (AST), modifying the tree programmatically, and serializing it back into source code while preserving original comments, whitespace, and formatting. Understanding how these tools handle parsing, node manipulation, and style-preserving generation is essential for building scalable, reliable codebase transformations.

The Codemod Pipeline

The process of running an AST-based codemod follows a three-step lifecycle: Parse, Transform, and Print.

  1. Parse: Source code text is converted into an Abstract Syntax Tree.
  2. Transform: A transformation script queries, modifies, replaces, or deletes specific nodes in the AST.
  3. Print: The modified AST is regenerated back into plain JavaScript source code.

1. Parsing and AST Representation with Recast

Recast handles the heavy lifting of parsing and printing. When Recast parses JavaScript using an underlying parser (such as Babel, Flow, or Esprima), it does not merely generate standard AST nodes conforming to the ESTree specification. It also attaches precise location metadata and tracks original formatting details, tokens, and comments.

Each AST node represents a syntactic construct in the language—such as variable declarations, function calls, binary expressions, or import statements. Recast preserves the original structure by wrapping each node with positional tokens that track where in the file the node was originally located.

2. Querying and Manipulating Nodes with jscodeshift

While Recast provides the low-level parsing and printing infrastructure, jscodeshift provides the execution engine and a high-level, fluent API for traversing and manipulating the AST.

Built on top of ast-types, jscodeshift provides a jQuery-like syntax for AST queries:

3. Non-Destructive Code Generation and Formatting

A major limitation of traditional AST stringifiers is that they reprint the entire file from scratch, stripping away custom whitespace, indentation rules, and comments. Recast solves this through non-destructive printing:

4. Scalable Execution Across Repositories

At scale, running codemods across thousands of files can be resource-intensive. jscodeshift serves as a CLI runner that parallelizes execution across multiple worker processes utilizing all available CPU cores. It provides dry-run capabilities, unified diff previews, and progress reporting, allowing developers to safely test, inspect, and apply sweeping structural changes across massive codebases within seconds.