Skip to content

Commit 01be543

Browse files
authored
Fix support for JS destructuring in acorn optimizer pass (#20635)
Fixes: #20631
1 parent 35012ec commit 01be543

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var usedAsDefaultArg = 42;
22

3-
function g(a, b = usedAsDefaultArg) {
4-
return a + b + 1;
3+
var usedAsDefaultArg2 = [ 1, 2 ];
4+
5+
function g({notUsed: notUsed}, a, b = usedAsDefaultArg, [c, d] = usedAsDefaultArg2) {
6+
return a + b + notUsed + 1;
57
}
68

79
Module["g"] = g;

test/optimizer/JSDCE-defaultArg.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
var usedAsDefaultArg = 42;
2+
var usedAsDefaultArg2 = [1,2];
23
var notUsed = 43;
34

45
// exported
5-
function g(a, b = usedAsDefaultArg) {
6-
return a+b+1;
6+
function g({notUsed}, a, b = usedAsDefaultArg, [c, d] = usedAsDefaultArg2) {
7+
return a+b+notUsed+1;
78
}
89

910
Module['g'] = g;

tools/acorn-optimizer.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,18 +371,28 @@ function runJSDCE(ast, aggressive) {
371371
}
372372
const scope = {};
373373
scopes.push(scope);
374-
node.params.forEach((param) => {
374+
node.params.forEach(function traverse(param) {
375375
if (param.type === 'RestElement') {
376376
param = param.argument;
377377
}
378378
if (param.type === 'AssignmentPattern') {
379379
c(param.right);
380380
param = param.left;
381381
}
382-
assert(param.type === 'Identifier', param.type);
383-
const name = param.name;
384-
ensureData(scope, name).def = 1;
385-
scope[name].param = 1;
382+
if (param.type === 'ArrayPattern') {
383+
for (var elem of param.elements) {
384+
traverse(elem);
385+
}
386+
} else if (param.type === 'ObjectPattern') {
387+
for (var prop of param.properties) {
388+
traverse(prop.key);
389+
}
390+
} else {
391+
assert(param.type === 'Identifier', param.type);
392+
const name = param.name;
393+
ensureData(scope, name).def = 1;
394+
scope[name].param = 1;
395+
}
386396
});
387397
c(node.body);
388398
// we can ignore self-references, i.e., references to ourselves inside

0 commit comments

Comments
 (0)