Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jscodeshift

Package Overview
Dependencies
Maintainers
0
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jscodeshift

A toolkit for JavaScript codemods

  • 17.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.8M
decreased by-12.51%
Maintainers
0
Weekly downloads
 
Created

What is jscodeshift?

jscodeshift is a toolkit for running codemods over multiple JavaScript or TypeScript files. It provides a runner as well as a wrapper around the recast AST toolkit, making it easier to write codemods that can transform code and assist in large-scale codebase refactors or migrations.

What are jscodeshift's main functionalities?

Transforming Syntax

This feature allows you to transform the syntax of JavaScript code. For example, the code sample provided changes all variable declarations to use 'const' instead of 'var' or 'let'.

const jscodeshift = require('jscodeshift');
const transform = (fileInfo, api) => {
  const j = api.jscodeshift;
  return j(fileInfo.source)
    .find(j.VariableDeclaration)
    .forEach(path => {
      j(path).replaceWith(
        j.variableDeclaration('const', path.node.declarations)
      );
    })
    .toSource();
};

Code Analysis

jscodeshift can be used to analyze code and extract information. In this example, the code collects all the identifiers from a JavaScript file.

const jscodeshift = require('jscodeshift');
const analyze = (fileInfo, api) => {
  const j = api.jscodeshift;
  const root = j(fileInfo.source);
  const identifiers = [];
  root.find(j.Identifier).forEach(path => {
    identifiers.push(path.node.name);
  });
  return identifiers;
};

Refactoring

The package can be used to perform refactoring tasks, such as renaming functions across a codebase. The provided code sample renames all function declarations to 'newFunctionName'.

const jscodeshift = require('jscodeshift');
const refactor = (fileInfo, api) => {
  const j = api.jscodeshift;
  return j(fileInfo.source)
    .find(j.FunctionDeclaration)
    .renameTo('newFunctionName')
    .toSource();
};

Other packages similar to jscodeshift

Keywords

FAQs

Package last updated on 01 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc