[Pkg-javascript-commits] [node-detective] 102/119: Use acorn's packaged traverser

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:44:39 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 b5430793e0711a71c84e6718461c2a89d3853221
Author: Andres Suarez <zertosh at gmail.com>
Date:   Thu Aug 20 00:43:32 2015 -0400

    Use acorn's packaged traverser
---
 index.js        | 61 ++++++++++++++++++---------------------------------------
 readme.markdown |  4 ++--
 2 files changed, 21 insertions(+), 44 deletions(-)

diff --git a/index.js b/index.js
index 9936d4a..0d0be7c 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,5 @@
-var aparse = require('acorn').parse;
+var acorn = require('acorn');
+var walk = require('acorn/dist/walk');
 var escodegen = require('escodegen');
 var defined = require('defined');
 
@@ -6,7 +7,7 @@ var requireRe = /\brequire\b/;
 
 function parse (src, opts) {
     if (!opts) opts = {};
-    return aparse(src, {
+    return acorn.parse(src, {
         ecmaVersion: defined(opts.ecmaVersion, 6),
         sourceType: opts.sourceType,
         ranges: defined(opts.ranges, opts.range),
@@ -19,31 +20,6 @@ function parse (src, opts) {
     });
 }
 
-var traverse = function (node, cb) {
-    if (Array.isArray(node)) {
-        for (var i = 0; i < node.length; i++) {
-            if (node[i] != null) {
-                node[i].parent = node;
-                traverse(node[i], cb);
-            }
-        }
-    }
-    else if (node && typeof node === 'object') {
-        cb(node);
-        for (var key in node) {
-            if (!node.hasOwnProperty(key)) continue;
-            if (key === 'parent' || !node[key]) continue;
-            node[key].parent = node;
-            traverse(node[key], cb);
-        }
-    }
-};
-
-var walk = function (src, opts, cb) {
-    var ast = parse(src, opts);
-    traverse(ast, cb);
-};
-
 var exports = module.exports = function (src, opts) {
     return exports.find(src, opts).strings;
 };
@@ -55,13 +31,10 @@ exports.find = function (src, opts) {
     if (typeof src !== 'string') src = String(src);
     
     var isRequire = opts.isRequire || function (node) {
-        var c = node.callee;
-        return c
-            && node.type === 'CallExpression'
-            && c.type === 'Identifier'
-            && c.name === word
+        return node.callee.type === 'Identifier'
+            && node.callee.name === word
         ;
-    }
+    };
     
     var modules = { strings : [], expressions : [] };
     if (opts.nodes) modules.nodes = [];
@@ -69,17 +42,21 @@ exports.find = function (src, opts) {
     var wordRe = word === 'require' ? requireRe : RegExp('\\b' + word + '\\b');
     if (!wordRe.test(src)) return modules;
     
-    walk(src, opts.parse, function (node) {
-        if (!isRequire(node)) return;
-        if (node.arguments.length) {
-            if (node.arguments[0].type === 'Literal') {
-                modules.strings.push(node.arguments[0].value);
-            }
-            else {
-                modules.expressions.push(escodegen.generate(node.arguments[0]));
+    var ast = parse(src, opts.parse);
+    
+    walk.simple(ast, {
+        CallExpression: function (node) {
+            if (!isRequire(node)) return;
+            if (node.arguments.length) {
+                if (node.arguments[0].type === 'Literal') {
+                    modules.strings.push(node.arguments[0].value);
+                }
+                else {
+                    modules.expressions.push(escodegen.generate(node.arguments[0]));
+                }
             }
+            if (opts.nodes) modules.nodes.push(node);
         }
-        if (opts.nodes) modules.nodes.push(node);
     });
     
     return modules;
diff --git a/readme.markdown b/readme.markdown
index e16b3e7..0cdd59f 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -61,8 +61,8 @@ Optionally:
 
 * `opts.word` - specify a different function name instead of `"require"`
 * `opts.nodes` - when `true`, populate `found.nodes`
-* `opts.isRequire(node)` - a function returning whether an AST node is a require
-call
+* `opts.isRequire(node)` - a function returning whether an AST `CallExpression`
+node is a require call
 * `opts.parse` - supply options directly to
 [acorn](https://npmjs.org/package/acorn) with some support for esprima-style
 options `range` and `loc`

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