-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
140 lines (117 loc) · 3.22 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
'use strict';
const parser = require('@babel/parser');
module.exports = class NodeSourceWalk {
// We use global state to stop the recursive traversal of the AST
#shouldStop = false;
/**
* @param {Object} options - Options to configure parser
* @param {Object} options.parser - An object with a parse method that returns an AST
*/
constructor(options = {}) {
this.parser = options.parser || parser;
if (options.parser) {
// We don't want to send that down to the actual parser
delete options.parser;
}
this.options = {
plugins: [
'jsx',
'flow',
'asyncGenerators',
'classProperties',
'doExpressions',
'dynamicImport',
'exportDefaultFrom',
'exportNamespaceFrom',
'functionBind',
'functionSent',
'nullishCoalescingOperator',
'objectRestSpread',
[
'decorators', {
decoratorsBeforeExport: true
}
],
'optionalChaining'
],
allowHashBang: true,
sourceType: 'module',
...options
};
}
/**
* @param {String} src
* @param {Object} [options] - Parser options
* @return {Object} The AST of the given src
*/
parse(src, options = this.options) {
// Keep around for consumers of parse that supply their own options
if (options.allowHashBang === undefined) {
options.allowHashBang = true;
}
return this.parser.parse(src, options);
}
/**
* Adapted from substack/node-detective
* Executes callback on a non-array AST node
*/
traverse(node, callback) {
if (this.#shouldStop) return;
if (Array.isArray(node)) {
for (const key of node) {
if (isObject(key)) {
// Mark that the node has been visited
key.parent = node;
this.traverse(key, callback);
}
}
} else if (isObject(node)) {
callback(node);
for (const [key, value] of Object.entries(node)) {
// Avoid visited nodes
if (key === 'parent' || !value) continue;
if (isObject(value)) {
value.parent = node;
}
this.traverse(value, callback);
}
}
}
/**
* Executes the passed callback for every traversed node of
* the passed in src's ast
*
* @param {String|Object} src - The source code or AST to traverse
* @param {Function} callback - Called for every node
*/
walk(src, callback) {
this.#shouldStop = false;
const ast = isObject(src) ? src : this.parse(src);
this.traverse(ast, callback);
}
moonwalk(node, callback) {
this.#shouldStop = false;
if (!isObject(node)) throw new Error('node must be an object');
this.#reverseTraverse(node, callback);
}
/**
* Halts further traversal of the AST
*/
stopWalking() {
this.#shouldStop = true;
}
#reverseTraverse(node, callback) {
if (this.#shouldStop || !node.parent) return;
if (Array.isArray(node.parent)) {
for (const parent of node.parent) {
callback(parent);
}
} else {
callback(node.parent);
}
this.#reverseTraverse(node.parent, callback);
}
};
function isObject(value) {
return typeof value === 'object' && !Array.isArray(value) && value !== null;
}