-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
193 lines (185 loc) · 5.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const j = require('jscodeshift')
function getKey(key) {
switch (key.type) {
case 'Identifier':
return key.name
case 'Literal':
case 'StringLiteral':
return key.value
}
}
function memberExpression(object, property) {
const result = j.memberExpression(object, property)
if (property.type !== 'Identifier') result.computed = true
return result
}
/**
* Searches for imports and require statements in an AST for specifiers
* corresponding those in the requested statement.
* @param root - the jscodeshift-wrapped AST to search
* @param statement - the AST for an import or require declaration to search for
* @returns {Object} a map of all the found imports, where the key is the alias
* in statement, and the value is the alias in the searched AST
*/
module.exports = function findImports(root, statement) {
if (Array.isArray(statement)) {
const result = {}
statement.forEach((s) => Object.assign(result, findImports(root, s)))
return result
}
let source, importKind
if (statement.type === 'ImportDeclaration') {
importKind = statement.importKind
source = statement.source.value
} else if (statement.type === 'VariableDeclaration') {
const { declarations } = statement
if (declarations.length !== 1) {
const result = {}
declarations.forEach((d) =>
Object.assign(
result,
findImports(root, Object.assign({}, statement, { declarations: [d] }))
)
)
return result
}
const declarator = declarations[0]
const { init } = declarator
if (
!init ||
init.type !== 'CallExpression' ||
init.callee.name !== 'require'
) {
throw new Error('statement must be an import or require')
}
importKind = 'value'
source = init.arguments[0].value
} else {
throw new Error('invalid statement type: ' + statement.type)
}
const imports = root.find(j.ImportDeclaration, {
source: { value: source },
})
const requires = []
const defaultRequires = []
root
.find(j.Program)
.nodes()[0]
.body.forEach((node) => {
if (node.type !== 'VariableDeclaration') return
for (let declarator of node.declarations) {
const { init } = declarator
if (!init) continue
if (
init.type === 'CallExpression' &&
init.callee.name === 'require' &&
init.arguments[0].value === source
) {
requires.push(declarator)
} else if (
init.type === 'MemberExpression' &&
init.property.name === 'default' &&
init.object.type === 'CallExpression' &&
init.object.callee.name === 'require' &&
init.object.arguments[0].value === source
) {
defaultRequires.push(declarator)
}
}
})
function getImportKind(path) {
return (
path.node.importKind ||
(path.parent && path.parent.node.importKind) ||
'value'
)
}
function findImport(imported, importKind, importedNode) {
let matches
if (imported === 'default') {
matches = imports
.find(j.ImportDefaultSpecifier)
.filter((p) => getImportKind(p) === importKind)
if (matches.size()) return matches.nodes()[0].local
matches = imports
.find(j.ImportSpecifier, {
imported: { name: 'default' },
})
.filter((p) => getImportKind(p) === importKind)
if (matches.size()) return matches.nodes()[0].local
if (defaultRequires.length) {
return defaultRequires[defaultRequires.length - 1].id
}
for (let node of requires) {
if (node.id.type === 'Identifier') return node.id
}
} else {
matches = imports
.find(j.ImportSpecifier, {
imported: { name: imported },
})
.filter((p) => getImportKind(p) === importKind)
if (matches.size()) return matches.nodes()[0].local
}
for (let node of requires) {
switch (node.id.type) {
case 'ObjectPattern':
for (let prop of node.id.properties) {
if (getKey(prop.key) === imported) return prop.value
}
break
case 'Identifier':
return memberExpression(
node.id,
importedNode || j.identifier(imported)
)
}
}
const namespace = imports.find(j.ImportNamespaceSpecifier)
if (namespace.size()) {
return memberExpression(
namespace.nodes()[0].local,
importedNode || j.identifier(imported)
)
}
}
const result = {}
if (statement.type === 'ImportDeclaration') {
statement.specifiers.forEach((desiredSpecifier) => {
if (desiredSpecifier.type === 'ImportNamespaceSpecifier') {
const found = imports.find(j.ImportNamespaceSpecifier)
if (found.size())
result[desiredSpecifier.local.name] = found.nodes()[0].local
} else {
const found = findImport(
desiredSpecifier.type === 'ImportDefaultSpecifier'
? 'default'
: desiredSpecifier.imported.name,
desiredSpecifier.importKind || importKind
)
if (found) result[desiredSpecifier.local.name] = found
}
})
} else if (statement.type === 'VariableDeclaration') {
const { id } = statement.declarations[0]
if (id.type === 'ObjectPattern') {
for (let prop of id.properties) {
if (prop.type !== 'ObjectProperty' && prop.type !== 'Property') continue
const key = getKey(prop.key)
const value = getKey(prop.value)
if (typeof key !== 'string' || typeof value !== 'string') continue
const found = findImport(key, 'value', prop.key)
if (found) result[value] = found
}
}
if (id.type === 'Identifier') {
for (let node of requires) {
if (node.id.type === 'Identifier') {
result[id.name] = node.id
break
}
}
}
}
return result
}