diff --git a/lib/grammar.js b/lib/grammar.js index bdd046bc..0b45b789 100644 --- a/lib/grammar.js +++ b/lib/grammar.js @@ -315,7 +315,7 @@ ], SimpleArgs: [ 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: [ diff --git a/lib/helpers.js b/lib/helpers.js index 4caf7446..b041ef8b 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -80,7 +80,7 @@ _b = array; for (_a = 0, _c = _b.length; _a < _c; _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; }); diff --git a/lib/nodes.js b/lib/nodes.js index c886cb51..592ae88e 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -466,7 +466,7 @@ _c = this.args; for (_b = 0, _d = _c.length; _b < _d; _b++) { arg = _c[_b]; - arg instanceof SplatNode ? (compilation = this.compileSplat(o)) : null; + (arg instanceof SplatNode) ? (compilation = this.compileSplat(o)) : null; } if (!(compilation)) { args = (function() { @@ -689,7 +689,7 @@ if (i === this.properties.length - 1) { join = ''; } - indent = prop instanceof CommentNode ? '' : this.idt(1); + indent = (prop instanceof CommentNode) ? '' : this.idt(1); if (!(prop instanceof AssignNode || prop instanceof CommentNode)) { prop = new AssignNode(prop, prop, 'object'); } @@ -1371,7 +1371,7 @@ if (code.substr(l - 1, 1) === ';') { code = code.substr(o, l - 1); } - return this.expression instanceof AssignNode ? code : ("(" + code + ")"); + return (this.expression instanceof AssignNode) ? code : ("(" + code + ")"); }; return ParentheticalNode; })(); @@ -1514,11 +1514,13 @@ IfNode = function(condition, body, tags) { this.condition = condition; this.body = body; - this.elseBody = null; this.tags = tags || {}; if (this.tags.invert) { 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; return this; }; @@ -1602,7 +1604,7 @@ } }; 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) { var body, child, comDent, condO, elsePart, ifDent, ifPart; diff --git a/src/nodes.coffee b/src/nodes.coffee index 899036f2..79875f20 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1330,9 +1330,12 @@ exports.IfNode: class IfNode extends BaseNode constructor: (condition, body, tags) -> @condition: condition @body: body - @elseBody: null @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 bodyNode: -> @body?.unwrap() diff --git a/test/test_if.coffee b/test/test_if.coffee index edebb0c0..a46ec0af 100644 --- a/test/test_if.coffee +++ b/test/test_if.coffee @@ -67,3 +67,10 @@ func: -> return (if false then callback()) ok func() is null + + +# If-to-ternary with instanceof requires parentheses (no comment). +if {} instanceof Object + ok yes +else + ok no