node-source-walk
Advanced tools
Comparing version 3.1.0 to 3.2.0
24
index.js
@@ -102,2 +102,26 @@ var babylon = require('babylon'); | ||
module.exports.prototype.moonwalk = function(node, cb) { | ||
this.shouldStop = false; | ||
if (typeof node !== 'object') { | ||
throw new Error('node must be an object'); | ||
} | ||
reverseTraverse.call(this, node, cb); | ||
}; | ||
function reverseTraverse(node, cb) { | ||
if (this.shouldStop || !node.parent) { return; } | ||
if (node.parent instanceof Array) { | ||
for (var i = 0, l = node.parent.length; i < l; i++) { | ||
cb(node.parent[i]); | ||
} | ||
} else { | ||
cb(node.parent); | ||
} | ||
reverseTraverse.call(this, node.parent, cb); | ||
} | ||
/** | ||
@@ -104,0 +128,0 @@ * Halts further traversal of the AST |
{ | ||
"name": "node-source-walk", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Execute a callback on every node of a source code's AST and stop walking when you see fit", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jscs -p google index.js test && mocha test/test.js" | ||
"test": "jscs index.js test && mocha test/test.js" | ||
}, | ||
@@ -33,5 +33,6 @@ "repository": { | ||
"jscs": "~2.4.0", | ||
"mocha": "~2.0.1", | ||
"sinon": "~1.12.2" | ||
"jscs-preset-mrjoelkemp": "~1.0.0", | ||
"mocha": "~2.2.5", | ||
"sinon": "~1.17.4" | ||
} | ||
} |
### node-source-walk [![npm](http://img.shields.io/npm/v/node-source-walk.svg)](https://npmjs.org/package/node-source-walk) [![npm](http://img.shields.io/npm/dm/node-source-walk.svg)](https://npmjs.org/package/node-source-walk) | ||
> Execute a callback on every node of a file's AST and stop walking whenever you see fit. | ||
> Synchronously execute a callback on every node of a file's AST and stop walking whenever you see fit. | ||
*A variation of [substack/node-detective](https://github.com/substack/node-detective) | ||
and simplification of [substack/node-falafel](https://github.com/substack/node-falafel).* | ||
`npm install --save node-source-walk` | ||
`npm install node-source-walk` | ||
### Usage | ||
@@ -20,4 +17,4 @@ | ||
walker.walk(src, function (node) { | ||
if (/* some condition */) { | ||
walker.walk(src, function(node) { | ||
if (node.type === whateverImLookingFor) { | ||
// No need to keep traversing since we found what we wanted | ||
@@ -62,9 +59,18 @@ walker.stopWalking(); | ||
* src: the contents of a file **OR** its (already parsed) AST | ||
* cb: a function that is called for every visited node | ||
* Recursively walks the given `src` from top to bottom | ||
* `src`: the contents of a file **OR** its (already parsed) AST | ||
* `cb`: a function that is called for every visited node. | ||
* The argument passed to `cb` will be the currently visited node. | ||
`moonwalk(node, cb)` | ||
* Recursively walks up an AST starting from the given node. This is a traversal that's in the opposite direction of `walk` and `traverse`. | ||
* `node`: a valid AST node | ||
* `cb`: a function that is called for every node (specifically via visiting the parent(s) of every node recursively). | ||
* The argument passed to `cb` will be the currently visited node. | ||
`stopWalking()` | ||
* Halts further walking of the AST until another manual call of `walk`. | ||
* This is super-beneficial when dealing with large source files | ||
* Halts further walking of the AST until another manual call of `walk` or `moonwalk`. | ||
* This is super-beneficial when dealing with large source files (or ASTs) | ||
@@ -80,1 +86,5 @@ `traverse(node, cb)` | ||
using the configured parser (or babylon by default). | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6946
108
88
4