From 2dea83312e59a8862dab4e366615bd410be5fda0 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Sat, 2 Mar 2019 17:56:36 +0100 Subject: [PATCH 1/4] Handle object destructuring a require call --- .../src/packagers/JSConcatPackager.js | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js b/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js index 2c26b52d22a..02a68deaf67 100644 --- a/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js +++ b/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js @@ -283,17 +283,34 @@ class JSConcatPackager extends Packager { // so that they can be referenced by other modules directly. if (t.isVariableDeclaration(node)) { for (let decl of node.declarations) { - decls.push(t.variableDeclarator(decl.id)); - if (decl.init) { - body.push( - t.expressionStatement( - t.assignmentExpression( - '=', - t.identifier(decl.id.name), - decl.init + if (t.isObjectPattern(decl.id)) { + for (let prop of decl.id.properties) { + decls.push(t.variableDeclarator(prop.value)); + if (decl.init) { + body.push( + t.expressionStatement( + t.assignmentExpression( + '=', + t.identifier(prop.value.name), + t.memberExpression(decl.init, prop.key) + ) + ) + ); + } + } + } else { + decls.push(t.variableDeclarator(decl.id)); + if (decl.init) { + body.push( + t.expressionStatement( + t.assignmentExpression( + '=', + t.identifier(decl.id.name), + decl.init + ) ) - ) - ); + ); + } } } } else if (t.isFunctionDeclaration(node)) { From e08f2cc6704c5346a05a7ba310ff1419555337ee Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Fri, 8 Mar 2019 00:19:42 +0100 Subject: [PATCH 2/4] Add test --- .../scope-hoisting/commonjs/wrap-destructuring/a.js | 3 +++ .../scope-hoisting/commonjs/wrap-destructuring/b.js | 7 +++++++ .../commonjs/wrap-destructuring/package.json | 4 ++++ .../core/integration-tests/test/scope-hoisting.js | 12 ++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/a.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/b.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/package.json diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/a.js new file mode 100644 index 00000000000..00fea395d12 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/a.js @@ -0,0 +1,3 @@ +(function(){ + output = require('./b'); +})(); diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/b.js new file mode 100644 index 00000000000..321586ca6d8 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/b.js @@ -0,0 +1,7 @@ +const x = { + a: 4, + b: 2 +} +const {a, b} = x; + +module.exports = [a, b]; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/package.json new file mode 100644 index 00000000000..97b77ba2dde --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/package.json @@ -0,0 +1,4 @@ +{ + "private": false, + "browserslist": ["node 8"] +} diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index df12a4a00ec..00bd5f607d4 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -1172,5 +1172,17 @@ describe('scope hoisting', function() { let output = await run(b); assert.deepEqual(output, 42); }); + + it('should support wrapping destructuring declarations', async function() { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/commonjs/wrap-destructuring/a.js' + ) + ); + + let output = await run(b); + assert.deepEqual(output, [4, 2]); + }); }); }); From aa00645545516a170accdb4866413024449fe0b8 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Fri, 8 Mar 2019 00:30:40 +0100 Subject: [PATCH 3/4] Array destructuring as well --- .../a.js | 0 .../commonjs/wrap-destructuring-array/b.js | 4 ++++ .../package.json | 2 +- .../commonjs/wrap-destructuring-object/a.js | 3 +++ .../b.js | 0 .../wrap-destructuring-object/package.json | 4 ++++ .../integration-tests/test/scope-hoisting.js | 16 ++++++++++++++-- .../src/packagers/JSConcatPackager.js | 16 ++++++++++++++++ 8 files changed, 42 insertions(+), 3 deletions(-) rename packages/core/integration-tests/test/integration/scope-hoisting/commonjs/{wrap-destructuring => wrap-destructuring-array}/a.js (100%) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/b.js rename packages/core/integration-tests/test/integration/scope-hoisting/commonjs/{wrap-destructuring => wrap-destructuring-array}/package.json (61%) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/a.js rename packages/core/integration-tests/test/integration/scope-hoisting/commonjs/{wrap-destructuring => wrap-destructuring-object}/b.js (100%) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/package.json diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/a.js similarity index 100% rename from packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/a.js rename to packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/a.js diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/b.js new file mode 100644 index 00000000000..71411c79d3b --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/b.js @@ -0,0 +1,4 @@ +const x = [1, 2] +const [a, b] = x; + +module.exports = [a, b]; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/package.json similarity index 61% rename from packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/package.json rename to packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/package.json index 97b77ba2dde..93281f0b785 100644 --- a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/package.json +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/package.json @@ -1,4 +1,4 @@ { - "private": false, + "private": true, "browserslist": ["node 8"] } diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/a.js new file mode 100644 index 00000000000..00fea395d12 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/a.js @@ -0,0 +1,3 @@ +(function(){ + output = require('./b'); +})(); diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/b.js similarity index 100% rename from packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring/b.js rename to packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/b.js diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/package.json new file mode 100644 index 00000000000..93281f0b785 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/package.json @@ -0,0 +1,4 @@ +{ + "private": true, + "browserslist": ["node 8"] +} diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index 00bd5f607d4..3e7b857d700 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -1173,11 +1173,23 @@ describe('scope hoisting', function() { assert.deepEqual(output, 42); }); - it('should support wrapping destructuring declarations', async function() { + it('should support wrapping array destructuring declarations', async function() { let b = await bundle( path.join( __dirname, - '/integration/scope-hoisting/commonjs/wrap-destructuring/a.js' + '/integration/scope-hoisting/commonjs/wrap-destructuring-array/a.js' + ) + ); + + let output = await run(b); + assert.deepEqual(output, [1, 2]); + }); + + it('should support wrapping object destructuring declarations', async function() { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/commonjs/wrap-destructuring-object/a.js' ) ); diff --git a/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js b/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js index 02a68deaf67..569d8b6792c 100644 --- a/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js +++ b/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js @@ -298,6 +298,22 @@ class JSConcatPackager extends Packager { ); } } + } else if (t.isArrayPattern(decl.id)) { + for (let i = 0; i < decl.id.elements.length; i++) { + let prop = decl.id.elements[i]; + decls.push(t.variableDeclarator(prop)); + if (decl.init) { + body.push( + t.expressionStatement( + t.assignmentExpression( + '=', + prop, + t.memberExpression(decl.init, t.numericLiteral(i), true) + ) + ) + ); + } + } } else { decls.push(t.variableDeclarator(decl.id)); if (decl.init) { From 3b35286d57699aceb1c36695cadec82b5c2a874d Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Fri, 24 May 2019 16:48:13 +0200 Subject: [PATCH 4/4] Massively simplify destructuring --- .../src/packagers/JSConcatPackager.js | 38 +++++-------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js b/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js index 569d8b6792c..f4504cbfc5d 100644 --- a/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js +++ b/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js @@ -283,36 +283,16 @@ class JSConcatPackager extends Packager { // so that they can be referenced by other modules directly. if (t.isVariableDeclaration(node)) { for (let decl of node.declarations) { - if (t.isObjectPattern(decl.id)) { - for (let prop of decl.id.properties) { - decls.push(t.variableDeclarator(prop.value)); - if (decl.init) { - body.push( - t.expressionStatement( - t.assignmentExpression( - '=', - t.identifier(prop.value.name), - t.memberExpression(decl.init, prop.key) - ) - ) - ); - } - } - } else if (t.isArrayPattern(decl.id)) { - for (let i = 0; i < decl.id.elements.length; i++) { - let prop = decl.id.elements[i]; + if (t.isObjectPattern(decl.id) || t.isArrayPattern(decl.id)) { + for (let prop of Object.values(t.getBindingIdentifiers(decl.id))) { decls.push(t.variableDeclarator(prop)); - if (decl.init) { - body.push( - t.expressionStatement( - t.assignmentExpression( - '=', - prop, - t.memberExpression(decl.init, t.numericLiteral(i), true) - ) - ) - ); - } + } + if (decl.init) { + body.push( + t.expressionStatement( + t.assignmentExpression('=', decl.id, decl.init) + ) + ); } } else { decls.push(t.variableDeclarator(decl.id));