From 4d0acc9b02c04c2235a51710a877445b62eb29ee Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 10 May 2010 22:50:11 -0400 Subject: [PATCH] fixing andreyvit's issue with parentheses not being applied to multi-operators as the condition clause of a switch... --- lib/nodes.js | 22 ++++++++++------------ src/nodes.coffee | 9 +++------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index b6eb6ef1..900f05b1 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1658,9 +1658,6 @@ this.body = body; this.else_body = null; this.tags = tags || {}; - if (this.condition instanceof Array) { - this.multiple = true; - } if (this.tags.invert) { this.condition = new OpNode('!', new ParentheticalNode(this.condition)); } @@ -1695,16 +1692,17 @@ this.switch_subject = variable; } this.condition = (function() { - if (this.multiple) { - _b = []; _c = this.condition; - for (i = 0, _d = _c.length; i < _d; i++) { - cond = _c[i]; - _b.push(new OpNode('==', (i === 0 ? this.assigner : this.switch_subject), cond)); - } - return _b; - } else { - return new OpNode('==', this.assigner, this.condition); + _b = []; _c = flatten([this.condition]); + for (i = 0, _d = _c.length; i < _d; i++) { + cond = _c[i]; + _b.push((function() { + if (cond instanceof OpNode) { + cond = new ParentheticalNode(cond); + } + return new OpNode('==', (i === 0 ? this.assigner : this.switch_subject), cond); + }).call(this)); } + return _b; }).call(this); if (this.is_chain) { this.else_body_node().switches_over(this.switch_subject); diff --git a/src/nodes.coffee b/src/nodes.coffee index c74cb257..43fbe3f4 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1234,7 +1234,6 @@ exports.IfNode: class IfNode extends BaseNode @body: body @else_body: null @tags: tags or {} - @multiple: true if @condition instanceof Array @condition: new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert @is_chain: false @@ -1259,11 +1258,9 @@ exports.IfNode: class IfNode extends BaseNode variable: literal(o.scope.free_variable()) @assigner: new AssignNode(variable, @switch_subject) @switch_subject: variable - @condition: if @multiple - for cond, i in @condition - new OpNode('==', (if i is 0 then @assigner else @switch_subject), cond) - else - new OpNode('==', @assigner, @condition) + @condition: for cond, i in flatten [@condition] + cond: new ParentheticalNode(cond) if cond instanceof OpNode + new OpNode('==', (if i is 0 then @assigner else @switch_subject), cond) @else_body_node().switches_over(@switch_subject) if @is_chain # prevent this rewrite from happening again @switch_subject: undefined