Skip to content

Commit e1e3516

Browse files
authored
fix corner case in reduce_vars (#5687)
1 parent bd5fc4c commit e1e3516

File tree

2 files changed

+234
-2
lines changed

2 files changed

+234
-2
lines changed

lib/compress.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ Compressor.prototype.compress = function(node) {
497497
function has_escaped(d, scope, node, parent) {
498498
if (parent instanceof AST_Assign) return parent.operator == "=" && parent.right === node;
499499
if (parent instanceof AST_Call) return parent.expression !== node || parent instanceof AST_New;
500+
if (parent instanceof AST_ClassField) return parent.value === node && !parent.static;
500501
if (parent instanceof AST_Exit) return parent.value === node && scope.resolve() !== d.scope.resolve();
501502
if (parent instanceof AST_VarDef) return parent.value === node;
502503
}
@@ -1164,7 +1165,11 @@ Compressor.prototype.compress = function(node) {
11641165
if (node.extends) node.extends.walk(tw);
11651166
var props = node.properties.filter(function(prop) {
11661167
reset_flags(prop);
1167-
if (prop.key instanceof AST_Node) prop.key.walk(tw);
1168+
if (prop.key instanceof AST_Node) {
1169+
tw.push(prop);
1170+
prop.key.walk(tw);
1171+
tw.pop();
1172+
}
11681173
return prop.value;
11691174
});
11701175
if (node.name) {
@@ -1184,13 +1189,15 @@ Compressor.prototype.compress = function(node) {
11841189
}
11851190
}
11861191
props.forEach(function(prop) {
1192+
tw.push(prop);
11871193
if (!prop.static || is_static_field_or_init(prop) && prop.value.contains_this()) {
11881194
push(tw);
11891195
prop.value.walk(tw);
11901196
pop(tw);
11911197
} else {
11921198
prop.value.walk(tw);
11931199
}
1200+
tw.pop();
11941201
});
11951202
return true;
11961203
});
@@ -12296,7 +12303,7 @@ Compressor.prototype.compress = function(node) {
1229612303
var single_use = def.single_use && !(parent instanceof AST_Call && parent.is_expr_pure(compressor));
1229712304
if (single_use) {
1229812305
if (is_lambda(fixed)) {
12299-
if ((def.scope !== self.scope.resolve() || def.in_loop)
12306+
if ((def.scope !== self.scope.resolve(true) || def.in_loop)
1230012307
&& (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) {
1230112308
single_use = false;
1230212309
} else if (def.redefined()) {

test/compress/classes.js

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ drop_extends: {
599599
inline: true,
600600
passes: 2,
601601
pure_getters: "strict",
602+
reduce_funcs: true,
602603
reduce_vars: true,
603604
sequences: true,
604605
side_effects: true,
@@ -921,6 +922,7 @@ single_use_3: {
921922

922923
single_use_4: {
923924
options = {
925+
reduce_funcs: true,
924926
reduce_vars: true,
925927
toplevel: true,
926928
unused: true,
@@ -1503,6 +1505,218 @@ keep_instanceof_3: {
15031505
node_version: ">=4"
15041506
}
15051507

1508+
keep_field_reference_1: {
1509+
options = {
1510+
reduce_funcs: true,
1511+
reduce_vars: true,
1512+
toplevel: true,
1513+
unused: true,
1514+
}
1515+
input: {
1516+
"use strict";
1517+
function f() {}
1518+
class A {
1519+
p = f;
1520+
}
1521+
console.log(new A().p === new A().p ? "PASS" : "FAIL");
1522+
}
1523+
expect: {
1524+
"use strict";
1525+
function f() {}
1526+
class A {
1527+
p = f;
1528+
}
1529+
console.log(new A().p === new A().p ? "PASS" : "FAIL");
1530+
}
1531+
expect_stdout: "PASS"
1532+
node_version: ">=12"
1533+
}
1534+
1535+
keep_field_reference_2: {
1536+
options = {
1537+
reduce_funcs: true,
1538+
reduce_vars: true,
1539+
toplevel: true,
1540+
unused: true,
1541+
}
1542+
input: {
1543+
"use strict";
1544+
function f() {}
1545+
var A = class {
1546+
p = f;
1547+
};
1548+
console.log(new A().p === new A().p ? "PASS" : "FAIL");
1549+
}
1550+
expect: {
1551+
"use strict";
1552+
function f() {}
1553+
var A = class {
1554+
p = f;
1555+
};
1556+
console.log(new A().p === new A().p ? "PASS" : "FAIL");
1557+
}
1558+
expect_stdout: "PASS"
1559+
node_version: ">=12"
1560+
}
1561+
1562+
keep_field_reference_3: {
1563+
options = {
1564+
reduce_funcs: true,
1565+
reduce_vars: true,
1566+
toplevel: true,
1567+
unused: true,
1568+
}
1569+
input: {
1570+
"use strict";
1571+
class A {}
1572+
class B {
1573+
p = A;
1574+
}
1575+
console.log(new B().p === new B().p ? "PASS" : "FAIL");
1576+
}
1577+
expect: {
1578+
"use strict";
1579+
class A {}
1580+
class B {
1581+
p = A;
1582+
}
1583+
console.log(new B().p === new B().p ? "PASS" : "FAIL");
1584+
}
1585+
expect_stdout: "PASS"
1586+
node_version: ">=12"
1587+
}
1588+
1589+
keep_field_reference_4: {
1590+
options = {
1591+
reduce_funcs: true,
1592+
reduce_vars: true,
1593+
toplevel: true,
1594+
unused: true,
1595+
}
1596+
input: {
1597+
"use strict";
1598+
var A = class {};
1599+
var B = class {
1600+
p = A;
1601+
};
1602+
console.log(new B().p === new B().p ? "PASS" : "FAIL");
1603+
}
1604+
expect: {
1605+
"use strict";
1606+
var A = class {};
1607+
var B = class {
1608+
p = A;
1609+
};
1610+
console.log(new B().p === new B().p ? "PASS" : "FAIL");
1611+
}
1612+
expect_stdout: "PASS"
1613+
node_version: ">=12"
1614+
}
1615+
1616+
keep_static_field_reference_1: {
1617+
options = {
1618+
reduce_funcs: true,
1619+
reduce_vars: true,
1620+
toplevel: true,
1621+
unused: true,
1622+
}
1623+
input: {
1624+
"use strict";
1625+
function f() {}
1626+
class A {
1627+
static P = f;
1628+
}
1629+
console.log(A.P === A.P ? "PASS" : "FAIL");
1630+
}
1631+
expect: {
1632+
"use strict";
1633+
class A {
1634+
static P = function() {};
1635+
}
1636+
console.log(A.P === A.P ? "PASS" : "FAIL");
1637+
}
1638+
expect_stdout: "PASS"
1639+
node_version: ">=12"
1640+
}
1641+
1642+
keep_static_field_reference_2: {
1643+
options = {
1644+
reduce_funcs: true,
1645+
reduce_vars: true,
1646+
toplevel: true,
1647+
unused: true,
1648+
}
1649+
input: {
1650+
"use strict";
1651+
function f() {}
1652+
var A = class {
1653+
static P = f;
1654+
};
1655+
console.log(A.P === A.P ? "PASS" : "FAIL");
1656+
}
1657+
expect: {
1658+
"use strict";
1659+
var A = class {
1660+
static P = function() {};
1661+
};
1662+
console.log(A.P === A.P ? "PASS" : "FAIL");
1663+
}
1664+
expect_stdout: "PASS"
1665+
node_version: ">=12"
1666+
}
1667+
1668+
keep_static_field_reference_3: {
1669+
options = {
1670+
reduce_funcs: true,
1671+
reduce_vars: true,
1672+
toplevel: true,
1673+
unused: true,
1674+
}
1675+
input: {
1676+
"use strict";
1677+
class A {}
1678+
class B {
1679+
static P = A;
1680+
}
1681+
console.log(B.P === B.P ? "PASS" : "FAIL");
1682+
}
1683+
expect: {
1684+
"use strict";
1685+
class B {
1686+
static P = class {};
1687+
}
1688+
console.log(B.P === B.P ? "PASS" : "FAIL");
1689+
}
1690+
expect_stdout: "PASS"
1691+
node_version: ">=12"
1692+
}
1693+
1694+
keep_static_field_reference_4: {
1695+
options = {
1696+
reduce_funcs: true,
1697+
reduce_vars: true,
1698+
toplevel: true,
1699+
unused: true,
1700+
}
1701+
input: {
1702+
"use strict";
1703+
var A = class {};
1704+
var B = class {
1705+
static P = A;
1706+
};
1707+
console.log(B.P === B.P ? "PASS" : "FAIL");
1708+
}
1709+
expect: {
1710+
"use strict";
1711+
var B = class {
1712+
static P = class {};
1713+
};
1714+
console.log(B.P === B.P ? "PASS" : "FAIL");
1715+
}
1716+
expect_stdout: "PASS"
1717+
node_version: ">=12"
1718+
}
1719+
15061720
issue_805_1: {
15071721
options = {
15081722
inline: true,
@@ -2313,6 +2527,7 @@ issue_4962_1: {
23132527
options = {
23142528
ie: true,
23152529
inline: true,
2530+
reduce_funcs: true,
23162531
reduce_vars: true,
23172532
unused: true,
23182533
}
@@ -2344,6 +2559,7 @@ issue_4962_1_strict: {
23442559
options = {
23452560
ie: true,
23462561
inline: true,
2562+
reduce_funcs: true,
23472563
reduce_vars: true,
23482564
unused: true,
23492565
}
@@ -2371,6 +2587,7 @@ issue_4962_1_strict_direct: {
23712587
options = {
23722588
ie: true,
23732589
inline: true,
2590+
reduce_funcs: true,
23742591
reduce_vars: true,
23752592
unused: true,
23762593
}
@@ -2402,6 +2619,7 @@ issue_4962_2: {
24022619
options = {
24032620
ie: true,
24042621
inline: true,
2622+
reduce_funcs: true,
24052623
reduce_vars: true,
24062624
unused: true,
24072625
}
@@ -2433,6 +2651,7 @@ issue_4962_2_strict: {
24332651
options = {
24342652
ie: true,
24352653
inline: true,
2654+
reduce_funcs: true,
24362655
reduce_vars: true,
24372656
unused: true,
24382657
}
@@ -2461,6 +2680,7 @@ issue_4962_2_strict_direct: {
24612680
options = {
24622681
ie: true,
24632682
inline: true,
2683+
reduce_funcs: true,
24642684
reduce_vars: true,
24652685
unused: true,
24662686
}
@@ -2495,6 +2715,7 @@ issue_4962_2_strict_direct_inline: {
24952715
ie: true,
24962716
inline: true,
24972717
passes: 2,
2718+
reduce_funcs: true,
24982719
reduce_vars: true,
24992720
unused: true,
25002721
}
@@ -2878,6 +3099,7 @@ issue_5053_4: {
28783099
issue_5082_1: {
28793100
options = {
28803101
inline: true,
3102+
reduce_funcs: true,
28813103
reduce_vars: true,
28823104
unused: true,
28833105
}
@@ -2910,6 +3132,7 @@ issue_5082_1: {
29103132
issue_5082_1_strict: {
29113133
options = {
29123134
inline: true,
3135+
reduce_funcs: true,
29133136
reduce_vars: true,
29143137
unused: true,
29153138
}
@@ -2943,6 +3166,7 @@ issue_5082_2: {
29433166
options = {
29443167
inline: true,
29453168
passes: 2,
3169+
reduce_funcs: true,
29463170
reduce_vars: true,
29473171
unused: true,
29483172
}
@@ -2976,6 +3200,7 @@ issue_5082_2_static: {
29763200
options = {
29773201
inline: true,
29783202
passes: 2,
3203+
reduce_funcs: true,
29793204
reduce_vars: true,
29803205
unused: true,
29813206
}

0 commit comments

Comments
 (0)