Skip to content

Commit 828f1fe

Browse files
authored
[acorn-opt] Use walkPattern for variable declarators too (#24553)
1 parent f5dcbde commit 828f1fe

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

test/js_optimizer/JSDCE-objectPattern-output.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ let z = 50;
55
globalThis.f = function([, r]) {
66
let {a, b} = r;
77
let {z: c} = r;
8-
let [, i, {foo: p, bar: q}] = r;
98
return g(a, b, c, d, z);
109
};
1110

tools/acorn-optimizer.mjs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,16 @@ function JSDCE(ast, aggressive) {
243243
let removedHere = 0;
244244
node.declarations = node.declarations.filter((node) => {
245245
assert(node.type === 'VariableDeclarator');
246-
const id = node.id;
247-
if (id.type === 'ObjectPattern' || id.type === 'ArrayPattern') {
248-
// TODO: DCE into object patterns, that is, things like
249-
// let { a, b } = ..
250-
// let [ a, b ] = ..
251-
return true;
252-
}
253-
assert(id.type === 'Identifier');
254-
const curr = id.name;
255-
const value = node.init;
256-
const keep = !names.has(curr) || (value && hasSideEffects(value));
246+
let keep = node.init && hasSideEffects(node.init);
247+
walkPattern(
248+
node.id,
249+
(value) => {
250+
keep ||= hasSideEffects(value);
251+
},
252+
(boundName) => {
253+
keep ||= !names.has(boundName);
254+
},
255+
);
257256
if (!keep) removedHere = 1;
258257
return keep;
259258
});

0 commit comments

Comments
 (0)