Permalink
Browse files

adding option to include node for each require in a nodes array

  • Loading branch information...
1 parent d3b2da5 commit 47bc4de751520dc48a2e09ffcdb3bcb0166d5727 @thlorenz thlorenz committed Mar 31, 2013
Showing with 20 additions and 0 deletions.
  1. +2 −0 index.js
  2. +3 −0 readme.markdown
  3. +15 −0 test/both.js
View
@@ -44,6 +44,7 @@ exports.find = function (src, opts) {
}
var modules = { strings : [], expressions : [] };
+ if (opts.nodes) modules.nodes = [];
if (src.indexOf(word) == -1) return modules;
@@ -56,6 +57,7 @@ exports.find = function (src, opts) {
else {
modules.expressions.push(escodegen.generate(node.arguments[0]));
}
+ if (opts.nodes) modules.nodes.push(node);
});
return modules;
View
@@ -57,6 +57,9 @@ The "expressions" array will contain the stringified expressions.
Optionally you can specify a different function besides `"require"` to analyze
with `opts.word`.
+You can also specify `opts.nodes = true` in order to include a "nodes" array
+which contains an AST node for each of the require() calls.
+
# install
With [npm](https://npmjs.org) do:
View
@@ -7,5 +7,20 @@ test('both', function (t) {
var modules = detective.find(src);
t.deepEqual(modules.strings, [ 'a', 'b' ]);
t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]);
+ t.notOk(modules.nodes, 'has no nodes');
+ t.end();
+});
+
+test('both with nodes specified in opts', function (t) {
+ var modules = detective.find(src, { nodes: true });
+ t.deepEqual(modules.strings, [ 'a', 'b' ]);
+ t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]);
+ t.deepEqual(
+ modules.nodes.map(function (n) {
+ var arg = n.arguments[0];
+ return arg.value || arg.left.value;
+ }),
+ [ 'a', 'b', 'c', 'd' ],
+ 'has a node for each require');
t.end();
});

0 comments on commit 47bc4de

Please sign in to comment.