-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
163 lines (140 loc) · 4.22 KB
/
index.js
File metadata and controls
163 lines (140 loc) · 4.22 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
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
'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 = {}) {
const { parser: customParser, ...restOptions } = options;
this.parser = customParser || parser;
this.options = {
plugins: [
'jsx',
'flow',
'asyncGenerators',
'classProperties',
'doExpressions',
'dynamicImport',
'exportDefaultFrom',
'exportNamespaceFrom',
'functionBind',
'functionSent',
'nullishCoalescingOperator',
'objectRestSpread',
[
'decorators', {
decoratorsBeforeExport: true
}
],
'optionalChaining'
],
allowHashBang: true,
sourceType: 'module',
...restOptions
};
}
/**
* @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) {
return this.parser.parse(src, { ...options, allowHashBang: true });
}
return this.parser.parse(src, options);
}
/**
* Adapted from substack/node-detective
* Executes callback on a non-array AST node
*
* @param {object|object[]} node - AST node or array of nodes
* @param {function(object): void} callback - Function executed for each visited node
*/
traverse(node, callback) {
if (this.#shouldStop) return;
if (Array.isArray(node)) {
for (const key of node) {
if (this.#isObject(key)) {
// Mark that the node has been visited
key.parent = node;
this.traverse(key, callback);
if (this.#shouldStop) return;
}
}
} else if (this.#isObject(node)) {
callback(node);
for (const key of Object.keys(node)) {
// Avoid visited nodes
if (key === 'parent') continue;
const value = node[key];
// Only recurse into objects and arrays; skip primitives and null
if (value === null || typeof value !== 'object') continue;
if (!Array.isArray(value)) {
value.parent = node;
}
this.traverse(value, callback);
if (this.#shouldStop) return;
}
}
}
/**
* 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(object): void} callback - Called for every node
*/
walk(src, callback) {
this.#shouldStop = false;
const ast = this.#isObject(src) ? src : this.parse(src);
this.traverse(ast, callback);
}
/**
* Walks upward through parent nodes, executing the callback for each ancestor
*
* @param {object} node - The starting AST node
* @param {function(object): void} callback - Called for each parent node
*/
moonwalk(node, callback) {
this.#shouldStop = false;
if (!this.#isObject(node)) throw new Error('node must be an object');
this.#reverseTraverse(node, callback);
}
/**
* Halts further traversal of the AST
*/
stopWalking() {
this.#shouldStop = true;
}
/**
* Traverses upward through parent nodes
*
* @param {object} node - Current AST node
* @param {function(object): void} callback - Called for each parent node
*/
#reverseTraverse(node, callback) {
if (this.#shouldStop || !node.parent) return;
if (Array.isArray(node.parent)) {
for (const parent of node.parent) {
callback(parent);
if (this.#shouldStop) return;
}
} else {
callback(node.parent);
}
this.#reverseTraverse(node.parent, callback);
}
/**
* Determines whether a value is a non-null object
*
* @param {*} value - Value to test
* @return {boolean} True if value is a non-array object
*/
#isObject(value) {
return typeof value === 'object' && !Array.isArray(value) && value !== null;
}
};