mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 11:31:20 -05:00
783: corrected chained comparison precedence
This commit is contained in:
40
lib/nodes.js
40
lib/nodes.js
@@ -1232,9 +1232,6 @@
|
||||
'!==': '===',
|
||||
'===': '!=='
|
||||
};
|
||||
Op.prototype.CHAINABLE = ['<', '>', '>=', '<=', '===', '!=='];
|
||||
Op.prototype.PREFIX_OPERATORS = ['new', 'typeof', 'delete'];
|
||||
Op.prototype.MUTATORS = ['++', '--', 'delete'];
|
||||
Op.prototype.children = ['first', 'second'];
|
||||
Op.prototype.isUnary = function() {
|
||||
return !this.second;
|
||||
@@ -1244,7 +1241,7 @@
|
||||
};
|
||||
Op.prototype.isChainable = function() {
|
||||
var _ref2;
|
||||
return _ref2 = this.operator, __indexOf.call(this.CHAINABLE, _ref2) >= 0;
|
||||
return (_ref2 = this.operator) === '<' || _ref2 === '>' || _ref2 === '>=' || _ref2 === '<=' || _ref2 === '===' || _ref2 === '!==';
|
||||
};
|
||||
Op.prototype.invert = function() {
|
||||
var op;
|
||||
@@ -1253,18 +1250,15 @@
|
||||
return this;
|
||||
} else return this.second ? new Parens(this).invert() : Op.__super__.invert.call(this);
|
||||
};
|
||||
Op.prototype.toString = function(idt) {
|
||||
return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
|
||||
};
|
||||
Op.prototype.unfoldSoak = function(o) {
|
||||
var _ref2;
|
||||
return (_ref2 = this.operator, __indexOf.call(this.MUTATORS, _ref2) >= 0) && If.unfoldSoak(o, this, 'first');
|
||||
return ((_ref2 = this.operator) === '++' || _ref2 === '--' || _ref2 === 'delete') && If.unfoldSoak(o, this, 'first');
|
||||
};
|
||||
Op.prototype.compileNode = function(o) {
|
||||
if (this.isUnary()) {
|
||||
return this.compileUnary(o);
|
||||
}
|
||||
if (this.isChainable() && this.first.unwrap().isChainable()) {
|
||||
if (this.isChainable() && this.first.isChainable()) {
|
||||
return this.compileChain(o);
|
||||
}
|
||||
if (this.operator === '?') {
|
||||
@@ -1274,9 +1268,14 @@
|
||||
return "" + (this.first.compile(o, LEVEL.OP)) + " " + this.operator + " " + (this.second.compile(o, LEVEL.OP));
|
||||
};
|
||||
Op.prototype.compileChain = function(o) {
|
||||
var _ref2, shared;
|
||||
_ref2 = this.first.unwrap().second.cache(o), this.first.second = _ref2[0], shared = _ref2[1];
|
||||
return "" + (this.first.compile(o, LEVEL.OP)) + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LEVEL.OP));
|
||||
var _ref2, code, fst, shared;
|
||||
_ref2 = this.first.second.cache(o), this.first.second = _ref2[0], shared = _ref2[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));
|
||||
return o.level < LEVEL.OP ? code : "(" + code + ")";
|
||||
};
|
||||
Op.prototype.compileExistence = function(o) {
|
||||
var fst, ref;
|
||||
@@ -1290,10 +1289,19 @@
|
||||
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o, LEVEL.LIST)));
|
||||
};
|
||||
Op.prototype.compileUnary = function(o) {
|
||||
var _ref2, _ref3, parts, space;
|
||||
space = (_ref2 = this.operator, __indexOf.call(this.PREFIX_OPERATORS, _ref2) >= 0) || this.first instanceof Op && this.first.operator === this.operator && ((_ref3 = this.operator) === '+' || _ref3 === '-') ? ' ' : '';
|
||||
parts = [this.operator, space, this.first.compile(o, LEVEL.OP)];
|
||||
return (this.flip ? parts.reverse() : parts).join('');
|
||||
var op, parts;
|
||||
parts = [op = this.operator];
|
||||
if ((op === 'new' || op === 'typeof' || op === 'delete') || (op === '+' || op === '-') && this.first instanceof Op && this.first.operator === op) {
|
||||
parts.push(' ');
|
||||
}
|
||||
parts.push(this.first.compile(o, LEVEL.OP));
|
||||
if (this.flip) {
|
||||
parts.reverse();
|
||||
}
|
||||
return parts.join('');
|
||||
};
|
||||
Op.prototype.toString = function(idt) {
|
||||
return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
|
||||
};
|
||||
return Op;
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user