1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

fixing If/else-to-ternary with instanceof as an unparenthesized condition. JS operator precedence.

This commit is contained in:
Jeremy Ashkenas 2010-07-23 20:44:56 -07:00
parent 24a5adc898
commit de9fb7777b
5 changed files with 21 additions and 9 deletions

View file

@ -315,7 +315,7 @@
], ],
SimpleArgs: [ SimpleArgs: [
o("Expression"), o("SimpleArgs , Expression", function() { o("Expression"), o("SimpleArgs , Expression", function() {
return $1 instanceof Array ? $1.concat([$3]) : [$1].concat([$3]); return ($1 instanceof Array) ? $1.concat([$3]) : [$1].concat([$3]);
}) })
], ],
Try: [ Try: [

View file

@ -80,7 +80,7 @@
_b = array; _b = array;
for (_a = 0, _c = _b.length; _a < _c; _a++) { for (_a = 0, _c = _b.length; _a < _c; _a++) {
item = _b[_a]; item = _b[_a];
item instanceof Array ? (memo = memo.concat(item)) : memo.push(item); (item instanceof Array) ? (memo = memo.concat(item)) : memo.push(item);
} }
return memo; return memo;
}); });

View file

@ -466,7 +466,7 @@
_c = this.args; _c = this.args;
for (_b = 0, _d = _c.length; _b < _d; _b++) { for (_b = 0, _d = _c.length; _b < _d; _b++) {
arg = _c[_b]; arg = _c[_b];
arg instanceof SplatNode ? (compilation = this.compileSplat(o)) : null; (arg instanceof SplatNode) ? (compilation = this.compileSplat(o)) : null;
} }
if (!(compilation)) { if (!(compilation)) {
args = (function() { args = (function() {
@ -689,7 +689,7 @@
if (i === this.properties.length - 1) { if (i === this.properties.length - 1) {
join = ''; join = '';
} }
indent = prop instanceof CommentNode ? '' : this.idt(1); indent = (prop instanceof CommentNode) ? '' : this.idt(1);
if (!(prop instanceof AssignNode || prop instanceof CommentNode)) { if (!(prop instanceof AssignNode || prop instanceof CommentNode)) {
prop = new AssignNode(prop, prop, 'object'); prop = new AssignNode(prop, prop, 'object');
} }
@ -1371,7 +1371,7 @@
if (code.substr(l - 1, 1) === ';') { if (code.substr(l - 1, 1) === ';') {
code = code.substr(o, l - 1); code = code.substr(o, l - 1);
} }
return this.expression instanceof AssignNode ? code : ("(" + code + ")"); return (this.expression instanceof AssignNode) ? code : ("(" + code + ")");
}; };
return ParentheticalNode; return ParentheticalNode;
})(); })();
@ -1514,11 +1514,13 @@
IfNode = function(condition, body, tags) { IfNode = function(condition, body, tags) {
this.condition = condition; this.condition = condition;
this.body = body; this.body = body;
this.elseBody = null;
this.tags = tags || {}; this.tags = tags || {};
if (this.tags.invert) { if (this.tags.invert) {
this.condition = new OpNode('!', new ParentheticalNode(this.condition)); this.condition = new OpNode('!', new ParentheticalNode(this.condition));
} else if (this.condition instanceof OpNode && this.condition.operator === 'instanceof') {
this.condition = new ParentheticalNode(this.condition);
} }
this.elseBody = null;
this.isChain = false; this.isChain = false;
return this; return this;
}; };
@ -1602,7 +1604,7 @@
} }
}; };
IfNode.prototype.ensureExpressions = function(node) { IfNode.prototype.ensureExpressions = function(node) {
return node instanceof Expressions ? node : new Expressions([node]); return (node instanceof Expressions) ? node : new Expressions([node]);
}; };
IfNode.prototype.compileStatement = function(o) { IfNode.prototype.compileStatement = function(o) {
var body, child, comDent, condO, elsePart, ifDent, ifPart; var body, child, comDent, condO, elsePart, ifDent, ifPart;

View file

@ -1330,9 +1330,12 @@ exports.IfNode: class IfNode extends BaseNode
constructor: (condition, body, tags) -> constructor: (condition, body, tags) ->
@condition: condition @condition: condition
@body: body @body: body
@elseBody: null
@tags: tags or {} @tags: tags or {}
@condition: new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert if @tags.invert
@condition: new OpNode('!', new ParentheticalNode(@condition))
else if @condition instanceof OpNode and @condition.operator is 'instanceof'
@condition: new ParentheticalNode(@condition)
@elseBody: null
@isChain: false @isChain: false
bodyNode: -> @body?.unwrap() bodyNode: -> @body?.unwrap()

View file

@ -67,3 +67,10 @@ func: ->
return (if false then callback()) return (if false then callback())
ok func() is null ok func() is null
# If-to-ternary with instanceof requires parentheses (no comment).
if {} instanceof Object
ok yes
else
ok no