Skip to content

Commit

Permalink
Merge pull request #1624 from alexlamsl/harmony-v2.8.14
Browse files Browse the repository at this point in the history
Merging from master for 2.8.14
  • Loading branch information
alexlamsl authored Mar 19, 2017
2 parents 129e449 + 4bf21ce commit 7906033
Show file tree
Hide file tree
Showing 24 changed files with 349 additions and 85 deletions.
85 changes: 33 additions & 52 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2791,7 +2791,7 @@ merge(Compressor.prototype, {
if (self.args.length != 1) {
return make_node(AST_Array, self, {
elements: self.args
}).transform(compressor);
}).optimize(compressor);
}
break;
case "Object":
Expand All @@ -2809,7 +2809,7 @@ merge(Compressor.prototype, {
left: self.args[0],
operator: "+",
right: make_node(AST_String, self, { value: "" })
}).transform(compressor);
}).optimize(compressor);
break;
case "Number":
if (self.args.length == 0) return make_node(AST_Number, self, {
Expand All @@ -2818,7 +2818,7 @@ merge(Compressor.prototype, {
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
expression: self.args[0],
operator: "+"
}).transform(compressor);
}).optimize(compressor);
case "Boolean":
if (self.args.length == 0) return make_node(AST_False, self);
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
Expand All @@ -2827,7 +2827,7 @@ merge(Compressor.prototype, {
operator: "!"
}),
operator: "!"
}).transform(compressor);
}).optimize(compressor);
break;
case "Function":
// new Function() => function(){}
Expand Down Expand Up @@ -2897,7 +2897,7 @@ merge(Compressor.prototype, {
left: make_node(AST_String, self, { value: "" }),
operator: "+",
right: exp.expression
}).transform(compressor);
}).optimize(compressor);
}
else if (exp instanceof AST_Dot && exp.expression instanceof AST_Array && exp.property == "join") EXIT: {
var separator;
Expand Down Expand Up @@ -2951,7 +2951,7 @@ merge(Compressor.prototype, {
left : prev,
right : el
});
}, first).transform(compressor);
}, first).optimize(compressor);
}
// need this awkward cloning to not affect original element
// best_of will decide which one to get through.
Expand All @@ -2961,6 +2961,16 @@ merge(Compressor.prototype, {
node.expression.expression.elements = elements;
return best_of(compressor, self, node);
}
else if (exp instanceof AST_Dot && exp.expression.is_string(compressor) && exp.property == "charAt") {
var arg = self.args[0];
var index = arg ? arg.evaluate(compressor) : 0;
if (index !== arg) {
return make_node(AST_Sub, exp, {
expression: exp.expression,
property: make_node_from_constant(index | 0, arg || exp)
}).optimize(compressor);
}
}
}
if (exp instanceof AST_Function && !self.expression.is_generator) {
if (exp.body[0] instanceof AST_Return) {
Expand Down Expand Up @@ -3129,33 +3139,30 @@ merge(Compressor.prototype, {
return self;
});

function has_side_effects_or_prop_access(node, compressor) {
var save_pure_getters = compressor.option("pure_getters");
compressor.options.pure_getters = false;
var ret = node.has_side_effects(compressor);
compressor.options.pure_getters = save_pure_getters;
return ret;
}

AST_Binary.DEFMETHOD("lift_sequences", function(compressor){
if (compressor.option("sequences")) {
if (this.left instanceof AST_Seq) {
var seq = this.left;
var x = seq.to_array();
this.left = x.pop();
x.push(this);
seq = AST_Seq.from_array(x).transform(compressor);
return seq;
}
if (this.right instanceof AST_Seq
&& this instanceof AST_Assign
&& !has_side_effects_or_prop_access(this.left, compressor)) {
var seq = this.right;
var x = seq.to_array();
this.right = x.pop();
x.push(this);
seq = AST_Seq.from_array(x).transform(compressor);
return seq;
return AST_Seq.from_array(x).optimize(compressor);
}
if (this.right instanceof AST_Seq && !this.left.has_side_effects(compressor)) {
var assign = this.operator == "=" && this.left instanceof AST_SymbolRef;
var root = this.right;
var cursor, seq = root;
while (assign || !seq.car.has_side_effects(compressor)) {
cursor = seq;
if (seq.cdr instanceof AST_Seq) {
seq = seq.cdr;
} else break;
}
if (cursor) {
this.right = cursor.cdr;
cursor.cdr = this;
return root.optimize(compressor);
}
}
}
return this;
Expand Down Expand Up @@ -3190,32 +3197,6 @@ merge(Compressor.prototype, {
reverse();
}
}
if (/^[!=]==?$/.test(self.operator)) {
if (self.left instanceof AST_SymbolRef && self.right instanceof AST_Conditional) {
if (self.right.consequent instanceof AST_SymbolRef
&& self.right.consequent.definition() === self.left.definition()) {
if (/^==/.test(self.operator)) return self.right.condition;
if (/^!=/.test(self.operator)) return self.right.condition.negate(compressor);
}
if (self.right.alternative instanceof AST_SymbolRef
&& self.right.alternative.definition() === self.left.definition()) {
if (/^==/.test(self.operator)) return self.right.condition.negate(compressor);
if (/^!=/.test(self.operator)) return self.right.condition;
}
}
if (self.right instanceof AST_SymbolRef && self.left instanceof AST_Conditional) {
if (self.left.consequent instanceof AST_SymbolRef
&& self.left.consequent.definition() === self.right.definition()) {
if (/^==/.test(self.operator)) return self.left.condition;
if (/^!=/.test(self.operator)) return self.left.condition.negate(compressor);
}
if (self.left.alternative instanceof AST_SymbolRef
&& self.left.alternative.definition() === self.right.definition()) {
if (/^==/.test(self.operator)) return self.left.condition.negate(compressor);
if (/^!=/.test(self.operator)) return self.left.condition;
}
}
}
}
self = self.lift_sequences(compressor);
if (compressor.option("comparisons")) 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.13",
"version": "2.8.14",
"engines": {
"node": ">=0.8.0"
},
Expand Down
7 changes: 7 additions & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ collapse_vars_side_effects_1: {
z = i += 4;
log(x, z, y, i);
}
f1(), f2(), f3(), f4();
}
expect: {
function f1() {
Expand Down Expand Up @@ -73,7 +74,9 @@ collapse_vars_side_effects_1: {
y = i += 3;
log(x, i += 4, y, i);
}
f1(), f2(), f3(), f4();
}
expect_stdout: true
}

collapse_vars_side_effects_2: {
Expand Down Expand Up @@ -823,6 +826,7 @@ collapse_vars_repeated: {
console.log(e + "!");
})("!");
}
expect_stdout: true
}

