Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: estraverse/escope to work with unknowns (fixes #5476)
- Loading branch information
Showing
5 changed files
with
81 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters