Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope hoisting destructuring #2742

Merged
merged 7 commits into from May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,3 @@
(function(){
output = require('./b');
})();
@@ -0,0 +1,4 @@
const x = [1, 2]
const [a, b] = x;

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

module.exports = [a, b];
@@ -0,0 +1,4 @@
{
"private": true,
"browserslist": ["node 8"]
}
24 changes: 24 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
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]);
});
});
});
53 changes: 43 additions & 10 deletions packages/core/parcel-bundler/src/packagers/JSConcatPackager.js
Expand Up @@ -283,17 +283,50 @@ 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) {
mischnic marked this conversation as resolved.
Show resolved Hide resolved
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++) {
mischnic marked this conversation as resolved.
Show resolved Hide resolved
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) {
body.push(
t.expressionStatement(
t.assignmentExpression(
'=',
t.identifier(decl.id.name),
decl.init
)
)
)
);
);
}
}
}
} else if (t.isFunctionDeclaration(node)) {
Expand Down