-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
55 lines (46 loc) · 1.17 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
"use strict"
const SKIP = Symbol("skip")
const STOP = Symbol("stop")
function traverse(context, node, visitor) {
let allVisitorKeys = context.getSourceCode().visitorKeys
let queue = []
queue.push({
node,
parent: null,
parentKey: null,
parentPath: null
})
while (queue.length) {
let currentPath = queue.shift()
let result = visitor(currentPath)
if (result === STOP) break
if (result === SKIP) continue
let visitorKeys = allVisitorKeys[currentPath.node.type]
if (!visitorKeys) continue
for (let visitorKey of visitorKeys) {
let child = currentPath.node[visitorKey]
if (!child) {
continue
} else if (Array.isArray(child)) {
for (let item of child) {
queue.push({
node: item,
parent: currentPath.node,
parentKey: visitorKey,
parentPath: currentPath,
})
}
} else {
queue.push({
node: child,
parent: currentPath.node,
parentKey: visitorKey,
parentPath: currentPath,
})
}
}
}
}
module.exports = traverse
module.exports.SKIP = SKIP
module.exports.STOP = STOP