From 4c266bb7c8a9c94791f928507a8d2bd8c4912aa9 Mon Sep 17 00:00:00 2001 From: Yun Jia Date: Sun, 24 Mar 2019 21:39:17 -0700 Subject: [PATCH] Fix #358: handle array destructuring assignment Also a new test case is added to `misc tests -> misc tests` --- src/ast-utils.js | 4 +++ .../array-destructuring-assignment/main.js | 9 ++++++ test/test.js | 32 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 test/samples/array-destructuring-assignment/main.js diff --git a/src/ast-utils.js b/src/ast-utils.js index cc6e901..4eaeb7f 100644 --- a/src/ast-utils.js +++ b/src/ast-utils.js @@ -60,6 +60,10 @@ const extractors = { AssignmentPattern(names, node) { extractors[node.left.type](names, node.left); + }, + + MemberExpression(names, node) { + extractors[node.property.type](names, node.property) } }; diff --git a/test/samples/array-destructuring-assignment/main.js b/test/samples/array-destructuring-assignment/main.js new file mode 100644 index 0000000..451ba3e --- /dev/null +++ b/test/samples/array-destructuring-assignment/main.js @@ -0,0 +1,9 @@ + +function shuffleArray(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } +} + +exports.shuffleArray = shuffleArray; diff --git a/test/test.js b/test/test.js index f4b4033..6deae88 100644 --- a/test/test.js +++ b/test/test.js @@ -727,6 +727,38 @@ var esm = /*#__PURE__*/Object.freeze({ var main = esm; module.exports = main; +` + ); + }); + + it('handles array destructuring assignment', async () => { + const bundle = await rollup({ + input: 'samples/array-destructuring-assignment/main.js', + plugins: [commonjs({ sourceMap: true })] + }); + + const code = await getCodeFromBundle(bundle); + assert.equal( + code, + `'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); + +function shuffleArray(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } +} + +var shuffleArray_1 = shuffleArray; + +var main = { + shuffleArray: shuffleArray_1 +}; + +exports.default = main; +exports.shuffleArray = shuffleArray_1; ` ); });