collapse_vars_closures: {
Expand Down Expand Up @@ -1109,6 +1113,7 @@ collapse_vars_eval_and_with: {
return function() { with (o) console.log(a) };
})()();
}
expect_stdout: true
}

collapse_vars_constants: {
Expand Down Expand Up @@ -1168,6 +1173,7 @@ collapse_vars_arguments: {
(function(){console.log(arguments);})(7, 1);
})();
}
expect_stdout: true
}

collapse_vars_short_circuit: {
Expand Down Expand Up @@ -1317,6 +1323,7 @@ collapse_vars_regexp: {
console.log(result[0]);
})();
}
expect_stdout: true
}

issue_1537: {
Expand Down
7 changes: 7 additions & 0 deletions test/compress/concat-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ concat_2: {
"1" + "2" + "3"
);
}
expect_stdout: true
}

concat_3: {
Expand Down Expand Up @@ -79,6 +80,7 @@ concat_3: {
1 + 2 + "3" + "4" + "5"
);
}
expect_stdout: true
}

concat_4: {
Expand Down Expand Up @@ -107,6 +109,7 @@ concat_4: {
1 + "2" + "3" + "4" + "5"
);
}
expect_stdout: true
}

concat_5: {
Expand Down Expand Up @@ -135,6 +138,7 @@ concat_5: {
"1" + 2 + "3" + "4" + "5"
);
}
expect_stdout: true
}

concat_6: {
Expand Down Expand Up @@ -163,6 +167,7 @@ concat_6: {
"1" + "2" + "3" + "4" + "5"
);
}
expect_stdout: true
}

concat_7: {
Expand All @@ -188,6 +193,7 @@ concat_7: {
x += "foo"
);
}
expect_stdout: true
}

concat_8: {
Expand All @@ -213,4 +219,5 @@ concat_8: {
x += "foo"
);
}
expect_stdout: true
}
96 changes: 96 additions & 0 deletions test/compress/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,99 @@ no_evaluate: {
}
}
}

equality_conditionals_false: {
options = {
conditionals: false,
sequences: true,
}
input: {
function f(a, b, c) {
console.log(
a == (b ? a : a),
a == (b ? a : c),
a != (b ? a : a),
a != (b ? a : c),
a === (b ? a : a),
a === (b ? a : c),
a !== (b ? a : a),
a !== (b ? a : c)
);
}
f(0, 0, 0);
f(0, true, 0);
f(1, 2, 3);
f(1, null, 3);
f(NaN);
f(NaN, "foo");
}
expect: {
function f(a, b, c) {
console.log(
a == (b ? a : a),
a == (b ? a : c),
a != (b ? a : a),
a != (b ? a : c),
a === (b ? a : a),
a === (b ? a : c),
a !== (b ? a : a),
a !== (b ? a : c)
);
}
f(0, 0, 0),
f(0, true, 0),
f(1, 2, 3),
f(1, null, 3),
f(NaN),
f(NaN, "foo");
}
expect_stdout: true
}

equality_conditionals_true: {
options = {
conditionals: true,
sequences: true,
}
input: {
function f(a, b, c) {
console.log(
a == (b ? a : a),
a == (b ? a : c),
a != (b ? a : a),
a != (b ? a : c),
a === (b ? a : a),
a === (b ? a : c),
a !== (b ? a : a),
a !== (b ? a : c)
);
}
f(0, 0, 0);
f(0, true, 0);
f(1, 2, 3);
f(1, null, 3);
f(NaN);
f(NaN, "foo");
}
expect: {
function f(a, b, c) {
console.log(
(b, a == a),
a == (b ? a : c),
(b, a != a),
a != (b ? a : c),
(b, a === a),
a === (b ? a : c),
(b, a !== a),
a !== (b ? a : c)
);
}
f(0, 0, 0),
f(0, true, 0),
f(1, 2, 3),
f(1, null, 3),
f(NaN),
f(NaN, "foo");
}
expect_stdout: true
}
1 change: 1 addition & 0 deletions test/compress/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,5 @@ regexp_literal_not_const: {
while (result = REGEXP_LITERAL.exec("acdabcdeabbb")) console.log(result[0]);
})();
}
expect_stdout: true
}
Loading

0 comments on commit 7906033

Please sign in to comment.