Class: Collection

Collection(paths, parent, types) → {Collection}

This represents a generic collection of node paths. It only has a generic API to access and process the elements of the list. It doesn't know anything about AST types.

Constructor

new Collection(paths, parent, types) → {Collection}

Parameters:
Name Type Description
paths Array An array of AST paths
parent Collection A parent collection
types Array An array of types all the paths in the collection have in common. If not passed, it will be inferred from the paths.
Mixes In:
Source:
Returns:
Type
Collection

Members

length

Returns the number of elements in this collection.
Source:

Methods

at(index) → {Collection}

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
Parameters:
Name Type Description
index number
Source:
Returns:
Type
Collection

childElements() → {JSXElementCollection}

Returns all children that are JSXElements.
Mixes In:
Source:
Returns:
Type
JSXElementCollection

childElements() → {JSXElementCollection}

Returns all children that are JSXElements.
Mixes In:
Source:
Returns:
Type
JSXElementCollection

childNodes() → {Collection}

Returns all child nodes, including literals and expressions.
Mixes In:
Source:
Returns:
Type
Collection

childNodes() → {Collection}

Returns all child nodes, including literals and expressions.
Mixes In:
Source:
Returns:
Type
Collection

closest() → {Collection}

Traverse the AST up and finds the closest node of the provided type.
Parameters:
Type Description
Collection
filter
Mixes In:
Source:
Returns:
Type
Collection

closest() → {Collection}

Traverse the AST up and finds the closest node of the provided type.
Parameters:
Type Description
Collection
filter
Mixes In:
Source:
Returns:
Type
Collection

closestScope() → {Collection}

Returns a collection containing the paths that create the scope of the currently selected paths. Dedupes the paths.
Mixes In:
Source:
Returns:
Type
Collection

closestScope() → {Collection}

Returns a collection containing the paths that create the scope of the currently selected paths. Dedupes the paths.
Mixes In:
Source:
Returns:
Type
Collection

every(callback) → {boolean}

Tests whether all paths pass the test implemented by the provided callback.
Parameters:
Name Type Description
callback function
Source:
Returns:
Type
boolean

filter(callback) → {Collection}

Returns a new collection containing the nodes for which the callback returns true.
Parameters:
Name Type Description
callback function
Source:
Returns:
Type
Collection

find() → {Collection}

Find nodes of a specific type within the nodes of this collection.
Parameters:
Type Description
type
filter
Mixes In:
Source:
Returns:
Type
Collection

find() → {Collection}

Find nodes of a specific type within the nodes of this collection.
Parameters:
Type Description
type
filter
Mixes In:
Source:
Returns:
Type
Collection

findJSXElements(name) → {Collection}

Finds all JSXElements optionally filtered by name
Parameters:
Name Type Description
name string
Mixes In:
Source:
Returns:
Type
Collection

findJSXElements(name) → {Collection}

Finds all JSXElements optionally filtered by name
Parameters:
Name Type Description
name string
Mixes In:
Source:
Returns:
Type
Collection

findJSXElementsByModuleName()

Finds all JSXElements by module name. Given var Bar = require('Foo'); findJSXElementsByModuleName('Foo') will find , without having to know the variable name.
Mixes In:
Source:

findJSXElementsByModuleName()

Finds all JSXElements by module name. Given var Bar = require('Foo'); findJSXElementsByModuleName('Foo') will find , without having to know the variable name.
Mixes In:
Source:

findVariableDeclarators(name) → {Collection}

Finds all variable declarators, optionally filtered by name.
Parameters:
Name Type Description
name string
Mixes In:
Source:
Returns:
Type
Collection

findVariableDeclarators(name) → {Collection}

Finds all variable declarators, optionally filtered by name.
Parameters:
Name Type Description
name string
Mixes In:
Source:
Returns:
Type
Collection

forEach(callback) → {Collection}

Executes callback for each node/path in the collection.
Parameters:
Name Type Description
callback function
Source:
Returns:
The collection itself
Type
Collection

get()

Proxies to NodePath#get of the first path.
Parameters:
Name Type Description
...fields string | number
Source:

getTypes() → {Array.<string>}

Returns the type(s) of the collection. This is only used for unit tests, I don't think other consumers would need it.
Source:
Returns:
Type
Array.<string>

getVariableDeclarators(nameGetter) → {Collection}

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.
Parameters:
Name Type Description
nameGetter function
Mixes In:
Source:
Returns:
Type
Collection

getVariableDeclarators(nameGetter) → {Collection}

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.
Parameters:
Name Type Description
nameGetter function
Mixes In:
Source:
Returns:
Type
Collection

insertAfter(insert) → {Collection}

Inserts a new node after the current one.
Parameters:
Name Type Description
insert Node | Array.<Node> | function
Mixes In:
Source:
Returns:
Type
Collection

insertAfter(insert) → {Collection}

Inserts a new node after the current one.
Parameters:
Name Type Description
insert Node | Array.<Node> | function
Mixes In:
Source:
Returns:
Type
Collection

insertBefore(insert) → {Collection}

Inserts a new node before the current one.
Parameters:
Name Type Description
insert Node | Array.<Node> | function
Mixes In:
Source:
Returns:
Type
Collection

insertBefore(insert) → {Collection}

Inserts a new node before the current one.
Parameters:
Name Type Description
insert Node | Array.<Node> | function
Mixes In:
Source:
Returns:
Type
Collection

isOfType(type) → {boolean}

Returns true if this collection has the type 'type'.
Parameters:
Name Type Description
type Type
Source:
Returns:
Type
boolean

map(callback, type)

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.
Parameters:
Name Type Description
callback function
type Type Force the new collection to be of a specific type
Source:

nodes() → {Array}

Returns an array of AST nodes in this collection.
Source:
Returns:
Type
Array

renameTo(newName) → {Collection}

Renames a variable and all its occurrences.
Parameters:
Name Type Description
newName string
Mixes In:
Source:
Returns:
Type
Collection

renameTo(newName) → {Collection}

Renames a variable and all its occurrences.
Parameters:
Name Type Description
newName string
Mixes In:
Source:
Returns:
Type
Collection

replaceWith(nodes) → {Collection}

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.
Parameters:
Name Type Description
nodes Node | Array.<Node> | function
Mixes In:
Source:
Returns:
Type
Collection

replaceWith(nodes) → {Collection}

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.
Parameters:
Name Type Description
nodes Node | Array.<Node> | function
Mixes In:
Source:
Returns:
Type
Collection

size() → {number}

Returns the number of elements in this collection.
Source:
Returns:
Type
number

some(callback) → {boolean}

Tests whether at-least one path passes the test implemented by the provided callback.
Parameters:
Name Type Description
callback function
Source:
Returns:
Type
boolean