Skip to content

Commit

Permalink
fix (require)("skipme")
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Aug 21, 2012
1 parent 71d0400 commit a15e9b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
32 changes: 9 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ var traverse = function (node, cb) {
};

var walk = function (src, cb) {
var ast = esprima.parse(src);
traverse(ast, cb);
};

var walkSlow = function (src, cb) {
var ast = esprima.parse(src, { range : true });
traverse(ast, cb);
};
Expand All @@ -35,40 +30,31 @@ exports.find = function (src, opts) {
if (typeof src !== 'string') src = String(src);

function isRequire (node) {
return node.type === 'CallExpression'
&& node.callee.type === 'Identifier'
&& node.callee.name === word
var c = node.callee;
return c
&& node.type === 'CallExpression'
&& c.type === 'Identifier'
&& c.name === word
&& src.slice(c.range[0], c.range[1] + 1) === word
;
}

var modules = { strings : [], expressions : [] };

if (src.indexOf(word) == -1) return modules;

var slowPass = false;

walk(src, function (node) {
if (!isRequire(node)) return;
if (node.arguments.length
&& node.arguments[0].type === 'Literal') {
modules.strings.push(node.arguments[0].value);
}
else {
slowPass = true;
var r = node.arguments[0].range;
var s = src.slice(r[0], r[1] + 1);
modules.expressions.push(s);
}
});

if (slowPass) {
walkSlow(src, function (node) {
if (!isRequire(node)) return;
if (!node.arguments.length
|| node.arguments[0].type !== 'Literal') {
var r = node.arguments[0].range;
var s = src.slice(r[0], r[1] + 1);
modules.expressions.push(s);
}
});
}

return modules;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "detective",
"description" : "Find all calls to require() no matter how crazily nested using a proper walk of the AST",
"version" : "0.2.0",
"version" : "0.2.1",
"repository" : {
"type" : "git",
"url" : "git://github.com/substack/node-detective.git"
Expand Down

0 comments on commit a15e9b9

Please sign in to comment.