From 0110d18d4c1d061beb9b2060299a090d9ddcc944 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 26 Jan 2015 17:43:11 +1100 Subject: [PATCH] clearn up traversal and fix replacement node arrays not being traversed - fixes #589 --- lib/6to5/traverse/index.js | 31 ++++++++++++------- .../es6-destructuring/chained/exec.js | 6 ++++ 2 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/transformation/es6-destructuring/chained/exec.js diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index d18a1a33e6..f50b078383 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -115,8 +115,14 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent, if (this.shouldSkip) return this.shouldStop; - traverseNode(node, opts, ourScope, state); - this.exitNode(obj, key, node, opts.exit, parent, ourScope, state); + if (Array.isArray(node)) { + for (var i = 0; i < node.length; i++) { + traverseNode(node[i], opts, ourScope, state); + } + } else { + traverseNode(node, opts, ourScope, state); + this.exitNode(obj, key, node, opts.exit, parent, ourScope, state); + } return this.shouldStop; }; @@ -133,9 +139,10 @@ TraversalContext.prototype.visit = function (node, key, opts, scope, state) { return; } - for (var k = 0; k < nodes.length; k++) { - if (nodes[k] && this.visitNode(nodes, k, opts, scope, node, state)) + for (var i = 0; i < nodes.length; i++) { + if (nodes[i] && this.visitNode(nodes, i, opts, scope, node, state)) { return true; + } } if (this.shouldFlatten) { @@ -148,20 +155,19 @@ TraversalContext.prototype.visit = function (node, key, opts, scope, state) { } }; -function traverseNode(node, opts, scope, state) { +function traverseNode(node, opts, scope, state, debug) { var keys = t.VISITOR_KEYS[node.type]; if (!keys) return; var context = new TraversalContext(); - for (var j = 0; j < keys.length; j++) { - if (context.visit(node, keys[j], opts, scope, state)) { + for (var i = 0; i < keys.length; i++) { + if (context.visit(node, keys[i], opts, scope, state)) { return; } } } function traverse(parent, opts, scope, state) { - // falsy node if (!parent) return; if (!scope) { @@ -195,18 +201,21 @@ function clearNode(node) { node.loc = null; node.raw = null; - if (Array.isArray(node.trailingComments)) + if (Array.isArray(node.trailingComments)) { clearComments(node.trailingComments); + } - if (Array.isArray(node.leadingComments)) + if (Array.isArray(node.leadingComments)) { clearComments(node.leadingComments); + } } var clearVisitor = { enter: clearNode }; function clearComments(comments) { - for (var i = 0; i < comments.length; i++) + for (var i = 0; i < comments.length; i++) { clearNode(comments[i]); + } } traverse.removeProperties = function (tree) { diff --git a/test/fixtures/transformation/es6-destructuring/chained/exec.js b/test/fixtures/transformation/es6-destructuring/chained/exec.js new file mode 100644 index 0000000000..fca8b7ab2a --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring/chained/exec.js @@ -0,0 +1,6 @@ +var a, b, c, d; +({a,b}) = {c,d} = {a:1,b:2,c:3,d:4}; +assert.equal(a, 1); +assert.equal(b, 2); +assert.equal(c, 3); +assert.equal(d, 4);