Skip to content

Commit

Permalink
Fix parsing of nested object patterns in acorn-optimizer.js
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Dec 4, 2023
1 parent 1dd30e1 commit e5253dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions test/optimizer/JSDCE-objectPattern-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +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;
return g(a, b, c, d, z);
};

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

Expand Down
31 changes: 14 additions & 17 deletions tools/acorn-optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,25 +424,22 @@ function runJSDCE(ast, aggressive) {

recursiveWalk(ast, {
VariableDeclarator(node, c) {
const id = node.id;
if (id.type === 'ObjectPattern') {
id.properties.forEach((node) => {
const value = node.value;
assert(value.type === 'Identifier');
const name = value.name;
ensureData(scopes[scopes.length - 1], name).def = 1;
});
} else if (id.type === 'ArrayPattern') {
id.elements.forEach((node) => {
assertAt(node.type === 'Identifier', node.start, "expected Indentifier but found " + node.type);
const name = node.name;
function traverse(id) {
if (id.type === 'ObjectPattern') {
for (const prop of id.properties) {
traverse(prop.value);
}
} else if (id.type === 'ArrayPattern') {
for (const elem of id.elements) {
traverse(elem)
}
} else {
assertAt(id.type === 'Identifier', id.start);
const name = id.name;
ensureData(scopes[scopes.length - 1], name).def = 1;
});
} else {
assertAt(id.type === 'Identifier', id.start);
const name = id.name;
ensureData(scopes[scopes.length - 1], name).def = 1;
}
}
traverse(node.id);
if (node.init) c(node.init);
},
ObjectExpression(node, c) {
Expand Down

0 comments on commit e5253dc

Please sign in to comment.