@@ -914,12 +914,12 @@ merge(Compressor.prototype, {
|
||||
continue loop;
|
||||
case stat instanceof AST_If:
|
||||
if (stat.body instanceof AST_Return) {
|
||||
var value = stat.body.value;
|
||||
//---
|
||||
// pretty silly case, but:
|
||||
// if (foo()) return; return; ==> foo(); return;
|
||||
if (((in_lambda && ret.length == 0)
|
||||
|| (ret[0] instanceof AST_Return && !ret[0].value))
|
||||
&& !stat.body.value && !stat.alternative) {
|
||||
if ((in_lambda && ret.length == 0 || ret[0] instanceof AST_Return && !ret[0].value)
|
||||
&& !value && !stat.alternative) {
|
||||
CHANGED = true;
|
||||
var cond = make_node(AST_SimpleStatement, stat.condition, {
|
||||
body: stat.condition
|
||||
@@ -929,7 +929,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
//---
|
||||
// if (foo()) return x; return y; ==> return foo() ? x : y;
|
||||
if (ret[0] instanceof AST_Return && stat.body.value && ret[0].value && !stat.alternative) {
|
||||
if (ret[0] instanceof AST_Return && value && ret[0].value && !stat.alternative) {
|
||||
CHANGED = true;
|
||||
stat = stat.clone();
|
||||
stat.alternative = ret[0];
|
||||
@@ -939,7 +939,7 @@ merge(Compressor.prototype, {
|
||||
//---
|
||||
// if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined;
|
||||
if (multiple_if_returns && (ret.length == 0 || ret[0] instanceof AST_Return)
|
||||
&& stat.body.value && !stat.alternative && in_lambda) {
|
||||
&& value && !stat.alternative && in_lambda) {
|
||||
CHANGED = true;
|
||||
stat = stat.clone();
|
||||
stat.alternative = ret[0] || make_node(AST_Return, stat, {
|
||||
@@ -949,8 +949,8 @@ merge(Compressor.prototype, {
|
||||
continue loop;
|
||||
}
|
||||
//---
|
||||
// if (foo()) return; [ else x... ]; y... ==> if (!foo()) { x...; y... }
|
||||
if (!stat.body.value && in_lambda) {
|
||||
// if (foo()) return [ void bar() ]; [ else x...; ] y... ==> if (!foo()) { x...; y... } else bar();
|
||||
if (in_lambda && (!value || value instanceof AST_UnaryPrefix && value.operator == "void")) {
|
||||
CHANGED = true;
|
||||
stat = stat.clone();
|
||||
stat.condition = stat.condition.negate(compressor);
|
||||
@@ -959,11 +959,12 @@ merge(Compressor.prototype, {
|
||||
stat.body = make_node(AST_BlockStatement, stat, {
|
||||
body: body
|
||||
});
|
||||
stat.alternative = null;
|
||||
stat.alternative = value ? make_node(AST_SimpleStatement, value, {
|
||||
body: value.expression
|
||||
}) : null;
|
||||
ret = funs.concat([ stat.transform(compressor) ]);
|
||||
continue loop;
|
||||
}
|
||||
|
||||
//---
|
||||
// if (a) return b; if (c) return d; e; ==> return a ? b : c ? d : void e;
|
||||
//
|
||||
|
||||
@@ -302,3 +302,25 @@ issue_1437_conditionals: {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
issue_512: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
if_return: true,
|
||||
}
|
||||
input: {
|
||||
function a() {
|
||||
if (b()) {
|
||||
c();
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function a() {
|
||||
if (!b()) throw e;
|
||||
c();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,9 @@ describe("bin/uglifyjs", function () {
|
||||
eval(stdout);
|
||||
|
||||
assert.strictEqual(typeof WrappedUglifyJS, 'object');
|
||||
assert.strictEqual(WrappedUglifyJS.minify("foo([true,,2+3]);").code, "foo([!0,,5]);");
|
||||
var result = WrappedUglifyJS.minify("foo([true,,2+3]);");
|
||||
assert.strictEqual(result.error, undefined);
|
||||
assert.strictEqual(result.code, "foo([!0,,5]);");
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
@@ -15,7 +15,9 @@ describe("spidermonkey export/import sanity test", function() {
|
||||
|
||||
eval(stdout);
|
||||
assert.strictEqual(typeof SpiderUglify, "object");
|
||||
assert.strictEqual(SpiderUglify.minify("foo([true,,2+3]);").code, "foo([!0,,5]);");
|
||||
var result = SpiderUglify.minify("foo([true,,2+3]);");
|
||||
assert.strictEqual(result.error, undefined);
|
||||
assert.strictEqual(result.code, "foo([!0,,5]);");
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user