Socket
Book a DemoInstallSign in
Socket

jscodeshift-find-imports

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jscodeshift-find-imports

find imported/required identifiers with jscodeshift

2.0.5
latest
Source
npmnpm
Version published
Weekly downloads
25K
-6.78%
Maintainers
1
Weekly downloads
 
Created
Source

jscodeshift-find-imports

CircleCI Coverage Status semantic-release Commitizen friendly npm version

find imported/required identifiers with jscodeshift

Usage

npm i jscodeshift-find-imports
const findImports = require('jscodeshift-find-imports')
const j = require('jscodeshift')
const { statement } = j.template

const code = `
import bar from 'foo'
`

const imports = findImports(j(code), statement`import foo from 'foo'`)

console.log(imports) // {foo: {type: 'Identifier', name: 'bar'}}

Compatibility

Currently tested and working with jscodeshift@0.11.0 and the following parsers:

  • 'babel'
  • 'babylon'
  • 'ts'
  • 'flow'

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.

findImports(root, statements)

Arguments

root

The jscodeshift-wrapped AST of source code

statements

The AST of an ImportDeclaration or VariableDeclaration containing require calls to search for (e.g. const foo = require('foo')), or an array of them.

Returns

An object where each key is an identifier from your search statement(s) that was found, and the corresponding value is the AST for the local binding for that import (either an Identifier or MemberExpression node). For example, if you search for const {bar} = require('bar') but the source code has const foo = require('bar').bar, the result will have bar: { type: 'Identifier', name: 'bar' }.

The local binding will be a MemberExpression in cases like this:

const code = `
import React from 'react'
`
const imports = findImports(
  j(code),
  statement`import { Component } from 'react'`
)

In this case imports would be

{
  Component: {
    type: 'MemberExpression',
    object: {
      type: 'Identifier',
      name: 'React',
    },
    property: {
      type: 'Identifier',
      name: 'Component',
    },
  },
}

Keywords

jscodeshift

FAQs

Package last updated on 19 Jul 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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.