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

jscodeshift-imports

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jscodeshift-imports

A JSCodeshift extension with helpers for modifying `import` and `require` statements.

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2K
decreased by-15.44%
Maintainers
3
Weekly downloads
 
Created
Source

jscodeshift-imports extension

A JSCodeshift extension which contains helpers for modifying import and require statements.

Setup

Install extension: npm install jscodeshift-imports.

Register the extension with jscodeshift:

const imports = require('jscodeshift-imports');

module.exports = function(fileInfo, api) {
  const {jscodeshift} = api;

  imports.register(jscodeshift, imports.config.CJSBasicRequire);

  // Your transform here.
}

Different configs will be needed based on your code style, this extension comes with two default configs:

You can also provide your own custom config.

API

addImport

This helper allows you to insert require statements into the most appropriate place within a file, it does this in a non destructive way so your codemod will change as little in the file as it can.

Usage:

const imports = require('jscodeshift-imports');

module.exports = function(fileInfo, api) {
  const {jscodeshift} = api;
  const {statement} = jscodeshift.template;

  imports.register(jscodeshift, imports.config.CJSBasicRequire);

  return jscodeshift(file.source)
    .addImport(statement`
      const MyRequireItem = require('MyRequireItem');
    `)
    .toSource();
}

addImport accepts any require or import statement as long as it matches one of the config types.

Note: This transform cannot be trusted. Whilst this implementation works in most cases it can give incorrect whitespace due to limitations in the AST. You will need to manually verify the output after running.

Keywords

FAQs

Package last updated on 17 Oct 2016

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