Skip to content

Commit

Permalink
Fix js-optimizer crash on empty array destructuring element
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Dec 5, 2023
1 parent fb0d986 commit 95b30a8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion test/optimizer/JSDCE-objectPattern-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let z = 50;
globalThis.f = function(r) {
let {a: a, b: b} = r;
let {z: c} = r;
let [i, {foo: p, bar: q}] = r;
let [, i, {foo: p, bar: q}] = r;
return g(a, b, c, d, z);
};

Expand Down
2 changes: 1 addition & 1 deletion test/optimizer/JSDCE-objectPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let z = 50;
globalThis.f = function(r) {
let { a, b } = r;
let { z: c } = r;
let [i, {foo : p, bar : q}] = r;
let [/*empty*/, i, {foo : p, bar : q}] = r;
return g(a, b, c, d, z);
};

Expand Down
4 changes: 3 additions & 1 deletion tools/acorn-optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,18 +429,20 @@ function runJSDCE(ast, aggressive) {
function traverse(id) {
if (id.type === 'ObjectPattern') {
for (const prop of id.properties) {
assertAt(prop.value, id);
traverse(prop.value);
}
} else if (id.type === 'ArrayPattern') {
for (const elem of id.elements) {
traverse(elem);
if (elem) traverse(elem);
}
} else {
assertAt(id.type === 'Identifier', id, `expected Indentifier but found ${id.type}`);
const name = id.name;
ensureData(scopes[scopes.length - 1], name).def = 1;
}
}
assertAt(node, node);
traverse(node.id);
if (node.init) c(node.init);
},
Expand Down

0 comments on commit 95b30a8

Please sign in to comment.