Introduction
jscodeshift
is a toolkit for building and running codemods over multiple JavaScript or TypeScript files. It provides:
- A runner, which executes the provided transform for each file passed to it. It also outputs a summary of how many files have not been transformed.
- A wrapper around recast, providing a different API. Recast is an AST-to-AST transform tool and also tries to preserve the style of original code as much as possible.
How does jscodeshift work?
-
Parsing Code into AST
First, jscodeshift takes your JavaScript code and converts it into an Abstract Syntax Tree (AST). An AST is a tree representation of your code where each node represents a different part of the code, like variables, functions, and expressions.
-
Transforming the AST
Using jscodeshift, you can navigate through the AST and apply transformations. For example, you can find all instances of a certain function and rename it or change its parameters.
-
Generating Code from AST
After transforming the AST, jscodeshift converts it back into JavaScript code. The result is your original code with the specified changes applied.
Installation
Get jscodeshift from npm:
This will install the runner as jscodeshift
.