mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Chained comparisons now properly apply DeMorgan's Laws. I couldn't
believe there wasn't a test to remind me to toggle the {dis,con}junctions. Added that test.
This commit is contained in:
parent
23b4d2fd1d
commit
c50cb65019
3 changed files with 7 additions and 2 deletions
|
@ -1485,6 +1485,7 @@
|
|||
}
|
||||
curr = this;
|
||||
while (curr && (curr.operator != null)) {
|
||||
curr.invert = !curr.invert;
|
||||
curr.operator = INVERSIONS[curr.operator];
|
||||
curr = curr.first;
|
||||
}
|
||||
|
@ -1533,7 +1534,7 @@
|
|||
if (this.first.unwrap() instanceof Op && this.first.isChainable() && fst.charAt(0) === '(') {
|
||||
fst = fst.slice(1, -1);
|
||||
}
|
||||
code = "" + fst + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP));
|
||||
code = "" + fst + " " + (this.invert ? '&&' : '||') + " " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP));
|
||||
return "(" + code + ")";
|
||||
};
|
||||
Op.prototype.compileExistence = function(o) {
|
||||
|
|
|
@ -1182,6 +1182,7 @@ exports.Op = class Op extends Base
|
|||
return new Parens(this).invert() unless allInvertable
|
||||
curr = @
|
||||
while curr and curr.operator?
|
||||
curr.invert = !curr.invert
|
||||
curr.operator = INVERSIONS[curr.operator]
|
||||
curr = curr.first
|
||||
this
|
||||
|
@ -1219,7 +1220,7 @@ exports.Op = class Op extends Base
|
|||
[@first.second, shared] = @first.second.cache o
|
||||
fst = @first.compile o, LEVEL_OP
|
||||
fst = fst.slice 1, -1 if @first.unwrap() instanceof Op and @first.isChainable() and fst.charAt(0) is '('
|
||||
code = "#{fst} && #{ shared.compile o } #{@operator} #{ @second.compile o, LEVEL_OP }"
|
||||
code = "#{fst} #{if @invert then '&&' else '||'} #{ shared.compile o } #{@operator} #{ @second.compile o, LEVEL_OP }"
|
||||
"(#{code})"
|
||||
|
||||
compileExistence: (o) ->
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
# CoffeeScript's operations should be chainable, like Python's.
|
||||
ok 500 > 50 > 5 > -5
|
||||
|
||||
# Some chainable operators can be negated by `unless`
|
||||
ok (true unless 0==10!=100)
|
||||
|
||||
ok true is not false is true is not false
|
||||
|
||||
ok 0 is 0 isnt 50 is 50
|
||||
|
|
Loading…
Reference in a new issue