jscodeshift has around 25 APIs to help developers easily detect and transform any JS/TS code.

Generally, creating a codemod involves two main tasks: detection and transformation.

  1. Detecting a specific pattern in a large codebase can be expensive, so this task is often divided into two passes.
    1. In the first pass, we perform an initial scope reduction of the AST nodes to significantly reduce the search space and produce a collection of AST nodes to process.
    2. In the second pass, we process and filter the nodes collection to pinpoint the specific AST nodes that need transformation.
  2. Once we detect the desired nodes, we transform the AST and produce the modified code.

For jscodeshift, we have a set of APIs for each part of the codemod process (initial traversal, filtering, transformation), as detailed below. jscodeshift accepts —parser as argument. We can select from the list of parser that are currently supported, all those parsers should be compatible with estree spec and have same AST grammar. It's important to know the AST grammar for describing the nodes in the codemod.

Refer to this link for ES5, and this link for babel-parser AST node definitions.

Core API

Node Traversal APIs

Node Transformation APIs

Screenshot 2024-06-09 at 9.28.19 PM.png


OTHER