[Pkg-javascript-commits] [node-detective] 41/119: remove range capture from esprima ast

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:44:33 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-detective.

commit fa3cbe775ca4f3e2e4bf4e5b313aae699be7cd52
Author: Roman Shtylman <shtylman at gmail.com>
Date:   Fri Nov 23 22:25:55 2012 -0500

    remove range capture from esprima ast
    
    Improves the parse time by a significant amount.
    
    Use escodegen to re-create the source for reference when an expression
    is passed to require.
---
 index.js     | 8 +++-----
 package.json | 3 ++-
 test/both.js | 2 +-
 3 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/index.js b/index.js
index 7c8840a..e5380f3 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,5 @@
 var esprima = require('esprima');
+var escodegen = require('escodegen');
 
 var traverse = function (node, cb) {
     if (Array.isArray(node)) {
@@ -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);
 };
 
@@ -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
         ;
     }
     
@@ -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]));
         }
     });
     
diff --git a/package.json b/package.json
index 21f4145..4cd508c 100644
--- a/package.json
+++ b/package.json
@@ -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"
diff --git a/test/both.js b/test/both.js
index 5a59da9..7e09243 100644
--- a/test/both.js
+++ b/test/both.js
@@ -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();
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-detective.git



More information about the Pkg-javascript-commits mailing list