[Pkg-javascript-commits] [node-detective] 33/119: slow pass to pick off expression strings

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:44:32 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 1d279b2e3c0cd7cb33033d7ced3a5920cb1ea8ce
Author: James Halliday <mail at substack.net>
Date:   Mon Jul 30 07:37:12 2012 -0700

    slow pass to pick off expression strings
---
 index.js     | 53 ++++++++++++++++++++++++++++++++++++++---------------
 test/both.js |  2 +-
 2 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/index.js b/index.js
index 15d9e7d..724d742 100644
--- a/index.js
+++ b/index.js
@@ -8,14 +8,20 @@ var traverse = function (node, cb) {
     }
     else if (node && typeof node === 'object') {
         cb(node);
+        
         Object.keys(node).forEach(function (key) {
             traverse(node[key], cb);
         });
     }
 };
 
-var walk = function (src, cb, opts) {
-    var ast = esprima.parse(src.toString());
+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);
 };
 
@@ -26,26 +32,43 @@ var exports = module.exports = function (src, opts) {
 exports.find = function (src, opts) {
     if (!opts) opts = {};
     var word = opts.word === undefined ? 'require' : opts.word;
+    if (typeof src !== 'string') src = String(src);
+    
+    function isRequire (node) {
+        return node.type === 'CallExpression'
+            && node.callee.type === 'Identifier'
+            && node.callee.name === word
+        ;
+    }
     
     var modules = { strings : [], expressions : [] };
     
-    if (src.toString().indexOf(word) == -1) return modules;
+    if (src.indexOf(word) == -1) return modules;
+    
+    var slowPass = false;
     
     walk(src, function (node) {
-        var isRequire =
-            node.type === 'CallExpression'
-            && node.callee.type === 'Identifier'
-            && node.callee.name === word
-        ;
-        if (isRequire) {
-            if (node.arguments.length && node.arguments[0].type === 'Literal') {
-                modules.strings.push(node.arguments[0].value);
-            }
-            else {
-                modules.expressions.push('...');
-            }
+        if (!isRequire(node)) return;
+        if (node.arguments.length
+        && node.arguments[0].type === 'Literal') {
+            modules.strings.push(node.arguments[0].value);
+        }
+        else {
+            slowPass = true;
         }
     });
     
+    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;
 };
diff --git a/test/both.js b/test/both.js
index 971aebb..5a59da9 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