Skip to content

jscodeshift Documentation

Jeremy Greer edited this page May 6, 2016 · 3 revisions

Intro

TODO

NodePaths

NodePaths (aka Paths) wrap the actual AST node and provide information that is not available when looking at the node in isolation. Refer to ast-types for a more elaborate description.

parent

The wrapped AST node's parent, wrapped in another NodePath.

scope

Scope information about the wrapped AST node. See ast-types for more info.

node

The wrapped AST node.

value

Same as node

prune

Collections

A Collection is a generic collection of NodePaths. It only has a generic API to access and process the elements of the list. It doesn't know anything about AST types.

filter

Returns a new collection containing the nodes for which the callback returns true.

forEach

Executes callback for each node/path in the collection.

map

Executes the callback for every path in the collection and returns a new collection from the return values (which must be paths).

The callback can return null to indicate to exclude the element from the new collection.

If an array is returned, the array will be flattened into the result collection.

size

Returns the number of elements in this collection.

nodes

Returns an array of AST nodes in this collection.

at

Returns a new collection containing only the element at position index.

In case of a negative index, the element is taken from the end:

  • .at(0) - first element
  • .at(-1) - last element

get

Proxies to NodePath#get of the first path.

isOfType

Returns true if this collection has the type 'type'.

toSource

Returns pretty printed JS code.

Extensions

TODO

Node

find

Find nodes of a specific type within the nodes of this collection.

closestScope

Returns a collection containing the paths that create the scope of the currently selected paths. Dedupes the paths.

closest

Traverse the AST up and finds the closest node of the provided type.

getVariableDeclarators

Finds the declaration for each selected path. Useful for member expressions or JSXElements. Expects a callback function that maps each path to the name to look for.

If the callback returns a falsey value, the element is skipped.

replaceWith

Simply replaces the selected nodes with the provided node. If a function is provided it is executed for every node and the node is replaced with the functions return value.

insertBefore

Inserts a new node before the current one.

insertAfter

Inserts a new node after the current one.

remove

Removes all the paths in the current collection from the AST.

VariableDeclarator

findVariableDeclarators

Finds all variable declarators, optionally filtered by name.

requiresModule

Returns a function that returns true if the provided path is a variable declarator and requires one of the specified module names.

renameTo

Renames a variable and all its occurrences.

JSX

findJSXElements

Finds all JSXElements optionally filtered by name

findJSXElementsByModuleName

Finds all JSXElements by module name. Given

var Bar = require('Foo');
<Bar />

findJSXElementsByModuleName('Foo') will find , without having to know the variable name.

hasAttributes

Filter method for attributes.

hasChildren

Filter elements which contain a specific child type

childNodes

Returns all child nodes, including literals and expressions.

childElements

Returns all children that are JSXElements.

getRootName

Given a JSXElement, returns its "root" name. E.g. it would return Foo for both <Foo /> and <Foo.Bar />.

Templates

TODO

statement

statements

expresssion