fixing andreyvit's issue with parentheses not being applied to multi-operators as the condition clause of a switch...

This commit is contained in:
Jeremy Ashkenas 2010-05-10 22:50:11 -04:00
parent 393fbf1b66
commit 4d0acc9b02
2 changed files with 13 additions and 18 deletions

View File

@ -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);

View File

@ -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