Skip to content

Commit 43876d0

Browse files
authored
[acorn-opt] Add walkPattern helper. NFC (#24499)
Instead of implementing custom recursive functions, use recursiveWalk from a shared helper. It will automatically invoke one callback for nested expressions (e.g. computed properties or defaults) and another callback for any bound names.
1 parent c9d7713 commit 43876d0

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

tools/acorn-optimizer.mjs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ function dump(node) {
100100
console.log(JSON.stringify(node, null, ' '));
101101
}
102102

103+
// Traverse a pattern node (identifier, object/array pattern, etc) invoking onExpr on any nested expressions and onBoundIdent on any bound identifiers.
104+
function walkPattern(node, onExpr, onBoundIdent) {
105+
recursiveWalk(node, {
106+
AssignmentPattern(node, c) {
107+
c(node.left);
108+
onExpr(node.right);
109+
},
110+
Property(node, c) {
111+
if (node.computed) {
112+
onExpr(node.key);
113+
}
114+
c(node.value);
115+
},
116+
Identifier({name}) {
117+
onBoundIdent(name);
118+
},
119+
});
120+
}
121+
103122
function hasSideEffects(node) {
104123
// Conservative analysis.
105124
let has = false;
@@ -270,29 +289,12 @@ function JSDCE(ast, aggressive) {
270289
}
271290
const scope = {};
272291
scopes.push(scope);
273-
node.params.forEach(function traverse(param) {
274-
if (param.type === 'RestElement') {
275-
param = param.argument;
276-
}
277-
if (param.type === 'AssignmentPattern') {
278-
c(param.right);
279-
param = param.left;
280-
}
281-
if (param.type === 'ArrayPattern') {
282-
for (var elem of param.elements) {
283-
if (elem) traverse(elem);
284-
}
285-
} else if (param.type === 'ObjectPattern') {
286-
for (var prop of param.properties) {
287-
traverse(prop.key);
288-
}
289-
} else {
290-
assert(param.type === 'Identifier', param.type);
291-
const name = param.name;
292+
for (const param of node.params) {
293+
walkPattern(param, c, (name) => {
292294
ensureData(scope, name).def = 1;
293295
scope[name].param = 1;
294-
}
295-
});
296+
});
297+
}
296298
c(node.body);
297299
// we can ignore self-references, i.e., references to ourselves inside
298300
// ourselves, for named defined (defun) functions
@@ -316,22 +318,9 @@ function JSDCE(ast, aggressive) {
316318

317319
recursiveWalk(ast, {
318320
VariableDeclarator(node, c) {
319-
function traverse(id) {
320-
if (id.type === 'ObjectPattern') {
321-
for (const prop of id.properties) {
322-
traverse(prop.value);
323-
}
324-
} else if (id.type === 'ArrayPattern') {
325-
for (const elem of id.elements) {
326-
if (elem) traverse(elem);
327-
}
328-
} else {
329-
assertAt(id.type === 'Identifier', id, `expected Identifier but found ${id.type}`);
330-
const name = id.name;
331-
ensureData(scopes[scopes.length - 1], name).def = 1;
332-
}
333-
}
334-
traverse(node.id);
321+
walkPattern(node.id, c, (name) => {
322+
ensureData(scopes[scopes.length - 1], name).def = 1;
323+
});
335324
if (node.init) c(node.init);
336325
},
337326
ObjectExpression(node, c) {

0 commit comments

Comments
 (0)