Skip to content

Commit

Permalink
Scope hoisting destructuring (#2742)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored and devongovett committed May 24, 2019
1 parent e60a074 commit 4b50182
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(function(){
output = require('./b');
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const x = [1, 2]
const [a, b] = x;

module.exports = [a, b];
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"browserslist": ["node 8"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(function(){
output = require('./b');
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const x = {
a: 4,
b: 2
}
const {a, b} = x;

module.exports = [a, b];
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"browserslist": ["node 8"]
}
24 changes: 24 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -1229,5 +1229,29 @@ describe('scope hoisting', function() {
let output = await run(b);
assert.deepEqual(output, 2);
});

it('should support wrapping array destructuring declarations', async function() {
let b = await bundle(
path.join(
__dirname,
'/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'
)
);

let output = await run(b);
assert.deepEqual(output, [4, 2]);
});
});
});
33 changes: 23 additions & 10 deletions packages/core/parcel-bundler/src/packagers/JSConcatPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,30 @@ 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) || 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('=', decl.id, decl.init)
)
)
);
);
}
} 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)) {
Expand Down

0 comments on commit 4b50182

Please sign in to comment.