1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * @typedef {{ readonly [type: string]: ReadonlyArray<string> }} VisitorKeys
|
---|
7 | */
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * @type {VisitorKeys}
|
---|
11 | */
|
---|
12 | const KEYS = {
|
---|
13 | ArrayExpression: [
|
---|
14 | "elements"
|
---|
15 | ],
|
---|
16 | ArrayPattern: [
|
---|
17 | "elements"
|
---|
18 | ],
|
---|
19 | ArrowFunctionExpression: [
|
---|
20 | "params",
|
---|
21 | "body"
|
---|
22 | ],
|
---|
23 | AssignmentExpression: [
|
---|
24 | "left",
|
---|
25 | "right"
|
---|
26 | ],
|
---|
27 | AssignmentPattern: [
|
---|
28 | "left",
|
---|
29 | "right"
|
---|
30 | ],
|
---|
31 | AwaitExpression: [
|
---|
32 | "argument"
|
---|
33 | ],
|
---|
34 | BinaryExpression: [
|
---|
35 | "left",
|
---|
36 | "right"
|
---|
37 | ],
|
---|
38 | BlockStatement: [
|
---|
39 | "body"
|
---|
40 | ],
|
---|
41 | BreakStatement: [
|
---|
42 | "label"
|
---|
43 | ],
|
---|
44 | CallExpression: [
|
---|
45 | "callee",
|
---|
46 | "arguments"
|
---|
47 | ],
|
---|
48 | CatchClause: [
|
---|
49 | "param",
|
---|
50 | "body"
|
---|
51 | ],
|
---|
52 | ChainExpression: [
|
---|
53 | "expression"
|
---|
54 | ],
|
---|
55 | ClassBody: [
|
---|
56 | "body"
|
---|
57 | ],
|
---|
58 | ClassDeclaration: [
|
---|
59 | "id",
|
---|
60 | "superClass",
|
---|
61 | "body"
|
---|
62 | ],
|
---|
63 | ClassExpression: [
|
---|
64 | "id",
|
---|
65 | "superClass",
|
---|
66 | "body"
|
---|
67 | ],
|
---|
68 | ConditionalExpression: [
|
---|
69 | "test",
|
---|
70 | "consequent",
|
---|
71 | "alternate"
|
---|
72 | ],
|
---|
73 | ContinueStatement: [
|
---|
74 | "label"
|
---|
75 | ],
|
---|
76 | DebuggerStatement: [],
|
---|
77 | DoWhileStatement: [
|
---|
78 | "body",
|
---|
79 | "test"
|
---|
80 | ],
|
---|
81 | EmptyStatement: [],
|
---|
82 | ExperimentalRestProperty: [
|
---|
83 | "argument"
|
---|
84 | ],
|
---|
85 | ExperimentalSpreadProperty: [
|
---|
86 | "argument"
|
---|
87 | ],
|
---|
88 | ExportAllDeclaration: [
|
---|
89 | "exported",
|
---|
90 | "source"
|
---|
91 | ],
|
---|
92 | ExportDefaultDeclaration: [
|
---|
93 | "declaration"
|
---|
94 | ],
|
---|
95 | ExportNamedDeclaration: [
|
---|
96 | "declaration",
|
---|
97 | "specifiers",
|
---|
98 | "source"
|
---|
99 | ],
|
---|
100 | ExportSpecifier: [
|
---|
101 | "exported",
|
---|
102 | "local"
|
---|
103 | ],
|
---|
104 | ExpressionStatement: [
|
---|
105 | "expression"
|
---|
106 | ],
|
---|
107 | ForInStatement: [
|
---|
108 | "left",
|
---|
109 | "right",
|
---|
110 | "body"
|
---|
111 | ],
|
---|
112 | ForOfStatement: [
|
---|
113 | "left",
|
---|
114 | "right",
|
---|
115 | "body"
|
---|
116 | ],
|
---|
117 | ForStatement: [
|
---|
118 | "init",
|
---|
119 | "test",
|
---|
120 | "update",
|
---|
121 | "body"
|
---|
122 | ],
|
---|
123 | FunctionDeclaration: [
|
---|
124 | "id",
|
---|
125 | "params",
|
---|
126 | "body"
|
---|
127 | ],
|
---|
128 | FunctionExpression: [
|
---|
129 | "id",
|
---|
130 | "params",
|
---|
131 | "body"
|
---|
132 | ],
|
---|
133 | Identifier: [],
|
---|
134 | IfStatement: [
|
---|
135 | "test",
|
---|
136 | "consequent",
|
---|
137 | "alternate"
|
---|
138 | ],
|
---|
139 | ImportDeclaration: [
|
---|
140 | "specifiers",
|
---|
141 | "source"
|
---|
142 | ],
|
---|
143 | ImportDefaultSpecifier: [
|
---|
144 | "local"
|
---|
145 | ],
|
---|
146 | ImportExpression: [
|
---|
147 | "source"
|
---|
148 | ],
|
---|
149 | ImportNamespaceSpecifier: [
|
---|
150 | "local"
|
---|
151 | ],
|
---|
152 | ImportSpecifier: [
|
---|
153 | "imported",
|
---|
154 | "local"
|
---|
155 | ],
|
---|
156 | JSXAttribute: [
|
---|
157 | "name",
|
---|
158 | "value"
|
---|
159 | ],
|
---|
160 | JSXClosingElement: [
|
---|
161 | "name"
|
---|
162 | ],
|
---|
163 | JSXClosingFragment: [],
|
---|
164 | JSXElement: [
|
---|
165 | "openingElement",
|
---|
166 | "children",
|
---|
167 | "closingElement"
|
---|
168 | ],
|
---|
169 | JSXEmptyExpression: [],
|
---|
170 | JSXExpressionContainer: [
|
---|
171 | "expression"
|
---|
172 | ],
|
---|
173 | JSXFragment: [
|
---|
174 | "openingFragment",
|
---|
175 | "children",
|
---|
176 | "closingFragment"
|
---|
177 | ],
|
---|
178 | JSXIdentifier: [],
|
---|
179 | JSXMemberExpression: [
|
---|
180 | "object",
|
---|
181 | "property"
|
---|
182 | ],
|
---|
183 | JSXNamespacedName: [
|
---|
184 | "namespace",
|
---|
185 | "name"
|
---|
186 | ],
|
---|
187 | JSXOpeningElement: [
|
---|
188 | "name",
|
---|
189 | "attributes"
|
---|
190 | ],
|
---|
191 | JSXOpeningFragment: [],
|
---|
192 | JSXSpreadAttribute: [
|
---|
193 | "argument"
|
---|
194 | ],
|
---|
195 | JSXSpreadChild: [
|
---|
196 | "expression"
|
---|
197 | ],
|
---|
198 | JSXText: [],
|
---|
199 | LabeledStatement: [
|
---|
200 | "label",
|
---|
201 | "body"
|
---|
202 | ],
|
---|
203 | Literal: [],
|
---|
204 | LogicalExpression: [
|
---|
205 | "left",
|
---|
206 | "right"
|
---|
207 | ],
|
---|
208 | MemberExpression: [
|
---|
209 | "object",
|
---|
210 | "property"
|
---|
211 | ],
|
---|
212 | MetaProperty: [
|
---|
213 | "meta",
|
---|
214 | "property"
|
---|
215 | ],
|
---|
216 | MethodDefinition: [
|
---|
217 | "key",
|
---|
218 | "value"
|
---|
219 | ],
|
---|
220 | NewExpression: [
|
---|
221 | "callee",
|
---|
222 | "arguments"
|
---|
223 | ],
|
---|
224 | ObjectExpression: [
|
---|
225 | "properties"
|
---|
226 | ],
|
---|
227 | ObjectPattern: [
|
---|
228 | "properties"
|
---|
229 | ],
|
---|
230 | PrivateIdentifier: [],
|
---|
231 | Program: [
|
---|
232 | "body"
|
---|
233 | ],
|
---|
234 | Property: [
|
---|
235 | "key",
|
---|
236 | "value"
|
---|
237 | ],
|
---|
238 | PropertyDefinition: [
|
---|
239 | "key",
|
---|
240 | "value"
|
---|
241 | ],
|
---|
242 | RestElement: [
|
---|
243 | "argument"
|
---|
244 | ],
|
---|
245 | ReturnStatement: [
|
---|
246 | "argument"
|
---|
247 | ],
|
---|
248 | SequenceExpression: [
|
---|
249 | "expressions"
|
---|
250 | ],
|
---|
251 | SpreadElement: [
|
---|
252 | "argument"
|
---|
253 | ],
|
---|
254 | StaticBlock: [
|
---|
255 | "body"
|
---|
256 | ],
|
---|
257 | Super: [],
|
---|
258 | SwitchCase: [
|
---|
259 | "test",
|
---|
260 | "consequent"
|
---|
261 | ],
|
---|
262 | SwitchStatement: [
|
---|
263 | "discriminant",
|
---|
264 | "cases"
|
---|
265 | ],
|
---|
266 | TaggedTemplateExpression: [
|
---|
267 | "tag",
|
---|
268 | "quasi"
|
---|
269 | ],
|
---|
270 | TemplateElement: [],
|
---|
271 | TemplateLiteral: [
|
---|
272 | "quasis",
|
---|
273 | "expressions"
|
---|
274 | ],
|
---|
275 | ThisExpression: [],
|
---|
276 | ThrowStatement: [
|
---|
277 | "argument"
|
---|
278 | ],
|
---|
279 | TryStatement: [
|
---|
280 | "block",
|
---|
281 | "handler",
|
---|
282 | "finalizer"
|
---|
283 | ],
|
---|
284 | UnaryExpression: [
|
---|
285 | "argument"
|
---|
286 | ],
|
---|
287 | UpdateExpression: [
|
---|
288 | "argument"
|
---|
289 | ],
|
---|
290 | VariableDeclaration: [
|
---|
291 | "declarations"
|
---|
292 | ],
|
---|
293 | VariableDeclarator: [
|
---|
294 | "id",
|
---|
295 | "init"
|
---|
296 | ],
|
---|
297 | WhileStatement: [
|
---|
298 | "test",
|
---|
299 | "body"
|
---|
300 | ],
|
---|
301 | WithStatement: [
|
---|
302 | "object",
|
---|
303 | "body"
|
---|
304 | ],
|
---|
305 | YieldExpression: [
|
---|
306 | "argument"
|
---|
307 | ]
|
---|
308 | };
|
---|
309 |
|
---|
310 | // Types.
|
---|
311 | const NODE_TYPES = Object.keys(KEYS);
|
---|
312 |
|
---|
313 | // Freeze the keys.
|
---|
314 | for (const type of NODE_TYPES) {
|
---|
315 | Object.freeze(KEYS[type]);
|
---|
316 | }
|
---|
317 | Object.freeze(KEYS);
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * @author Toru Nagashima <https://github.com/mysticatea>
|
---|
321 | * See LICENSE file in root directory for full license.
|
---|
322 | */
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * @typedef {import('./visitor-keys.js').VisitorKeys} VisitorKeys
|
---|
326 | */
|
---|
327 |
|
---|
328 | // List to ignore keys.
|
---|
329 | const KEY_BLACKLIST = new Set([
|
---|
330 | "parent",
|
---|
331 | "leadingComments",
|
---|
332 | "trailingComments"
|
---|
333 | ]);
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Check whether a given key should be used or not.
|
---|
337 | * @param {string} key The key to check.
|
---|
338 | * @returns {boolean} `true` if the key should be used.
|
---|
339 | */
|
---|
340 | function filterKey(key) {
|
---|
341 | return !KEY_BLACKLIST.has(key) && key[0] !== "_";
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Get visitor keys of a given node.
|
---|
346 | * @param {object} node The AST node to get keys.
|
---|
347 | * @returns {readonly string[]} Visitor keys of the node.
|
---|
348 | */
|
---|
349 | function getKeys(node) {
|
---|
350 | return Object.keys(node).filter(filterKey);
|
---|
351 | }
|
---|
352 |
|
---|
353 | // Disable valid-jsdoc rule because it reports syntax error on the type of @returns.
|
---|
354 | // eslint-disable-next-line valid-jsdoc
|
---|
355 | /**
|
---|
356 | * Make the union set with `KEYS` and given keys.
|
---|
357 | * @param {VisitorKeys} additionalKeys The additional keys.
|
---|
358 | * @returns {VisitorKeys} The union set.
|
---|
359 | */
|
---|
360 | function unionWith(additionalKeys) {
|
---|
361 | const retv = /** @type {{
|
---|
362 | [type: string]: ReadonlyArray<string>
|
---|
363 | }} */ (Object.assign({}, KEYS));
|
---|
364 |
|
---|
365 | for (const type of Object.keys(additionalKeys)) {
|
---|
366 | if (Object.prototype.hasOwnProperty.call(retv, type)) {
|
---|
367 | const keys = new Set(additionalKeys[type]);
|
---|
368 |
|
---|
369 | for (const key of retv[type]) {
|
---|
370 | keys.add(key);
|
---|
371 | }
|
---|
372 |
|
---|
373 | retv[type] = Object.freeze(Array.from(keys));
|
---|
374 | } else {
|
---|
375 | retv[type] = Object.freeze(Array.from(additionalKeys[type]));
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | return Object.freeze(retv);
|
---|
380 | }
|
---|
381 |
|
---|
382 | exports.KEYS = KEYS;
|
---|
383 | exports.getKeys = getKeys;
|
---|
384 | exports.unionWith = unionWith;
|
---|