Skip to content

Commit

Permalink
Don't shake variables as computed property in member expressions (#3685)
Browse files Browse the repository at this point in the history
* Add test

* Fix shaking of computed property accesses

* More exhaustive test
  • Loading branch information
mischnic committed Oct 26, 2019
1 parent 36594cb commit 7175501
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import b from './b.js';
import c from './b.js';

output = [b('abc'), b('x'), c('abc'), c('x')];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let x = "abc",
y = "def";
let data = {};
data[x] = data[y] = true;

export default function(a) {
return data[a];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let x = "abc",
y = "def";
let data = {};
data[x] = true;
data[y] = true;

export default function(a) {
return data[a];
}
16 changes: 16 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,22 @@ describe('scope hoisting', function() {
assert(!contents.includes('method'));
});

it('keeps member expression with computed properties that are variables', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/tree-shaking-export-computed-prop/a.js'
),
{minify: true}
);

let output = await run(b);
assert.strictEqual(output[0], true);
assert.strictEqual(typeof output[1], 'undefined');
assert.strictEqual(output[2], true);
assert.strictEqual(typeof output[3], 'undefined');
});

it('support exporting a ES6 module exported as CommonJS', async function() {
let b = await bundle(
path.join(
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/scope-hoisting/src/shake.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ function isPure(binding) {

function isExportAssignment(path) {
return (
// match "path.any = any;"
// match "path.foo = bar;"
path.parentPath.isMemberExpression() &&
path.parentPath.node.object === path.node &&
path.parentPath.parentPath.isAssignmentExpression() &&
path.parentPath.parentPath.node.left === path.parentPath.node
);
Expand Down

0 comments on commit 7175501

Please sign in to comment.