node-source-walk
Advanced tools
Comparing version 1.3.0 to 1.4.0
32
index.js
@@ -16,4 +16,15 @@ var acorn = require('acorn'); | ||
// Adapted from substack/node-detective | ||
// Executes cb on a non-array AST node | ||
/** | ||
* @param {String} src | ||
* @param {Object} [options] - Parser options | ||
* @return {Object} The AST of the given src | ||
*/ | ||
module.exports.prototype.parse = function(src, options) { | ||
return acorn.parse(src, options); | ||
}; | ||
/** | ||
* Adapted from substack/node-detective | ||
* Executes cb on a non-array AST node | ||
*/ | ||
module.exports.prototype.traverse = function (node, cb) { | ||
@@ -46,8 +57,15 @@ var that = this; | ||
// Executes the passed callback for every traversed node of | ||
// the passed in src's ast | ||
/** | ||
* Executes the passed callback for every traversed node of | ||
* the passed in src's ast | ||
* | ||
* @param {String|Object} src - The source code or AST to traverse | ||
* @param {Function} cb - Called for every node | ||
*/ | ||
module.exports.prototype.walk = function (src, cb) { | ||
this.shouldStop = false; | ||
var ast = acorn.parse(src, this.options); | ||
var ast = typeof src === 'object' ? | ||
src : | ||
this.parse(src, this.options); | ||
@@ -57,5 +75,7 @@ this.traverse(ast, cb); | ||
// Halts further traversal of the AST | ||
/** | ||
* Halts further traversal of the AST | ||
*/ | ||
module.exports.prototype.stopWalking = function () { | ||
this.shouldStop = true; | ||
}; |
{ | ||
"name": "node-source-walk", | ||
"version": "1.3.0", | ||
"version": "1.4.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": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha test/test.js" | ||
}, | ||
@@ -27,3 +27,7 @@ "repository": { | ||
"acorn": "~0.10.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~2.0.1", | ||
"sinon": "~1.12.2" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
Execute a callback on every node of a source code's AST and stop walking whenever you see fit. | ||
Execute a callback on every node of a file's AST and stop walking whenever you see fit. | ||
@@ -16,17 +16,9 @@ *A variation of [substack/node-detective](https://github.com/substack/node-detective) | ||
// Assume src is the string contents of myfile.js | ||
// or the AST of an outside parse of myfile.js | ||
walker.walk(src, function (node) { | ||
// Example: looking for the use of define() | ||
var callee = node.callee; | ||
if (callee && | ||
node.type === 'CallExpression' && | ||
callee.type === 'Identifier' && | ||
callee.name === 'define') { | ||
console.log('AMD syntax'); | ||
// No need to keep traversing since we found | ||
// what we wanted | ||
walker.stopWalking(); | ||
} | ||
if (/* some condition */) { | ||
// No need to keep traversing since we found what we wanted | ||
walker.stopWalking(); | ||
} | ||
}); | ||
@@ -51,4 +43,4 @@ | ||
* Generates and recursively walks through the AST for `src` and executes `cb` | ||
on every node. | ||
* src: the contents of a file OR its AST (via Esprima or Acorn) | ||
* cb: a function that is called for every visited node | ||
@@ -55,0 +47,0 @@ `stopWalking()` |
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
5911
6
124
0
2
54