JSDoc: Class: Collection

# Class: Collection

## 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)

##### 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:
  - traversalMethods
  - mutationMethods
  - transformMethods
  - globalMethods
Source:
  - Collection.js, line 32

### Members

#### length

Returns the number of elements in this collection.

Source:
  - Collection.js, line 126

### 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:
  - Collection.js, line 172

##### Returns:

Type
  Collection

#### childElements() → {JSXElementCollection}

Returns all children that are JSXElements.

Mixes In:
  - traversalMethods.childElements
Source:
  - collections/JSXElement.js, line 155

##### Returns:

Type
  JSXElementCollection

#### childNodes() → {Collection}

Returns all child nodes, including literals and expressions.

Mixes In:
  - traversalMethods.childNodes
Source:
  - collections/JSXElement.js, line 138

##### 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:
  - traversalMethods.closest
Source:
  - collections/Node.js, line 78

##### 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:
  - traversalMethods.closestScope
Source:
  - collections/Node.js, line 67

##### Returns:

Type
  Collection

#### filter(callback) → {Collection}

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

##### Parameters:

Name | Type | Description
--- | --- | ---
`callback` | function |

Source:
  - Collection.js, line 64

##### Returns:

Type
  Collection

#### find() → {Collection}

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

##### Parameters:

Type | Description
--- | ---
type |
filter |

Mixes In:
  - traversalMethods.find
Source:
  - collections/Node.js, line 34

##### Returns:

Type
  Collection

#### findJSXElements(name) → {Collection}

Finds all JSXElements optionally filtered by name

##### Parameters:

Name | Type | Description
--- | --- | ---
`name` | string |

Mixes In:
  - globalMethods.findJSXElements
Source:
  - collections/JSXElement.js, line 37

##### 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:
  - globalMethods.findJSXElementsByModuleName
Source:
  - collections/JSXElement.js, line 51

#### findVariableDeclarators(name) → {Collection}

Finds all variable declarators, optionally filtered by name.

##### Parameters:

Name | Type | Description
--- | --- | ---
`name` | string |

Mixes In:
  - globalMethods.findVariableDeclarators
Source:
  - collections/VariableDeclarator.js, line 35

##### Returns:

Type
  Collection

#### forEach(callback) → {Collection}

Executes callback for each node/path in the collection.

##### Parameters:

Name | Type | Description
--- | --- | ---
`callback` | function |

Source:
  - Collection.js, line 74

##### Returns:

The collection itself

Type
  Collection

#### get()

Proxies to NodePath#get of the first path.

##### Parameters:

Name | Type | Description
--- | --- | ---
`...fields` | string | number |

Source:
  - Collection.js, line 187

#### 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:
  - Collection.js, line 204

##### 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:
  - traversalMethods.getVariableDeclarators
Source:
  - collections/Node.js, line 105

##### 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:
  - mutationMethods.insertAfter
Source:
  - collections/Node.js, line 169

##### 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:
  - mutationMethods.insertBefore
Source:
  - collections/Node.js, line 155

##### Returns:

Type
  Collection

#### isOfType(type) → {boolean}

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

##### Parameters:

Name | Type | Description
--- | --- | ---
`type` | Type |

Source:
  - Collection.js, line 214

##### 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:
  - Collection.js, line 94

#### nodes() → {Array}

Returns an array of AST nodes in this collection.

Source:
  - Collection.js, line 135

##### Returns:

Type
  Array

#### renameTo(newName) → {Collection}

Renames a variable and all its occurrences.

##### Parameters:

Name | Type | Description
--- | --- | ---
`newName` | string |

Mixes In:
  - transformMethods.renameTo
Source:
  - collections/VariableDeclarator.js, line 79

##### 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:
  - mutationMethods.replaceWith
Source:
  - collections/Node.js, line 141

##### Returns:

Type
  Collection

#### size() → {number}

Returns the number of elements in this collection.

Source:
  - Collection.js, line 117

##### Returns:

Type
  number