Giter VIP home page Giter VIP logo

jscodeshift-add-imports's Introduction

jscodeshift-add-imports

CircleCI Coverage Status semantic-release Commitizen friendly npm version

Easily add import and require statements with jscodeshift. If something is already imported, returns the locally bound identifier, and avoids name conflicts.

Usage

npm install --save jscodeshift-add-imports
const j = require('jscodeshift')
const addImports = require('jscodeshift-add-imports')

const { statement } = j.template

const code = `
// @flow
import {foo, type bar} from 'foo'
import baz from 'baz'
`

const root = j(code)
const result = addImports(root, [
  statement`import type {bar, baz} from 'foo'`,
  statement`import blah, {type qux} from 'qux'`,
])
console.log(result)
console.log(root.toSource())

Output code:

// @flow
import { foo, type bar } from 'foo'
import baz from 'baz'
import type { baz as baz1 } from 'foo'
import blah, { type qux } from 'qux'

Return value:

{
  bar: 'bar',
  baz: 'baz1',
  blah: 'blah',
  qux: 'qux',
}

Compatibility

Currently tested on [email protected] with the following parsers:

  • babylon
  • ts

There are currently issues with the babel and flow parsers.

It won't likely work with other custom parsers unless they output nodes in the same format as Babel for import declarations, variable declarations, require calls, and object patterns.

addImports(root, statments)

Arguments

root

The jscodeshift-wrapped AST of your source code

statements

The AST of an import declaration or variable declaration with requires to add, or an array of them.

Return value

An object where the key is the local identifier you requested in statements, and the value is the resulting local identifier used in the modified code (which could be the existing local identifier already imported in the code or the local identifier chosen to avoid name conflicts with an existing binding)

jscodeshift-add-imports's People

Contributors

jedwards1211 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.