mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Issue #891: cannot safely invert <
and >
to >=
and <=
(or the
other way around). Proper fix this time.
This commit is contained in:
parent
b1ba298ffc
commit
dd18703b50
2 changed files with 13 additions and 23 deletions
21
lib/nodes.js
21
lib/nodes.js
|
@ -1461,11 +1461,7 @@
|
||||||
};
|
};
|
||||||
INVERSIONS = {
|
INVERSIONS = {
|
||||||
'!==': '===',
|
'!==': '===',
|
||||||
'===': '!==',
|
'===': '!=='
|
||||||
'>': '<=',
|
|
||||||
'<=': '>',
|
|
||||||
'<': '>=',
|
|
||||||
'>=': '<'
|
|
||||||
};
|
};
|
||||||
Op.prototype.children = ['first', 'second'];
|
Op.prototype.children = ['first', 'second'];
|
||||||
Op.prototype.isUnary = function() {
|
Op.prototype.isUnary = function() {
|
||||||
|
@ -1477,11 +1473,11 @@
|
||||||
};
|
};
|
||||||
Op.prototype.invert = function() {
|
Op.prototype.invert = function() {
|
||||||
var fst, op, _ref;
|
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;
|
this.operator = op;
|
||||||
if (this.first.isChainable()) {
|
|
||||||
this.first.invert();
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
} else if (this.second) {
|
} else if (this.second) {
|
||||||
return new Parens(this).invert();
|
return new Parens(this).invert();
|
||||||
|
@ -1518,14 +1514,11 @@
|
||||||
var code, fst, shared, _ref;
|
var code, fst, shared, _ref;
|
||||||
_ref = this.first.second.cache(o), this.first.second = _ref[0], shared = _ref[1];
|
_ref = this.first.second.cache(o), this.first.second = _ref[0], shared = _ref[1];
|
||||||
fst = this.first.compile(o, LEVEL_OP);
|
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));
|
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;
|
return code;
|
||||||
} else {
|
} else {
|
||||||
return "(" + code + ")";
|
return "" + (this.inverted ? '!' : '') + "(" + code + ")";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Op.prototype.compileExistence = function(o) {
|
Op.prototype.compileExistence = function(o) {
|
||||||
|
|
|
@ -1161,10 +1161,6 @@ exports.Op = class Op extends Base
|
||||||
INVERSIONS =
|
INVERSIONS =
|
||||||
'!==': '==='
|
'!==': '==='
|
||||||
'===': '!=='
|
'===': '!=='
|
||||||
'>': '<='
|
|
||||||
'<=': '>'
|
|
||||||
'<': '>='
|
|
||||||
'>=': '<'
|
|
||||||
|
|
||||||
children: ['first', 'second']
|
children: ['first', 'second']
|
||||||
|
|
||||||
|
@ -1177,9 +1173,11 @@ exports.Op = class Op extends Base
|
||||||
@operator in ['<', '>', '>=', '<=', '===', '!==']
|
@operator in ['<', '>', '>=', '<=', '===', '!==']
|
||||||
|
|
||||||
invert: ->
|
invert: ->
|
||||||
if op = INVERSIONS[@operator]
|
if @isChainable() and @first.isChainable()
|
||||||
|
@inverted = !@inverted
|
||||||
|
this
|
||||||
|
else if op = INVERSIONS[@operator]
|
||||||
@operator = op
|
@operator = op
|
||||||
@first.invert() if @first.isChainable()
|
|
||||||
this
|
this
|
||||||
else if @second
|
else if @second
|
||||||
new Parens(this).invert()
|
new Parens(this).invert()
|
||||||
|
@ -1209,9 +1207,8 @@ exports.Op = class Op extends Base
|
||||||
compileChain: (o) ->
|
compileChain: (o) ->
|
||||||
[@first.second, shared] = @first.second.cache o
|
[@first.second, shared] = @first.second.cache o
|
||||||
fst = @first.compile o, LEVEL_OP
|
fst = @first.compile o, LEVEL_OP
|
||||||
fst = fst.slice 1, -1 if fst.charAt(0) is '('
|
|
||||||
code = "#{fst} && #{ shared.compile o } #{@operator} #{ @second.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) ->
|
compileExistence: (o) ->
|
||||||
if @first.isComplex()
|
if @first.isComplex()
|
||||||
|
|
Loading…
Add table
Reference in a new issue