Skip to content

Commit

Permalink
fix corner case in collapse_vars (#5503)
Browse files Browse the repository at this point in the history
fixes #5502
  • Loading branch information
alexlamsl committed Jun 9, 2022
1 parent f749863 commit 2501797
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2183,7 +2183,7 @@ Compressor.prototype.compress = function(node) {
can_replace = replace;
return signal_abort(node);
}
return handle_custom_scan_order(node, scanner);
if (handle_custom_scan_order(node, scanner)) return signal_abort(node);
}, signal_abort);
var multi_replacer = new TreeTransformer(function(node) {
if (abort) return node;
Expand Down Expand Up @@ -2345,14 +2345,28 @@ Compressor.prototype.compress = function(node) {
}

function handle_custom_scan_order(node, tt) {
if (!(node instanceof AST_BlockScope)) {
if (!(node instanceof AST_ClassProperty && !node.static)) return;
// Skip non-static class property values
if (node.key instanceof AST_Node) node.key = node.key.transform(tt);
return node;
}
if (!(node instanceof AST_BlockScope)) return;
// Skip (non-executed) functions
if (node instanceof AST_Scope) return node;
// Scan computed keys, static fields & initializers in class
if (node instanceof AST_Class) {
if (node.name) node.name = node.name.transform(tt);
if (node.extends) node.extends = node.extends.transform(tt);
node.properties.reduce(function(props, prop) {
if (prop.key instanceof AST_Node) prop.key = prop.key.transform(tt);
if (prop.static) {
if (prop instanceof AST_ClassField) {
props.push(prop);
} else if (prop instanceof AST_ClassInit) {
props.unshift(prop);
}
}
return props;
}, []).forEach(function(prop) {
prop.value = prop.value.transform(tt);
});
return node;
}
// Scan object only in a for-in/of statement
if (node instanceof AST_ForEnumeration) {
node.object = node.object.transform(tt);
Expand Down
36 changes: 36 additions & 0 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3143,3 +3143,39 @@ issue_5489_strict: {
]
node_version: ">=16"
}

issue_5502: {
options = {
collapse_vars: true,
}
input: {
"use strict";
var a = "FAIL";
class A {
static p = a;
[a = "PASS"];
}
try {
b++;
} finally {
var a, b = 42;
}
console.log(a, b);
}
expect: {
"use strict";
var a = "FAIL";
class A {
static p = a;
[a = "PASS"];
}
try {
b++;
} finally {
var a, b = 42;
}
console.log(a, b);
}
expect_stdout: "PASS 42"
node_version: ">=12"
}

0 comments on commit 2501797

Please sign in to comment.