packages/codemods/instructions.md
jscodeshiftThe main function that returns the jscodeshift instance.
source (String)const j = jscodeshift(sourceCode);
findFinds nodes that match the provided type.
type (String or Function)const variableDeclarations = j.find(j.VariableDeclaration);
findImportDeclarationsFinds all ImportDeclarations optionally filtered by name.
sourcePath (String)const routerImports = j.findImportDeclarations("react-router-dom");
closestScopeFinds the closest enclosing scope of a node.
const closestScopes = j.find(j.Identifier).closestScope();
closestFinds the nearest parent node that matches the specified type.
type (String or Function)const closestFunction = j.find(j.Identifier).closest(j.FunctionDeclaration);
getVariableDeclaratorsRetrieves variable declarators from the current collection.
callback (Function)const variableDeclarators = j
.find(j.Identifier)
.getVariableDeclarators((path) => path.value.name);
findVariableDeclaratorsFinds variable declarators by name.
name (String)const variableDeclarators = j.findVariableDeclarators("a");
filterFilters nodes based on a predicate function.
predicate (Function)const constDeclarations = j
.find(j.VariableDeclaration)
.filter((path) => path.node.kind === "const");
forEachIterates over each node in the collection.
callback (Function)j.find(j.VariableDeclaration).forEach((path) => {
console.log(path.node);
});
someChecks if at least one element in the collection passes the test.
callback (Function)const hasVariableA = root
.find(j.VariableDeclarator)
.some((path) => path.node.id.name === "a");
everyChecks if all elements in the collection pass the test.
callback (Function)const allAreConst = root
.find(j.VariableDeclaration)
.every((path) => path.node.kind === "const");
mapMaps each node in the collection to a new value.
callback (Function)const variableNames = j
.find(j.VariableDeclaration)
.map((path) => path.node.declarations.map((decl) => decl.id.name));
sizeReturns the number of nodes in the collection.
const numberOfNodes = j.find(j.VariableDeclaration).size();
lengthReturns the number of elements in the collection.
const varCount = root.find(j.VariableDeclarator).length;
nodesReturns the AST nodes in the collection.
const nodes = j.find(j.VariableDeclaration).nodes();
pathsReturns the paths of the found nodes.
const paths = j.find(j.VariableDeclaration).paths();
getASTReturns the root AST node of the collection.
const ast = root.getAST();
getGets the first node in the collection.
const firstVariableDeclaration = j.find(j.VariableDeclaration).get();
atNavigates to a specific path in the AST.
index (Number)const secondVariableDeclaration = j.find(j.VariableDeclaration).at(1);
getTypesReturns the set of node types present in the collection.
const types = root.find(j.VariableDeclarator).getTypes();
isOfTypeChecks if the node in the collection is of a specific type.
type (String)const isVariableDeclarator = root
.find(j.VariableDeclarator)
.at(0)
.isOfType("VariableDeclarator");
replaceWithReplaces the current node(s) with a new node.
newNode (Node or Function)j.find(j.Identifier).replaceWith((path) =>
j.identifier(path.node.name.toUpperCase())
);
insertBeforeInserts a node before the current node.
newNode (Node)j.find(j.FunctionDeclaration).insertBefore(
j.expressionStatement(j.stringLiteral("Inserted before"))
);
insertAfterInserts a node after the current node.
newNode (Node)j.find(j.FunctionDeclaration).insertAfter(
j.expressionStatement(j.stringLiteral("Inserted after"))
);
removeRemoves the current node(s).
j.find(j.VariableDeclaration).remove();
renameToRenames the nodes in the collection to a new name.
newName (String)root.find(j.Identifier, { name: "a" }).renameTo("x");
toSourceConverts the transformed AST back to source code.
options (Object)const transformedSource = j.toSource({ quote: "single" });
jscodeshift provides 278 node types which are mapped to their corresponding node type in ast-types.
this expression.any keyword.boolean keyword.number keyword.string keyword.For a complete list and detailed structure of each node, refer to the AST Grammar documentation.