function ref_once(tw, compressor, def) {
return compressor.option("unused")
&& !def.scope.pinned()
- && def.references.length - def.recursive_refs == 1
- && tw.loop_ids[def.id] === tw.in_loop;
+ && def.references.length - def.recursive_refs == 1;
}
function is_immutable(value) {
if (recursive) {
d.recursive_refs++;
} else if (value && ref_once(tw, compressor, d)) {
+ d.in_loop = tw.loop_ids[d.id] !== tw.in_loop;
d.single_use = value instanceof AST_Lambda && !value.pinned()
- || d.scope === this.scope && value.is_constant_expression();
+ || !d.in_loop && d.scope === this.scope && value.is_constant_expression();
} else {
d.single_use = false;
}
if (fn.name && fn.name.definition().recursive_refs > 0) return this;
if (this.is_expr_pure(compressor)) return this;
var stat = fn.first_statement();
- if (!(stat instanceof AST_Return)) return this;
+ if (!(stat instanceof AST_Return)) {
+ if (ignore_side_effects) {
+ var found = false;
+ fn.walk(new TreeWalker(function(node) {
+ if (found) return true;
+ if (node instanceof AST_Return) {
+ if (node.value && node.value.evaluate(compressor, true) !== undefined) found = true;
+ return true;
+ }
+ if (node instanceof AST_Scope && node !== fn) return true;
+ }));
+ if (!found) return void 0;
+ }
+ return this;
+ }
var args = eval_args(this.args);
if (!args) return this;
if (!stat.value) return undefined;
var single_use = def.single_use && !(parent instanceof AST_Call && parent.is_expr_pure(compressor));
if (single_use) {
if (fixed instanceof AST_Lambda) {
- if (def.scope !== self.scope
+ if ((def.scope !== self.scope || def.in_loop)
&& (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) {
single_use = false;
} else if (recursive_ref(compressor, def)) {
expressions.push(self);
return make_sequence(self, expressions);
}
- var condition = self.condition.is_truthy() || self.condition.evaluate(compressor);
+ var condition = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
if (!condition) {
AST_Node.warn("Condition always false [{file}:{line},{col}]", self.start);
return make_sequence(self, [ self.condition, self.alternative ]).optimize(compressor);
while (x);
}
expect: {
- function bar() {
- console.log("bar:", --x);
- }
var x = 3;
- for (;bar(), x;);
+ for (;function() {
+ console.log("bar:", --x);
+ }(), x;);
}
expect_stdout: true
}
while (x);
}
expect: {
- function bar() {
+ for (;;) (function() {
console.log("bar:");
- }
- for (;;) bar();
+ })();
}
}
}
expect: {
var a;
- function f(b) {
- console.log(a === b);
- a = b;
- }
function g() {}
for (var i = 3; --i >= 0;)
- f(g);
+ (function(b) {
+ console.log(a === b);
+ a = b;
+ })(g);
}
expect_stdout: [
"false",
console.log(sum);
}
expect: {
- function indirect_foo(x, y, z) {
- return function(x, y, z) {
- return x < y ? x * y + z : x * z - y;
- }(x, y, z);
- }
var sum = 0;
for (var i = 0; i < 100; ++i)
- sum += indirect_foo(i, i + 1, 3 * i);
+ sum += function(x, y, z) {
+ return function(x, y, z) {
+ return x < y ? x * y + z : x * z - y;
+ }(x, y, z);
+ }(i, i + 1, 3 * i);
console.log(sum);
}
expect_stdout: "348150"
console.log(sum);
}
expect: {
- var indirect_foo = function(x, y, z) {
- return function(x, y, z) {
- return x < y ? x * y + z : x * z - y;
- }(x, y, z);
- }
var sum = 0;
for (var i = 0; i < 100; ++i)
- sum += indirect_foo(i, i + 1, 3 * i);
+ sum += function(x, y, z) {
+ return function(x, y, z) {
+ return x < y ? x * y + z : x * z - y;
+ }(x, y, z);
+ }(i, i + 1, 3 * i);
console.log(sum);
}
expect_stdout: "348150"
console.log(sum);
}
expect: {
- function indirect_foo(x, y, z) {
- return function(x, y, z) {
- return x < y ? x * y + z : x * z - y;
- }(x, y, z);
- }
var sum = 0;
for (var i = 0; i < 100; ++i)
- sum += indirect_foo(i, i + 1, 3 * i);
+ sum += function(x, y, z) {
+ return function(x, y, z) {
+ return x < y ? x * y + z : x * z - y;
+ }(x, y, z);
+ }(i, i + 1, 3 * i);
console.log(sum);
}
expect_stdout: "348150"
console.log(sum);
}
expect: {
- var indirect_foo = function(x, y, z) {
- return function(x, y, z) {
- return x < y ? x * y + z : x * z - y;
- }(x, y, z);
- }
var sum = 0;
for (var i = 0; i < 100; ++i)
- sum += indirect_foo(i, i + 1, 3 * i);
+ sum += function(x, y, z) {
+ return function(x, y, z) {
+ return x < y ? x * y + z : x * z - y;
+ }(x, y, z);
+ }(i, i + 1, 3 * i);
console.log(sum);
}
expect_stdout: "348150"
}
expect_stdout: "PASS"
}
-
-void_side_effects: {
- options = {
- evaluate: true,
- reduce_vars: true,
- toplevel: true,
- unused: true,
- }
- input: {
- var a = void console.log("PASS");
- console.log(a);
- }
- expect: {
- console.log("PASS");
- console.log(void 0);
- }
- expect_stdout: [
- "PASS",
- "undefined",
- ]
-}