Skip to content

Commit

Permalink
remove range capture from esprima ast
Browse files Browse the repository at this point in the history
Improves the parse time by a significant amount.

Use escodegen to re-create the source for reference when an expression
is passed to require.
  • Loading branch information
defunctzombie committed Nov 24, 2012
1 parent 3044af7 commit fa3cbe7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
8 changes: 3 additions & 5 deletions index.js
@@ -1,4 +1,5 @@
var esprima = require('esprima');
var escodegen = require('escodegen');

var traverse = function (node, cb) {
if (Array.isArray(node)) {
Expand All @@ -16,7 +17,7 @@ var traverse = function (node, cb) {
};

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

Expand All @@ -35,7 +36,6 @@ exports.find = function (src, opts) {
&& node.type === 'CallExpression'
&& c.type === 'Identifier'
&& c.name === word
&& src.slice(c.range[0], c.range[1]) === word
;
}

Expand All @@ -50,9 +50,7 @@ exports.find = function (src, opts) {
modules.strings.push(node.arguments[0].value);
}
else {
var r = node.arguments[0].range;
var s = src.slice(r[0], r[1]);
modules.expressions.push(s);
modules.expressions.push(escodegen.generate(node.arguments[0]));
}
});

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -22,7 +22,8 @@
"test" : "tap test/*.js"
},
"dependencies" : {
"esprima" : "1.0.2"
"esprima" : "1.0.2",
"escodegen": "0.0.15"
},
"devDependencies" : {
"tap" : "~0.2.6"
Expand Down
2 changes: 1 addition & 1 deletion test/both.js
Expand Up @@ -6,6 +6,6 @@ var src = fs.readFileSync(__dirname + '/files/both.js');
test('both', function (t) {
var modules = detective.find(src);
t.deepEqual(modules.strings, [ 'a', 'b' ]);
t.deepEqual(modules.expressions, [ "'c'+x", "'d'+y" ]);
t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]);
t.end();
});

0 comments on commit fa3cbe7

Please sign in to comment.