Skip to content

Commit 0b0eac1

Browse files
authored
drop property assignment to constants (#2612)
1 parent b29fc8b commit 0b0eac1

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎lib/compress.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,11 @@ merge(Compressor.prototype, {
17081708
return this.consequent._dot_throw(compressor)
17091709
|| this.alternative._dot_throw(compressor);
17101710
})
1711+
def(AST_Dot, function(compressor) {
1712+
if (!is_strict(compressor)) return false;
1713+
if (this.expression instanceof AST_Function && this.property == "prototype") return false;
1714+
return true;
1715+
});
17111716
def(AST_Sequence, function(compressor) {
17121717
return this.tail_node()._dot_throw(compressor);
17131718
});
@@ -3184,8 +3189,14 @@ merge(Compressor.prototype, {
31843189
}
31853190
});
31863191
def(AST_Assign, function(compressor){
3187-
this.write_only = !this.left.has_side_effects(compressor);
3188-
return this;
3192+
var left = this.left;
3193+
if (left.has_side_effects(compressor)) return this;
3194+
this.write_only = true;
3195+
while (left instanceof AST_PropAccess) {
3196+
left = left.expression;
3197+
}
3198+
if (left instanceof AST_Symbol) return this;
3199+
return this.right.drop_side_effect_free(compressor);
31893200
});
31903201
def(AST_Conditional, function(compressor){
31913202
var consequent = this.consequent.drop_side_effect_free(compressor);

‎test/compress/properties.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,3 +1054,43 @@ issue_2513: {
10541054
"undefined undefined",
10551055
]
10561056
}
1057+
1058+
const_prop_assign_strict: {
1059+
options = {
1060+
pure_getters: "strict",
1061+
side_effects: true,
1062+
}
1063+
input: {
1064+
function Simulator() {
1065+
/abc/.index = 1;
1066+
this._aircraft = [];
1067+
}
1068+
(function() {}).prototype.destroy = x();
1069+
}
1070+
expect: {
1071+
function Simulator() {
1072+
this._aircraft = [];
1073+
}
1074+
x();
1075+
}
1076+
}
1077+
1078+
const_prop_assign_pure: {
1079+
options = {
1080+
pure_getters: true,
1081+
side_effects: true,
1082+
}
1083+
input: {
1084+
function Simulator() {
1085+
/abc/.index = 1;
1086+
this._aircraft = [];
1087+
}
1088+
(function() {}).prototype.destroy = x();
1089+
}
1090+
expect: {
1091+
function Simulator() {
1092+
this._aircraft = [];
1093+
}
1094+
x();
1095+
}
1096+
}

0 commit comments

Comments
 (0)