Skip to content

Commit

Permalink
Add support for an option to include ranges in the response
Browse files Browse the repository at this point in the history
Add an opts.ranges option in order to get a stringRanges response back
from detective, including string locations for the matched strings.
Currently only works for Literals in order to hack in an improvement
for browserify/browserify#296 .
  • Loading branch information
rowanbeentje committed May 20, 2013
1 parent ab71a3e commit d4f6cd0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var traverse = function (node, cb) {
};

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

Expand All @@ -30,10 +30,21 @@ var exports = module.exports = function (src, opts) {

exports.find = function (src, opts) {
if (!opts) opts = {};
var rangeOffset = 0;
var word = opts.word === undefined ? 'require' : opts.word;
if (typeof src !== 'string') src = String(src);
src = '(function(){' + src.replace(/^#![^\n]*\n/, '') + '\n})()';


// Remove any hashbang content, modifying subsequent ranges to match
rangeOffset += src.length;
src = src.replace(/^#![^\n]*\n/, '');
rangeOffset -= src.length;

// Wrap the provided source within its own scope, modifying subsequent ranges to match
var presrc = '(function(){';
var postsrc = '\n})()';
src = presrc + src + postsrc;
rangeOffset -= presrc.length;

function isRequire (node) {
var c = node.callee;
return c
Expand All @@ -45,6 +56,7 @@ exports.find = function (src, opts) {

var modules = { strings : [], expressions : [] };
if (opts.nodes) modules.nodes = [];
if (opts.ranges) modules.stringRanges = [];

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

Expand All @@ -53,6 +65,9 @@ exports.find = function (src, opts) {
if (node.arguments.length
&& node.arguments[0].type === 'Literal') {
modules.strings.push(node.arguments[0].value);
if (opts.ranges) {
modules.stringRanges.push([node.arguments[0].range[0] + rangeOffset, node.arguments[0].range[1] + rangeOffset]);
}
}
else {
modules.expressions.push(escodegen.generate(node.arguments[0]));
Expand Down

0 comments on commit d4f6cd0

Please sign in to comment.