Issue #891: cannot safely invert `<` and `>` to `>=` and `<=` (or the

other way around). Proper fix this time.
This commit is contained in:
Michael Ficarra 2010-12-02 20:55:19 -05:00
parent b1ba298ffc
commit dd18703b50
2 changed files with 13 additions and 23 deletions

View File

@ -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) {

View File

@ -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()