API Reference
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.
-
Detection
Detecting a specific pattern in a large codebase can be expensive, so this task is often divided into two passes.
-
First pass
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.
-
Second pass
In the second pass, we process and filter the nodes collection to pinpoint the specific AST nodes that need transformation.
-
-
Transformation
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 the jscodeshift
node types.
Building jscodeshift codemods
Core API
jscodeshift
The main function that returns the jscodeshift instance.
Parameters: source
(String): The source code to be transformed.
Example:
Node Traversal APIs
Below are APIs that often used in the initial scope reduction phase (source). The input is usually the whole file, and the output is a collection of nodes.
find
Finds nodes that match the provided type.
Parameters: type
(String or Function): The type of nodes to find.
Example:
closestScope
Finds the closest enclosing scope of a node. Useful for determining the scope context of variables and functions.
Example:
closest
Finds the nearest parent node that matches the specified type. The child node must be obtained from a previous function call, such as find.
Parameters:type
(String or Function): The type of ancestor to find.
getVariableDeclarators
Retrieves variable declarators from the current collection. If the callback function returns a falsy value, the element is not included in the result.
Parameters:callback
(Function): A function that returns the name of the variable to find.
Example:
findVariableDeclarators
(source)
Finds variable declarators by name.
Parameters: name
(String): The name of the variable to find.
Example:
Below are the APIs that are often used in the second phase, which is the detailed node filtering (source). The input of this phase is usually a collection of nodes, and the output, is specific nodes to transform.
filter
Filters nodes based on a predicate function.
Parameters: predicate
(Function): A function to test each element.
Example:
forEach
Iterates over each node in the collection.
Parameters: callback
(Function): A function to call for each node.
Example:
some
some
checks if at least one element in the collection passes the test implemented by the provided function.
Parameters: callback
: A function that tests each element. The callback function takes three arguments:
path
: The current element being processed.index
: The index of the current element.array
: The arraysome
was called upon.
Example:
every
every
checks if all elements in the collection pass the test implemented by the provided function.
Parameters: callback
: A function that tests each element. The callback function takes three arguments:
path
: The current element being processed.index
: The index of the current element.array
: The arrayevery
was called upon.
Example:
map
Maps each node in the collection to a new value.
Parameters:callback
(Function): A function to call for each node.
Example:
size
Returns the number of nodes in the collection.
Example:
length
length
returns the number of elements in the collection.
Example:
nodes
Returns the AST nodes in the collection.
Example:
paths
Returns the paths of the found nodes.
Example:
getAST
getAST
returns the root AST node of the collection.
Example:
get
Gets the first node in the collection.
Example:
at
Navigates to a specific path in the AST.
Parameters: index
(Number): The index of the path to navigate to.
Example:
getTypes
getTypes
returns the set of node types present in the collection.
Example:
isOfType
isOfType
checks if the node in the collection is of a specific type.
Parameters: type
: The type to check against.
Example:
Node Transformation APIs
Below are the APIs used in node transformations. (source)
replaceWith
Replaces the current node(s) with a new node.
Parameters: newNode
(Node or Function): The new node or a function that returns a new node.
Example:
insertBefore
Inserts a node before the current node.
Parameters: newNode
(Node): The node to insert.
Example:
insertAfter
Inserts a node after the current node.
Parameters: newNode
(Node): The node to insert.
Example:
remove
Removes the current node(s).
Example:
renameTo
(source)
renameTo
renames the nodes in the collection to a new name.
Parameters: newName
: The new name to rename to.
Example:
These descriptions and examples should give you a clear understanding of how to use each of these jscodeshift APIs.
toSource
Converts the transformed AST back to source code.
Parameters: options
(Object): Optional formatting options.
Example: