find imported/required identifiers with jscodeshift
npm install jscodeshift-find-imports




find imported/required identifiers with jscodeshift
```
npm i jscodeshift-find-imports
`js
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), statementimport foo from 'foo')
console.log(imports) // {foo: {type: 'Identifier', name: 'bar'}}
`
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.
The jscodeshift-wrapped AST of source code
The AST of an ImportDeclaration or VariableDeclaration containing requireconst foo = require('foo')
calls to search for (e.g. ), or an array of them.
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 orMemberExpression node). For example, if you search for const {bar} = require('bar') but theconst foo = require('bar').bar
source code has , the result will have bar: { type: 'Identifier', name: 'bar' }.
The local binding will be a MemberExpression in cases like this:
`js
const code =
import React from 'react'import { Component } from 'react'
const imports = findImports(
j(code),
statement`
)
In this case imports would be
`js``
{
Component: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'React',
},
property: {
type: 'Identifier',
name: 'Component',
},
},
}