Compare commits

...

5 Commits

Author SHA1 Message Date
Alex Lam S.L
4eb4cb656c v3.3.9 2018-01-27 12:56:34 +00:00
Alex Lam S.L
193612ac67 fix accounting after conversion to assignment (#2847)
Missing reference to `AST_SymbolRef` created by `unused` causes `collapse_vars` to misbehave.

fixes #2846
2018-01-26 14:21:11 +08:00
Alex Lam S.L
95cfce68ea backport of #2835 (#2841) 2018-01-23 05:45:45 +08:00
Alex Lam S.L
ec4202590d drop assignments to constant expressions only (#2839)
fixes #2838
2018-01-23 02:49:54 +08:00
Alex Lam S.L
5e2cd07d6f handle duplicate function declarations correctly (#2837)
fixes #2836
2018-01-23 01:28:09 +08:00
6 changed files with 87 additions and 6 deletions

View File

@@ -1642,7 +1642,7 @@ merge(Compressor.prototype, {
var stat = null; var stat = null;
for (var i = 0, len = block.body.length; i < len; i++) { for (var i = 0, len = block.body.length; i < len; i++) {
var line = block.body[i]; var line = block.body[i];
if (line instanceof AST_Definitions && declarations_only(line)) { if (line instanceof AST_Var && declarations_only(line)) {
decls.push(line); decls.push(line);
} else if (stat) { } else if (stat) {
return false; return false;
@@ -3038,9 +3038,11 @@ merge(Compressor.prototype, {
if (var_defs.length > 1 && (!def.value || sym.orig.indexOf(def.name) > sym.eliminated)) { if (var_defs.length > 1 && (!def.value || sym.orig.indexOf(def.name) > sym.eliminated)) {
compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
if (def.value) { if (def.value) {
var ref = make_node(AST_SymbolRef, def.name, def.name);
sym.references.push(ref);
var assign = make_node(AST_Assign, def, { var assign = make_node(AST_Assign, def, {
operator: "=", operator: "=",
left: make_node(AST_SymbolRef, def.name, def.name), left: ref,
right: def.value right: def.value
}); });
if (fixed_ids[sym.id] === def) { if (fixed_ids[sym.id] === def) {
@@ -3469,8 +3471,10 @@ merge(Compressor.prototype, {
while (left instanceof AST_PropAccess) { while (left instanceof AST_PropAccess) {
left = left.expression; left = left.expression;
} }
if (left instanceof AST_Symbol) return this; if (left.is_constant_expression(compressor.find_parent(AST_Scope))) {
return this.right.drop_side_effect_free(compressor); return this.right.drop_side_effect_free(compressor);
}
return this;
}); });
def(AST_Conditional, function(compressor){ def(AST_Conditional, function(compressor){
var consequent = this.consequent.drop_side_effect_free(compressor); var consequent = this.consequent.drop_side_effect_free(compressor);

View File

@@ -305,7 +305,7 @@ AST_Scope.DEFMETHOD("find_variable", function(name){
AST_Scope.DEFMETHOD("def_function", function(symbol, init){ AST_Scope.DEFMETHOD("def_function", function(symbol, init){
var def = this.def_variable(symbol, init); var def = this.def_variable(symbol, init);
if (!def.init) def.init = init; if (!def.init || def.init instanceof AST_Defun) def.init = init;
this.functions.set(symbol.name, def); this.functions.set(symbol.name, def);
return def; return def;
}); });

View File

@@ -4,7 +4,7 @@
"homepage": "http://lisperator.net/uglifyjs", "homepage": "http://lisperator.net/uglifyjs",
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)", "author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"version": "3.3.8", "version": "3.3.9",
"engines": { "engines": {
"node": ">=0.8.0" "node": ">=0.8.0"
}, },

View File

@@ -1692,3 +1692,30 @@ issue_2768: {
} }
expect_stdout: "PASS undefined" expect_stdout: "PASS undefined"
} }
issue_2846: {
options = {
collapse_vars: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function f(a, b) {
var a = 0;
b && b(a);
return a++;
}
var c = f();
console.log(c);
}
expect: {
var c = function(a, b) {
a = 0;
b && b(a);
return a++;
}();
console.log(c);
}
expect_stdout: "0"
}

View File

@@ -694,3 +694,30 @@ issue_2678: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
issue_2838: {
options = {
pure_getters: true,
side_effects: true,
}
input: {
function f(a, b) {
(a || b).c = "PASS";
(function() {
return f(a, b);
}).prototype.foo = "bar";
}
var o = {};
f(null, o);
console.log(o.c);
}
expect: {
function f(a, b) {
(a || b).c = "PASS";
}
var o = {};
f(null, o);
console.log(o.c);
}
expect_stdout: "PASS"
}

View File

@@ -5360,3 +5360,26 @@ issue_2799_2: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
issue_2836: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function f() {
return "FAIL";
}
console.log(f());
function f() {
return "PASS";
}
}
expect: {
console.log(function() {
return "PASS";
}());
}
expect_stdout: "PASS"
}