From 57e656d87bc3c0a03440c44a262b21362ff52848 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Sun, 25 Nov 2018 19:29:58 +0100 Subject: [PATCH] Fix destructuring assignment in arrow functions. (#8916) Fixes 8912. --- src/index.js | 7 ++++++- .../destructuring/assignment-arrow-function-block/input.js | 1 + .../assignment-arrow-function-block/output.js | 4 ++++ .../assignment-arrow-function-no-block/input.js | 1 + .../assignment-arrow-function-no-block/output.js | 6 ++++++ 5 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/destructuring/assignment-arrow-function-block/input.js create mode 100644 test/fixtures/destructuring/assignment-arrow-function-block/output.js create mode 100644 test/fixtures/destructuring/assignment-arrow-function-no-block/input.js create mode 100644 test/fixtures/destructuring/assignment-arrow-function-no-block/output.js diff --git a/src/index.js b/src/index.js index ccc76fb918d9..3e8bd464298c 100644 --- a/src/index.js +++ b/src/index.js @@ -540,7 +540,12 @@ export default declare((api, options) => { destructuring.init(node.left, ref || node.right); if (ref) { - nodes.push(t.expressionStatement(t.cloneNode(ref))); + if (path.parentPath.isArrowFunctionExpression()) { + path.replaceWith(t.blockStatement([])); + nodes.push(t.returnStatement(t.cloneNode(ref))); + } else { + nodes.push(t.expressionStatement(t.cloneNode(ref))); + } } path.replaceWithMultiple(nodes); diff --git a/test/fixtures/destructuring/assignment-arrow-function-block/input.js b/test/fixtures/destructuring/assignment-arrow-function-block/input.js new file mode 100644 index 000000000000..71ebd5203878 --- /dev/null +++ b/test/fixtures/destructuring/assignment-arrow-function-block/input.js @@ -0,0 +1 @@ +() => { [a, b] = [1, 2] } diff --git a/test/fixtures/destructuring/assignment-arrow-function-block/output.js b/test/fixtures/destructuring/assignment-arrow-function-block/output.js new file mode 100644 index 000000000000..4dca9fc16ca5 --- /dev/null +++ b/test/fixtures/destructuring/assignment-arrow-function-block/output.js @@ -0,0 +1,4 @@ +() => { + a = 1; + b = 2; +}; diff --git a/test/fixtures/destructuring/assignment-arrow-function-no-block/input.js b/test/fixtures/destructuring/assignment-arrow-function-no-block/input.js new file mode 100644 index 000000000000..da9e7d37e015 --- /dev/null +++ b/test/fixtures/destructuring/assignment-arrow-function-no-block/input.js @@ -0,0 +1 @@ +() => [a, b] = [1, 2] diff --git a/test/fixtures/destructuring/assignment-arrow-function-no-block/output.js b/test/fixtures/destructuring/assignment-arrow-function-no-block/output.js new file mode 100644 index 000000000000..408faf04b5e8 --- /dev/null +++ b/test/fixtures/destructuring/assignment-arrow-function-no-block/output.js @@ -0,0 +1,6 @@ +() => { + var _ref = [1, 2]; + a = _ref[0]; + b = _ref[1]; + return _ref; +};