[Pkg-javascript-commits] [node-detective] 30/119: pass chaining test

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 39f70e19947d9f79471b00bfd34e00baab0c47d7
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date:   Tue Apr 24 00:46:41 2012 +1200

    pass chaining test
---
 index.js | 60 ++++++++++++++----------------------------------------------
 1 file changed, 14 insertions(+), 46 deletions(-)

diff --git a/index.js b/index.js
index d3bfd3f..8df9358 100644
--- a/index.js
+++ b/index.js
@@ -1,16 +1,16 @@
 var uglify = require('uglify-js');
 
-var traverse = function (node, cb) {
+var traverse = function (node, cb, parent, grandparent) {
     // Call cb on all good AST nodes.
     if (Array.isArray(node) && node[0]
     && typeof node[0] === 'object' && node[0].name) {
-        cb({ name : node[0].name, value : node.slice(1) });
+        cb({ name : node[0].name, value : node.slice(1) , grandparent: grandparent});
     }
     
     // Traverse down the tree on arrays and objects.
     if (Array.isArray(node)
     || Object.prototype.toString.call(node) === "[object Object]") {
-        for (var key in node) traverse(node[key], cb);
+        for (var key in node) traverse(node[key], cb, node, parent);
     }
 };
 
@@ -36,50 +36,18 @@ exports.find = function (src, opts) {
     if (src.toString().indexOf(word) == -1) return modules;
     
     walk(src, function (node) {
-        var isRequire = node.name === 'call'
-            && node.value[0][0] === 'name'
-            && node.value[0][1] === word
+        var gp = node.grandparent;
+        var isRequire = Array.isArray(gp)
+            && gp[0] 
+            && (gp[0] === 'call' || gp[0].name === 'call')
+            && gp[1][0] === 'name'
+            && gp[1][1] === word
         ;
-        if (isRequire) {
-            var expr = node.value[1][0];
-            
-            if (expr[0].name === 'string') {
-                modules.strings.push(expr[1]);
-            }
-            else {
-                modules.expressions.push(deparse(expr));
-            }
-        }
-        
-        var isDotRequire = (node.name === 'dot' || node.name === 'call')
-            && node.value[0][0] === 'call'
-            && node.value[0][1][0] === 'name'
-            && node.value[0][1][1] === word
-        ;
-        
-        if (isDotRequire) {
-            var expr = node.value[0][2][0];
-            if (expr[0].name === 'string') {
-                modules.strings.push(expr[1]);
-            }
-            else {
-                modules.expressions.push(deparse(expr));
-            }
-        }
-        
-        var isDotCallRequire = node.name === 'call'
-            && node.value[0][0] === 'dot'
-            && node.value[0][1][0] === 'call'
-            && node.value[0][1][1][0] === 'name'
-            && node.value[0][1][1][1] === word
-        ;
-        if (isDotCallRequire) {
-            var expr = node.value[0][1][2][0];
-            if (expr[0].name === 'string') {
-                modules.strings.push(expr[1]);
-            }
-            else {
-                modules.expressions.push(deparse(expr));
+        if(isRequire) {
+            if(node.name === 'string') {
+                modules.strings.push(node.value[0]);
+            } else {
+                modules.expressions.push(deparse(gp[2][0]));
             }
         }
     });

-- 
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