https://github.com/rajasegar/jscodeshift-collections
Collections for some AST nodes in jscodeshift
https://github.com/rajasegar/jscodeshift-collections
ast codemod codemods collection collections hacktoberfest hacktoberfest-accepted jscodeshift
Last synced: about 1 year ago
JSON representation
Collections for some AST nodes in jscodeshift
- Host: GitHub
- URL: https://github.com/rajasegar/jscodeshift-collections
- Owner: rajasegar
- Created: 2020-03-14T07:15:11.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-09T10:19:47.000Z (about 2 years ago)
- Last Synced: 2025-04-28T22:42:09.732Z (about 1 year ago)
- Topics: ast, codemod, codemods, collection, collections, hacktoberfest, hacktoberfest-accepted, jscodeshift
- Language: JavaScript
- Homepage:
- Size: 1.06 MB
- Stars: 17
- Watchers: 2
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# jscodeshift-collections

[](https://npmjs.org/package/jscodeshift-collections "View this project on npm")
[](https://coveralls.io/github/rajasegar/jscodeshift-collections?branch=master)
[](https://github.com/semantic-release/semantic-release)
[](https://conventionalcommits.org)
Some more Collections for jscodeshift for easy and terse api for writing Codemods
## Install
```
npm install jscodeshift-collections
```
## List of collections
- [Functiondeclaration](#functiondeclaration)
- [CallExpression](#callexpression)
- [Importdeclaration](#importdeclaration)
- [ClassDeclaration](#classdeclaration)
## Usage
```js
const jscsCollections = require('jscodeshift-collections');
module.exports = function(fileInfo, api) {
const j = api.jscodeshift;
jscsCollections.registerCollections(j);
return j(fileInfo.source)
.findFunctionDeclarations('foo')
.renameTo('bar')
.toSource();
}
```
## Example
Say for example, if you want to rename a function declaration `foo` to `bar`
### Transform with new Collection API
```js
j.findFunctionDeclarations('foo')
.renameTo('bar')
```
### Before
```js
function foo() {
}
```
### After
```js
function bar() {
}
```
## List of Collections:
### FunctionDeclaration
```js
// Find all function declarations
j.findFunctionDeclarations()
// Find all function declarations with name=foo and rename them to bar
j.findFunctionDeclarations('foo')
.renameTo('xyz');
// Find and remove params
j.findFunctionDeclarations('bar')
.removeParam('a');
// Find and add params
j.findFunctionDeclarations('bar')
.addParam('d');
```
### CallExpression
```js
// Find all call expressions
j.findCallExpressions();
// Find all member expressions
j.findCallExpressions('foo.bar');
// Find all nested member expressions
j.findCallExpressions('foo.bar.baz');
// Find and rename call expressions
j.findCallExpressions('foo')
.renameTo('xyz');
// Find and rename member expressions
j.findCallExpressions('foo')
.renameTo('foo.bar');
// Find and remove params
j.findCallExpressions('bar')
.removeParam('a');
// Find and add params
j.findCallExpressions('bar')
.addParam('d');
```
### ImportDeclaration
```js
// Find all import declarations
j.findImportDeclarations();
// Find and rename
j.findImportDeclarations('a')
.renameTo('xyz');
// Find and remove specifiers
j.findImportDeclarations('a')
.removeSpecifier('a');
// Find and add specifiers
j.findImportDeclarations('a')
.addSpecifier('c');
```
### ClassDeclaration
```js
// Find all class declarations
j.findClassdeclarations();
// Find and rename
j.findClassDeclarations('foo').renameTo('bar')
```