Skip to content

Commit

Permalink
Merge pull request #1541 from alexlamsl/harmony-v2.8.5
Browse files Browse the repository at this point in the history
Merging from master for 2.8.5
  • Loading branch information
alexlamsl authored Mar 2, 2017
2 parents e27dab7 + f704e9b commit c8e6144
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 51 deletions.
106 changes: 75 additions & 31 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ merge(Compressor.prototype, {
var reduce_vars = rescan && compressor.option("reduce_vars");
var safe_ids = [];
push();
var suppressor = new TreeWalker(function(node) {
if (node instanceof AST_Symbol) {
var d = node.definition();
if (node instanceof AST_SymbolRef) d.references.push(node);
d.fixed = false;
}
});
var tw = new TreeWalker(function(node){
if (!(node instanceof AST_Directive || node instanceof AST_Constant)) {
node._squeezed = false;
Expand All @@ -198,12 +205,16 @@ merge(Compressor.prototype, {
}
}
if (node instanceof AST_VarDef) {
var d = node.name.definition();
if (d.fixed === undefined) {
d.fixed = node.value || make_node(AST_Undefined, node);
mark_as_safe(d);
if (node.name instanceof AST_Destructuring) {
node.name.walk(suppressor);
} else {
d.fixed = false;
var d = node.name.definition();
if (d.fixed === undefined) {
d.fixed = node.value || make_node(AST_Undefined, node);
mark_as_safe(d);
} else {
d.fixed = false;
}
}
}
var iife;
Expand Down Expand Up @@ -244,9 +255,7 @@ merge(Compressor.prototype, {
return true;
}
if (node instanceof AST_ForIn) {
if (node.init instanceof AST_SymbolRef) {
node.init.definition().fixed = false;
}
node.init.walk(suppressor);
node.object.walk(tw);
push();
node.body.walk(tw);
Expand Down Expand Up @@ -485,8 +494,12 @@ merge(Compressor.prototype, {
// Constant single use vars can be replaced in any scope.
if (var_decl.value.is_constant()) {
var ctt = new TreeTransformer(function(node) {
if (node === ref)
return replace_var(node, ctt.parent(), true);
if (node === ref) {
var parent = ctt.parent();
if (!(parent instanceof AST_ForIn && parent.init === node)) {
return replace_var(node, parent, true);
}
}
});
stat.transform(ctt);
continue;
Expand Down Expand Up @@ -575,7 +588,7 @@ merge(Compressor.prototype, {
// Further optimize statement after substitution.
stat.reset_opt_flags(compressor);

compressor.warn("Replacing " + (is_constant ? "constant" : "variable") +
compressor.warn("Collapsing " + (is_constant ? "constant" : "variable") +
" " + var_name + " [{file}:{line},{col}]", node.start);
CHANGED = true;
return value;
Expand Down Expand Up @@ -1074,12 +1087,6 @@ merge(Compressor.prototype, {
def(AST_Conditional, function(compressor){
return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
});
def(AST_Call, function(compressor){
return compressor.option("unsafe")
&& this.expression instanceof AST_SymbolRef
&& this.expression.name == "String"
&& this.expression.undeclared();
});
})(function(node, func){
node.DEFMETHOD("is_string", func);
});
Expand Down Expand Up @@ -1899,16 +1906,18 @@ merge(Compressor.prototype, {
}
return node;
}
if (assign_as_unused
&& node instanceof AST_Assign
&& node.operator == "="
&& node.left instanceof AST_SymbolRef) {
var def = node.left.definition();
if ((drop_vars || !def.global)
&& !(def.id in in_use_ids)
&& self.variables.get(def.name) === def) {
return node.right;
if (assign_as_unused) {
var n = node;
while (n instanceof AST_Assign
&& n.operator == "="
&& n.left instanceof AST_SymbolRef) {
var def = n.left.definition();
if (def.id in in_use_ids
|| !drop_vars && def.global
|| self.variables.get(def.name) !== def) break;
n = n.right;
}
if (n !== node) return n;
}
if (node instanceof AST_For) {
descend(node, this);
Expand Down Expand Up @@ -2251,7 +2260,7 @@ merge(Compressor.prototype, {
}
} else {
// self instanceof AST_Do
return self.body;
return self;
}
}
if (self instanceof AST_While) {
Expand Down Expand Up @@ -2614,6 +2623,20 @@ merge(Compressor.prototype, {
});

OPT(AST_Call, function(self, compressor){
if (compressor.option("unused")
&& self.expression instanceof AST_Function
&& !self.expression.uses_arguments
&& !self.expression.uses_eval
&& self.args.length > self.expression.argnames.length) {
var end = self.expression.argnames.length;
for (var i = end, len = self.args.length; i < len; i++) {
var node = self.args[i].drop_side_effect_free(compressor);
if (node) {
self.args[end++] = node;
}
}
self.args.length = end;
}
if (compressor.option("unsafe")) {
var exp = self.expression;
if (exp instanceof AST_SymbolRef && exp.undeclared()) {
Expand Down Expand Up @@ -2815,6 +2838,12 @@ merge(Compressor.prototype, {
}
}
}
if (self.args.length == 0
&& self.expression instanceof AST_Function
&& self.expression.body[0] instanceof AST_Return
&& self.expression.body[0].value.is_constant()) {
return self.expression.body[0].value;
}
if (compressor.option("negate_iife")
&& compressor.parent() instanceof AST_SimpleStatement
&& is_iife_call(self)) {
Expand Down Expand Up @@ -3118,10 +3147,25 @@ merge(Compressor.prototype, {
}
}
}
if (self.operator == "+" && self.right instanceof AST_String
&& self.right.getValue() === "" && self.left instanceof AST_Binary
&& self.left.operator == "+" && self.left.is_string(compressor)) {
return self.left;
if (self.operator == "+") {
if (self.right instanceof AST_String
&& self.right.getValue() == ""
&& self.left.is_string(compressor)) {
return self.left;
}
if (self.left instanceof AST_String
&& self.left.getValue() == ""
&& self.right.is_string(compressor)) {
return self.right;
}
if (self.left instanceof AST_Binary
&& self.left.operator == "+"
&& self.left.left instanceof AST_String
&& self.left.left.getValue() == ""
&& self.right.is_string(compressor)) {
self.left = self.left.right;
return self.transform(compressor);
}
}
if (compressor.option("evaluate")) {
switch (self.operator) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"homepage": "http://lisperator.net/uglifyjs",
"author": "Mihai Bazon <[email protected]> (http://lisperator.net/)",
"license": "BSD-2-Clause",
"version": "2.8.4",
"version": "2.8.5",
"engines": {
"node": ">=0.8.0"
},
Expand Down
30 changes: 29 additions & 1 deletion test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ collapse_vars_constants: {
function f3(x) {
var b = x.prop;
sideeffect1();
return b + (function() { return -9; })();
return b + -9;
}
}
}
Expand Down Expand Up @@ -1315,3 +1315,31 @@ collapse_vars_regexp: {
})();
}
}

issue_1537: {
options = {
collapse_vars: true,
}
input: {
var k = '';
for (k in {prop: 'val'}){}
}
expect: {
var k = '';
for (k in {prop: 'val'});
}
}

issue_1537_for_of: {
options = {
collapse_vars: true,
}
input: {
var k = '';
for (k of {prop: 'val'}){}
}
expect: {
var k = '';
for (k of {prop: 'val'});
}
}
50 changes: 50 additions & 0 deletions test/compress/concat-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,53 @@ concat_6: {
);
}
}

concat_7: {
input: {
console.log(
"" + 1,
"" + "1",
"" + 1 + 2,
"" + 1 + "2",
"" + "1" + 2,
"" + "1" + "2",
"" + (x += "foo")
);
}
expect: {
console.log(
"" + 1,
"1",
"" + 1 + 2,
1 + "2",
"1" + 2,
"1" + "2",
x += "foo"
);
}
}

concat_8: {
input: {
console.log(
1 + "",
"1" + "",
1 + 2 + "",
1 + "2" + "",
"1" + 2 + "",
"1" + "2" + "",
(x += "foo") + ""
);
}
expect: {
console.log(
1 + "",
"1",
1 + 2 + "",
1 + "2",
"1" + 2,
"1" + "2",
x += "foo"
);
}
}
42 changes: 42 additions & 0 deletions test/compress/destructuring.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,45 @@ destructuring_dont_evaluate_with_undefined_as_default_assignment: {
[foo = void 0] = bar;
}
}

reduce_vars: {
options = {
reduce_vars: true,
}
input: {
{const [aa, [bb, cc]] = dd;}
{let [aa, [bb, cc]] = dd;}
var [aa, [bb, cc]] = dd;
[aa, [bb, cc]] = dd;
{const {aa, bb: {cc, dd}} = {aa:1, bb: {cc:2, dd: 3}};}
{let {aa, bb: {cc, dd}} = {aa:1, bb: {cc:2, dd: 3}};}
var {aa, bb: {cc, dd}} = {aa:1, bb: {cc:2, dd: 3}};
({aa, bb: {cc, dd}} = {aa:1, bb: {cc:2, dd: 3}});
const [{a},b] = c;
let [{a},b] = c;
var [{a},b] = c;
[{a},b] = c;
for (const [x,y] in pairs);
for (let [x,y] in pairs);
for (var [x,y] in pairs);
for ([x,y] in pairs);
}
expect: {
{const [aa, [bb, cc]] = dd;}
{let [aa, [bb, cc]] = dd;}
var [aa, [bb, cc]] = dd;
[aa, [bb, cc]] = dd;
{const {aa, bb: {cc, dd}} = {aa:1, bb: {cc:2, dd: 3}};}
{let {aa, bb: {cc, dd}} = {aa:1, bb: {cc:2, dd: 3}};}
var {aa, bb: {cc, dd}} = {aa:1, bb: {cc:2, dd: 3}};
({aa, bb: {cc, dd}} = {aa:1, bb: {cc:2, dd: 3}});
const [{a},b] = c;
let [{a},b] = c;
var [{a},b] = c;
[{a},b] = c;
for (const [x,y] in pairs);
for (let [x,y] in pairs);
for (var [x,y] in pairs);
for ([x,y] in pairs);
}
}
21 changes: 21 additions & 0 deletions test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,24 @@ const_assign: {
}
}
}

issue_1539: {
options = {
cascade: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
function f() {
var a, b;
a = b = 42;
return a;
}
}
expect: {
function f() {
return 42;
}
}
}
23 changes: 23 additions & 0 deletions test/compress/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,29 @@ call_args: {
}
}

call_args_drop_param: {
options = {
evaluate: true,
keep_fargs: false,
reduce_vars: true,
unused: true,
}
input: {
const a = 1;
console.log(a);
+function(a) {
return a;
}(a, b);
}
expect: {
const a = 1;
console.log(1);
+function() {
return 1;
}(b);
}
}

in_boolean_context: {
options = {
booleans: true,
Expand Down
Loading

0 comments on commit c8e6144

Please sign in to comment.