From dd18703b50210094da9c1293f3dbafd32d249367 Mon Sep 17 00:00:00 2001 From: Michael Ficarra Date: Thu, 2 Dec 2010 20:55:19 -0500 Subject: [PATCH] Issue #891: cannot safely invert `<` and `>` to `>=` and `<=` (or the other way around). Proper fix this time. --- lib/nodes.js | 21 +++++++-------------- src/nodes.coffee | 15 ++++++--------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index b5c2868f..d3b5ec93 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1461,11 +1461,7 @@ }; INVERSIONS = { '!==': '===', - '===': '!==', - '>': '<=', - '<=': '>', - '<': '>=', - '>=': '<' + '===': '!==' }; Op.prototype.children = ['first', 'second']; Op.prototype.isUnary = function() { @@ -1477,11 +1473,11 @@ }; Op.prototype.invert = function() { var fst, op, _ref; - if (op = INVERSIONS[this.operator]) { + if (this.isChainable() && this.first.isChainable()) { + this.inverted = !this.inverted; + return this; + } else if (op = INVERSIONS[this.operator]) { this.operator = op; - if (this.first.isChainable()) { - this.first.invert(); - } return this; } else if (this.second) { return new Parens(this).invert(); @@ -1518,14 +1514,11 @@ var code, fst, shared, _ref; _ref = this.first.second.cache(o), this.first.second = _ref[0], shared = _ref[1]; fst = this.first.compile(o, LEVEL_OP); - if (fst.charAt(0) === '(') { - fst = fst.slice(1, -1); - } code = "" + fst + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP)); - if (o.level < LEVEL_OP) { + if (o.level < LEVEL_OP && !this.inverted) { return code; } else { - return "(" + code + ")"; + return "" + (this.inverted ? '!' : '') + "(" + code + ")"; } }; Op.prototype.compileExistence = function(o) { diff --git a/src/nodes.coffee b/src/nodes.coffee index d80cbfe6..6080b3c8 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1161,10 +1161,6 @@ exports.Op = class Op extends Base INVERSIONS = '!==': '===' '===': '!==' - '>': '<=' - '<=': '>' - '<': '>=' - '>=': '<' children: ['first', 'second'] @@ -1177,9 +1173,11 @@ exports.Op = class Op extends Base @operator in ['<', '>', '>=', '<=', '===', '!=='] invert: -> - if op = INVERSIONS[@operator] + if @isChainable() and @first.isChainable() + @inverted = !@inverted + this + else if op = INVERSIONS[@operator] @operator = op - @first.invert() if @first.isChainable() this else if @second new Parens(this).invert() @@ -1208,10 +1206,9 @@ exports.Op = class Op extends Base # true compileChain: (o) -> [@first.second, shared] = @first.second.cache o - fst = @first.compile o, LEVEL_OP - fst = fst.slice 1, -1 if fst.charAt(0) is '(' + fst = @first.compile o, LEVEL_OP code = "#{fst} && #{ shared.compile o } #{@operator} #{ @second.compile o, LEVEL_OP }" - if o.level < LEVEL_OP then code else "(#{code})" + if o.level < LEVEL_OP and not @inverted then code else "#{if @inverted then '!' else ''}(#{code})" compileExistence: (o) -> if @first.isComplex()