From f9b906e12c4f39204df41f953affc71604b08033 Mon Sep 17 00:00:00 2001 From: Justin Lau Date: Sun, 5 May 2013 19:54:27 +0800 Subject: [PATCH 1/2] Added test cases for #104. Signed-off-by: Justin Lau --- test/compress/issue-143.js | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/compress/issue-143.js diff --git a/test/compress/issue-143.js b/test/compress/issue-143.js new file mode 100644 index 00000000..4c79790b --- /dev/null +++ b/test/compress/issue-143.js @@ -0,0 +1,48 @@ +/** + * There was an incorrect sort behaviour documented in issue #143: + * (x = f(…)) <= x → x >= (x = f(…)) + * + * For example, let the equation be: + * (a = parseInt('100')) <= a + * + * If a was an integer and has the value of 99, + * (a = parseInt('100')) <= a → 100 <= 100 → true + * + * When transformed incorrectly: + * a >= (a = parseInt('100')) → 99 >= 100 → false + */ + +tranformation_sort_order_equal: { + options = { + comparisons: true, + }; + + input: { (a = parseInt('100')) == a } + expect: { (a = parseInt('100')) == a } +} + +tranformation_sort_order_unequal: { + options = { + comparisons: true, + }; + + input: { (a = parseInt('100')) != a } + expect: { (a = parseInt('100')) != a } +} + +tranformation_sort_order_lesser_or_equal: { + options = { + comparisons: true, + }; + + input: { (a = parseInt('100')) <= a } + expect: { (a = parseInt('100')) <= a } +} +tranformation_sort_order_greater_or_equal: { + options = { + comparisons: true, + }; + + input: { (a = parseInt('100')) >= a } + expect: { (a = parseInt('100')) >= a } +} \ No newline at end of file From fd501299a53731346aaedbaf9763305a1d1b9c2b Mon Sep 17 00:00:00 2001 From: Justin Lau Date: Sun, 5 May 2013 20:38:32 +0800 Subject: [PATCH 2/2] Fixed typeof undefined optimization and updated related test case to accomodates the sort behaviour changes made in commit mishoo/UglifyJS2@aebafad41eab48f43ed649ce8c77e8f1528b50da. Signed-off-by: Justin Lau --- lib/compress.js | 16 ++++++++-------- test/compress/issue-105.js | 5 ++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index ebd3dd7a..3692128f 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1751,14 +1751,14 @@ merge(Compressor.prototype, { // XXX: intentionally falling down to the next case case "==": case "!=": - if (self.left instanceof AST_String - && self.left.value == "undefined" - && self.right instanceof AST_UnaryPrefix - && self.right.operator == "typeof" - && compressor.option("unsafe")) { - if (!(self.right.expression instanceof AST_SymbolRef) - || !self.right.expression.undeclared()) { - self.left = self.right.expression; + if (compressor.option("unsafe") + && self.left instanceof AST_UnaryPrefix + && self.left.operator == "typeof" + && self.right instanceof AST_String + && self.right.value == "undefined") { + if (!(self.left.expression instanceof AST_SymbolRef) + || !self.left.expression.undeclared()) { + self.left = self.left.expression; self.right = make_node(AST_Undefined, self.left).optimize(compressor); if (self.operator.length == 2) self.operator += "="; } diff --git a/test/compress/issue-105.js b/test/compress/issue-105.js index 349d732d..0c37eb82 100644 --- a/test/compress/issue-105.js +++ b/test/compress/issue-105.js @@ -1,10 +1,9 @@ typeof_eq_undefined: { options = { - comparisons: true, - unsafe: false + comparisons: true }; input: { a = typeof b.c != "undefined" } - expect: { a = "undefined" != typeof b.c } + expect: { a = typeof b.c != "undefined" } } typeof_eq_undefined_unsafe: {