node/tools/eslint/lib/util/traverser.js
silverwind d9b8758f47 tools: update ESLint to 2.7.0
PR-URL: https://github.com/nodejs/node/pull/6132
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: thefourtheye <thechargingvolcano@gmail.com>
2016-04-20 16:24:21 -07:00

56 lines
1.5 KiB
JavaScript

/**
* @fileoverview Wrapper around estraverse
* @author Nicholas C. Zakas
* @copyright 2016 Nicholas C. Zakas. All rights reserved.
* See LICENSE in root directory for full license.
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var estraverse = require("estraverse");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
var KEY_BLACKLIST = [
"parent",
"leadingComments",
"trailingComments"
];
/**
* Wrapper around an estraverse controller that ensures the correct keys
* are visited.
* @constructor
*/
function Traverser() {
var controller = Object.create(new estraverse.Controller()),
originalTraverse = controller.traverse;
// intercept call to traverse() and add the fallback key to the visitor
controller.traverse = function(node, visitor) {
visitor.fallback = Traverser.getKeys;
return originalTraverse.call(this, node, visitor);
};
return controller;
}
/**
* Calculates the keys to use for traversal.
* @param {ASTNode} node The node to read keys from.
* @returns {string[]} An array of keys to visit on the node.
* @private
*/
Traverser.getKeys = function(node) {
return Object.keys(node).filter(function(key) {
return KEY_BLACKLIST.indexOf(key) === -1;
});
};
module.exports = Traverser;