Skip to content

rajasegar/jscodeshift-collections

Repository files navigation

jscodeshift-collections

Build and Deploy npm version Coverage Status semantic-release Conventional Commits

Some more Collections for jscodeshift for easy and terse api for writing Codemods

Install

npm install jscodeshift-collections

List of collections

Usage

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

j.findFunctionDeclarations('foo')
 .renameTo('bar')

Before

function foo() {
}

After

function bar() {
}

List of Collections:

FunctionDeclaration

// 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

// 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

// 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

// Find all class declarations
j.findClassdeclarations();

// Find and rename
j.findClassDeclarations('foo').renameTo('bar')
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy