Skip to content

Commit

Permalink
Do not unpack array patterns that update a referenced binding
Browse files Browse the repository at this point in the history
We used this.scope.traverse to check if the array initializer referenced
a binding which is present in the array pattern. It doesn't work because,
in order to check if an identifier is a reference, we need to check its
parent.
this.scope.binding can't set the correct `parentPath`, because the only
path that it knows is the root scope's (e.g. the function path).
Use `t.traverseFast` which gives the correct parent.
  • Loading branch information
nicolo-ribaudo committed Aug 25, 2018
1 parent ca233d5 commit 3395ab5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/babel-plugin-transform-destructuring/src/index.js
Expand Up @@ -44,13 +44,16 @@ export default declare((api, options) => {
return false;
}

const arrayUnpackVisitor = {
ReferencedIdentifier(path, state) {
if (state.bindings[path.node.name]) {
state.deopt = true;
path.stop();
}
},
// NOTE: This visitor is meant to be used via t.traverseFast
const arrayUnpackVisitor = (node, state, parent) => {
if (
t.isIdentifier(node) &&
t.isReferenced(node, parent) &&
state.bindings[node.name]
) {
state.deopt = true;
return true;
}
};

class DestructuringTransformer {
Expand Down Expand Up @@ -281,8 +284,8 @@ export default declare((api, options) => {

// deopt on reference to left side identifiers
const bindings = t.getBindingIdentifiers(pattern);
const state = { deopt: false, bindings };
this.scope.traverse(arr, arrayUnpackVisitor, state);
const state = { deopt: false, bindings, array: arr };
t.traverseFast(arr, arrayUnpackVisitor, state);
return !state.deopt;
}

Expand Down
@@ -0,0 +1,4 @@
function isBetween(x, a, b) {
if (a > b) [a, b] = [b, a];
return x > a && x < b;
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-destructuring"]
}
@@ -0,0 +1,9 @@
function isBetween(x, a, b) {
if (a > b) {
var _ref = [b, a];
a = _ref[0];
b = _ref[1];
}

return x > a && x < b;
}

0 comments on commit 3395ab5

Please sign in to comment.