index.js
1.48 KB
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
var assert = require('assert')
/**
* Get a list of all identifiers that are initialised by this (possibly destructuring)
* node.
*
* eg with input:
*
* var { a: [b, ...c], d } = xyz
*
* this returns the nodes for 'b', 'c', and 'd'
*/
module.exports = function getAssignedIdentifiers (node, identifiers) {
assert.equal(typeof node, 'object', 'get-assigned-identifiers: node must be object')
assert.equal(typeof node.type, 'string', 'get-assigned-identifiers: node must have a type')
identifiers = identifiers || []
if (node.type === 'ImportDeclaration') {
node.specifiers.forEach(function (el) {
getAssignedIdentifiers(el, identifiers)
})
}
if (node.type === 'ImportDefaultSpecifier' || node.type === 'ImportNamespaceSpecifier' || node.type === 'ImportSpecifier') {
node = node.local
}
if (node.type === 'RestElement') {
node = node.argument
}
if (node.type === 'ArrayPattern') {
node.elements.forEach(function (el) {
// `el` might be `null` in case of `[x,,y] = whatever`
if (el) {
getAssignedIdentifiers(el, identifiers)
}
})
}
if (node.type === 'ObjectPattern') {
node.properties.forEach(function (prop) {
if (prop.type === 'Property') {
getAssignedIdentifiers(prop.value, identifiers)
} else if (prop.type === 'RestElement') {
getAssignedIdentifiers(prop, identifiers)
}
})
}
if (node.type === 'Identifier') {
identifiers.push(node)
}
return identifiers
}