1 | /**
|
2 | * @fileoverview Wrapper around estraverse
|
3 | * @author Nicholas C. Zakas
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | const estraverse = require("estraverse");
|
12 |
|
13 | //------------------------------------------------------------------------------
|
14 | // Helpers
|
15 | //------------------------------------------------------------------------------
|
16 |
|
17 | const KEY_BLACKLIST = [
|
18 | "parent",
|
19 | "leadingComments",
|
20 | "trailingComments"
|
21 | ];
|
22 |
|
23 | /**
|
24 | * Wrapper around an estraverse controller that ensures the correct keys
|
25 | * are visited.
|
26 | * @constructor
|
27 | */
|
28 | function Traverser() {
|
29 |
|
30 | const controller = Object.create(new estraverse.Controller()),
|
31 | originalTraverse = controller.traverse;
|
32 |
|
33 | // intercept call to traverse() and add the fallback key to the visitor
|
34 | controller.traverse = function(node, visitor) {
|
35 | visitor.fallback = Traverser.getKeys;
|
36 | return originalTraverse.call(this, node, visitor);
|
37 | };
|
38 |
|
39 | return controller;
|
40 | }
|
41 |
|
42 | /**
|
43 | * Calculates the keys to use for traversal.
|
44 | * @param {ASTNode} node The node to read keys from.
|
45 | * @returns {string[]} An array of keys to visit on the node.
|
46 | * @private
|
47 | */
|
48 | Traverser.getKeys = function(node) {
|
49 | return Object.keys(node).filter(function(key) {
|
50 | return KEY_BLACKLIST.indexOf(key) === -1;
|
51 | });
|
52 | };
|
53 |
|
54 | module.exports = Traverser;
|
55 |
|
56 |
|