&& (comments = this.start.comments_before)
&& comments.length
&& /[@#]__PURE__/.test((last_comment = comments[comments.length - 1]).value)) {
- compressor.warn("Dropping __PURE__ call [{file}:{line},{col}]", this.start);
- last_comment.value = last_comment.value.replace(/[@#]__PURE__/g, ' ');
- pure = true;
+ pure = last_comment;
}
return this.pure = pure;
});
line : def.name.start.line,
col : def.name.start.col
};
- if (def.value && def.value.has_side_effects(compressor)) {
- def._unused_side_effects = true;
+ if (def.value && (def._unused_side_effects = def.value.drop_side_effect_free(compressor))) {
compressor.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", w);
return true;
}
for (var i = 0; i < def.length;) {
var x = def[i];
if (x._unused_side_effects) {
- side_effects.push(x.value);
+ side_effects.push(x._unused_side_effects);
def.splice(i, 1);
} else {
if (side_effects.length > 0) {
def(AST_This, return_null);
def(AST_Call, function(compressor, first_in_statement){
if (!this.has_pure_annotation(compressor) && compressor.pure_funcs(this)) return this;
+ if (this.pure) {
+ compressor.warn("Dropping __PURE__ call [{file}:{line},{col}]", this.start);
+ this.pure.value = this.pure.value.replace(/[@#]__PURE__/g, ' ');
+ }
var args = trim(this.args, compressor, first_in_statement);
return args && AST_Seq.from_array(args);
});
case "typeof":
if (this.expression instanceof AST_SymbolRef) return null;
default:
- if (first_in_statement && is_iife_call(this.expression)) return this;
- return this.expression.drop_side_effect_free(compressor, first_in_statement);
+ var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
+ if (first_in_statement
+ && this instanceof AST_UnaryPrefix
+ && is_iife_call(expression)) {
+ if (expression === this.expression && this.operator.length === 1) return this;
+ return make_node(AST_UnaryPrefix, this, {
+ operator: this.operator.length === 1 ? this.operator : "!",
+ expression: expression
+ });
+ }
+ return expression;
}
});
def(AST_SymbolRef, function() {
OPT(AST_SimpleStatement, function(self, compressor){
if (compressor.option("side_effects")) {
var body = self.body;
- if (!body.has_side_effects(compressor)) {
- compressor.warn("Dropping side-effect-free statement [{file}:{line},{col}]", self.start);
- return make_node(AST_EmptyStatement, self);
- }
var node = body.drop_side_effect_free(compressor, true);
if (!node) {
compressor.warn("Dropping side-effect-free statement [{file}:{line},{col}]", self.start);
}
}
if (!self.car.has_side_effects(compressor)
- && !self.cdr.has_side_effects(compressor)
&& self.car.equivalent_to(self.cdr)) {
return self.car;
}
}
- if (self.cdr instanceof AST_UnaryPrefix
- && self.cdr.operator == "void"
- && !self.cdr.expression.has_side_effects(compressor)) {
- self.cdr.expression = self.car;
- return self.cdr;
- }
if (self.cdr instanceof AST_Undefined) {
return make_node(AST_UnaryPrefix, self, {
operator : "void",
});
OPT(AST_UnaryPrefix, function(self, compressor){
- self = self.lift_sequences(compressor);
+ var seq = self.lift_sequences(compressor);
+ if (seq !== self) {
+ return seq;
+ }
var e = self.expression;
+ if (compressor.option("side_effects") && self.operator == "void") {
+ e = e.drop_side_effect_free(compressor);
+ if (e) {
+ self.expression = e;
+ return self;
+ } else {
+ return make_node(AST_Undefined, self);
+ }
+ }
if (compressor.option("booleans") && compressor.in_boolean_context()) {
switch (self.operator) {
case "!":
// typeof always returns a non-empty string, thus it's
// always true in booleans
compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
- if (self.expression.has_side_effects(compressor)) {
- return make_node(AST_Seq, self, {
- car: self.expression,
- cdr: make_node(AST_True, self)
- });
- }
- return make_node(AST_True, self);
+ return make_node(AST_Seq, self, {
+ car: e,
+ cdr: make_node(AST_True, self)
+ }).optimize(compressor);
}
}
return self.evaluate(compressor)[0];
var rr = self.right.evaluate(compressor);
if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) {
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
- if (self.left.has_side_effects(compressor)) {
- return make_node(AST_Seq, self, {
- car: self.left,
- cdr: make_node(AST_False)
- }).optimize(compressor);
- }
- return make_node(AST_False, self);
+ return make_node(AST_Seq, self, {
+ car: self.left,
+ cdr: make_node(AST_False)
+ }).optimize(compressor);
}
if (ll.length > 1 && ll[1]) {
return rr[0];
var rr = self.right.evaluate(compressor);
if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) {
compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
- if (self.left.has_side_effects(compressor)) {
- return make_node(AST_Seq, self, {
- car: self.left,
- cdr: make_node(AST_True)
- }).optimize(compressor);
- }
- return make_node(AST_True, self);
+ return make_node(AST_Seq, self, {
+ car: self.left,
+ cdr: make_node(AST_True, self)
+ }).optimize(compressor);
}
if (ll.length > 1 && !ll[1]) {
return rr[0];
case "+":
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
- if ((ll.length > 1 && ll[0] instanceof AST_String && ll[1] && !self.right.has_side_effects(compressor)) ||
- (rr.length > 1 && rr[0] instanceof AST_String && rr[1] && !self.left.has_side_effects(compressor))) {
+ if (ll.length > 1 && ll[0] instanceof AST_String && ll[1]) {
+ compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
+ return make_node(AST_Seq, self, {
+ car: self.right,
+ cdr: make_node(AST_True, self)
+ }).optimize(compressor);
+ }
+ if (rr.length > 1 && rr[0] instanceof AST_String && rr[1]) {
compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
- return make_node(AST_True, self);
+ return make_node(AST_Seq, self, {
+ car: self.left,
+ cdr: make_node(AST_True, self)
+ }).optimize(compressor);
}
break;
}
&& alternative instanceof AST_Assign
&& consequent.operator == alternative.operator
&& consequent.left.equivalent_to(alternative.left)
- && !consequent.left.has_side_effects(compressor)
+ && (!consequent.left.has_side_effects(compressor)
+ || !self.condition.has_side_effects(compressor))
) {
/*
* Stuff like this:
})
});
}
+ // x ? y(a) : y(b) --> y(x ? a : b)
if (consequent instanceof AST_Call
&& alternative.TYPE === consequent.TYPE
- && consequent.args.length == alternative.args.length
- && !consequent.expression.has_side_effects(compressor)
- && consequent.expression.equivalent_to(alternative.expression)) {
- if (consequent.args.length == 0) {
- return make_node(AST_Seq, self, {
- car: self.condition,
- cdr: consequent
- });
- }
- if (consequent.args.length == 1) {
- consequent.args[0] = make_node(AST_Conditional, self, {
- condition: self.condition,
- consequent: consequent.args[0],
- alternative: alternative.args[0]
- });
- return consequent;
- }
+ && consequent.args.length == 1
+ && alternative.args.length == 1
+ && consequent.expression.equivalent_to(alternative.expression)
+ && !consequent.expression.has_side_effects(compressor)) {
+ consequent.args[0] = make_node(AST_Conditional, self, {
+ condition: self.condition,
+ consequent: consequent.args[0],
+ alternative: alternative.args[0]
+ });
+ return consequent;
}
// x?y?z:a:a --> x&&y?z:a
if (consequent instanceof AST_Conditional
alternative: alternative
});
}
- // y?1:1 --> 1
- if (consequent.is_constant()
- && alternative.is_constant()
- && consequent.equivalent_to(alternative)) {
- var consequent_value = consequent.evaluate(compressor)[0];
- if (self.condition.has_side_effects(compressor)) {
- return AST_Seq.from_array([self.condition, consequent_value]);
- } else {
- return consequent_value;
- }
+ // x ? y : y --> x, y
+ if (consequent.equivalent_to(alternative)) {
+ return make_node(AST_Seq, self, {
+ car: self.condition,
+ cdr: consequent
+ }).optimize(compressor);
}
if (is_true(self.consequent)) {
});
function literals_in_boolean_context(self, compressor) {
- if (compressor.option("booleans") && compressor.in_boolean_context() && !self.has_side_effects(compressor)) {
- return make_node(AST_True, self);
+ if (compressor.option("booleans") && compressor.in_boolean_context()) {
+ var best = first_in_statement(compressor) ? best_of_statement : best_of;
+ return best(self, make_node(AST_Seq, self, {
+ car: self,
+ cdr: make_node(AST_True, self)
+ }).optimize(compressor));
}
return self;
};
"WARN: Dropping unused variable iife1 [test/compress/issue-1261.js:84,12]",
]
}
+
+should_warn: {
+ options = {
+ booleans: true,
+ conditionals: true,
+ evaluate: true,
+ side_effects: true,
+ }
+ input: {
+ /* @__PURE__ */(function(){x})(), void/* @__PURE__ */(function(){y})();
+ /* @__PURE__ */(function(){x})() || true ? foo() : bar();
+ true || /* @__PURE__ */(function(){y})() ? foo() : bar();
+ /* @__PURE__ */(function(){x})() && false ? foo() : bar();
+ false && /* @__PURE__ */(function(){y})() ? foo() : bar();
+ /* @__PURE__ */(function(){x})() + "foo" ? bar() : baz();
+ "foo" + /* @__PURE__ */(function(){y})() ? bar() : baz();
+ /* @__PURE__ */(function(){x})() ? foo() : foo();
+ [/* @__PURE__ */(function(){x})()] ? foo() : bar();
+ !{ foo: /* @__PURE__ */(function(){x})() } ? bar() : baz();
+ }
+ expect: {
+ foo();
+ foo();
+ bar();
+ bar();
+ bar();
+ bar();
+ foo();
+ foo();
+ baz();
+ }
+ expect_warnings: [
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:128,61]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:128,23]",
+ "WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:128,23]",
+ "WARN: Boolean || always true [test/compress/issue-1261.js:129,23]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:129,23]",
+ "WARN: Condition always true [test/compress/issue-1261.js:129,23]",
+ "WARN: Boolean || always true [test/compress/issue-1261.js:130,8]",
+ "WARN: Condition always true [test/compress/issue-1261.js:130,8]",
+ "WARN: Boolean && always false [test/compress/issue-1261.js:131,23]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:131,23]",
+ "WARN: Condition always false [test/compress/issue-1261.js:131,23]",
+ "WARN: Boolean && always false [test/compress/issue-1261.js:132,8]",
+ "WARN: Condition always false [test/compress/issue-1261.js:132,8]",
+ "WARN: + in boolean context always true [test/compress/issue-1261.js:133,23]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:133,23]",
+ "WARN: Condition always true [test/compress/issue-1261.js:133,23]",
+ "WARN: + in boolean context always true [test/compress/issue-1261.js:134,8]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:134,31]",
+ "WARN: Condition always true [test/compress/issue-1261.js:134,8]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:135,23]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:136,24]",
+ "WARN: Condition always true [test/compress/issue-1261.js:136,8]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:137,31]",
+ "WARN: Condition always false [test/compress/issue-1261.js:137,8]",
+ ]
+}