[Pkg-javascript-commits] [node-astw] 02/30: fixes with a passing test adapted from falafel

Bastien Roucariès rouca at moszumanska.debian.org
Tue Sep 26 12:55:03 UTC 2017


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

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

commit 4846db7db1a20a50f8f578da504d55f09244e360
Author: James Halliday <mail at substack.net>
Date:   Sat Mar 2 02:15:08 2013 -0800

    fixes with a passing test adapted from falafel
---
 index.js       | 25 +++++++++++++++++++------
 test/parent.js | 26 ++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/index.js b/index.js
index 1d9b91d..4d71a1f 100644
--- a/index.js
+++ b/index.js
@@ -3,27 +3,40 @@ var parse = require('esprima').parse;
 module.exports = function (src) {
     var ast = typeof src === 'string' ? parse(src) : src;
     return function (cb) {
-        return walk(ast, undefined, cb);
+        walk(ast, undefined, cb);
     };
 };
 
 function walk (node, parent, cb) {
-    objectKeys(node).forEach(function (key) {
-        if (key === 'parent') return;
+    var keys = objectKeys(node);
+    for (var i = 0; i < keys.length; i++) {
+        var key = keys[i];
+        if (key === 'parent') continue;
         
         var child = node[key];
         if (isArray(child)) {
-            child.forEach(function (c) {
+            for (var j = 0; j < child.length; j++) {
+                var c = child[j];
                 if (c && typeof c.type === 'string') {
                     c.parent = node;
                     walk(c, node, cb);
                 }
-            });
+            }
         }
         else if (child && typeof child.type === 'string') {
             child.parent = node;
             walk(child, node, cb);
         }
-    });
+    }
     cb(node);
 }
+
+var isArray = Array.isArray || function (xs) {
+    return Object.prototype.toString.call(xs) === '[object Array]';
+};
+
+var objectKeys = Object.keys || function (obj) {
+    var keys = [];
+    for (var key in obj) keys.push(key);
+    return keys;
+};
diff --git a/test/parent.js b/test/parent.js
new file mode 100644
index 0000000..26e2011
--- /dev/null
+++ b/test/parent.js
@@ -0,0 +1,26 @@
+var astw = require('../');
+var test = require('tape');
+var generate = require('escodegen').generate;
+
+function unparse (s) {
+    return generate(s, { format: { compact: true } });
+}
+
+test('parent', function (t) {
+    t.plan(4);
+    
+    var walk = astw('(' + function () {
+        var xs = [ 1, 2, 3 ];
+        fn(ys);
+    } + ')()');
+    
+    walk(function (node) {
+        if (node.type === 'ArrayExpression') {
+            t.equal(node.parent.type, 'VariableDeclarator');
+            t.equal(unparse(node.parent), 'xs=[1,2,3]');
+            t.equal(node.parent.parent.type, 'VariableDeclaration');
+            t.equal(unparse(node.parent.parent), 'var xs=[1,2,3];'
+            );
+        }
+    });
+});

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



More information about the Pkg-javascript-commits mailing list