mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
removing now-unused logic from the IfNode, that used to handle switch statements.
This commit is contained in:
36
lib/nodes.js
36
lib/nodes.js
@@ -1808,7 +1808,7 @@
|
|||||||
};
|
};
|
||||||
__extends(IfNode, BaseNode);
|
__extends(IfNode, BaseNode);
|
||||||
IfNode.prototype["class"] = 'IfNode';
|
IfNode.prototype["class"] = 'IfNode';
|
||||||
IfNode.prototype.children = ['condition', 'switchSubject', 'body', 'elseBody', 'assigner'];
|
IfNode.prototype.children = ['condition', 'body', 'elseBody', 'assigner'];
|
||||||
IfNode.prototype.topSensitive = function() {
|
IfNode.prototype.topSensitive = function() {
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@@ -1822,37 +1822,6 @@
|
|||||||
this.tags.statement = true;
|
this.tags.statement = true;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
IfNode.prototype.switchesOver = function(expression) {
|
|
||||||
this.switchSubject = expression;
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
IfNode.prototype.rewriteSwitch = function(o) {
|
|
||||||
var _b, _c, _d, cond, i, variable;
|
|
||||||
this.assigner = this.switchSubject;
|
|
||||||
if (!(this.switchSubject.unwrap() instanceof LiteralNode)) {
|
|
||||||
variable = literal(o.scope.freeVariable());
|
|
||||||
this.assigner = new AssignNode(variable, this.switchSubject);
|
|
||||||
this.switchSubject = variable;
|
|
||||||
}
|
|
||||||
this.condition = (function() {
|
|
||||||
_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.switchSubject, cond);
|
|
||||||
}).call(this));
|
|
||||||
}
|
|
||||||
return _b;
|
|
||||||
}).call(this);
|
|
||||||
if (this.isChain) {
|
|
||||||
this.elseBodyNode().switchesOver(this.switchSubject);
|
|
||||||
}
|
|
||||||
this.switchSubject = undefined;
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
IfNode.prototype.addElse = function(elseBody, statement) {
|
IfNode.prototype.addElse = function(elseBody, statement) {
|
||||||
if (this.isChain) {
|
if (this.isChain) {
|
||||||
this.elseBodyNode().addElse(elseBody, statement);
|
this.elseBodyNode().addElse(elseBody, statement);
|
||||||
@@ -1897,9 +1866,6 @@
|
|||||||
};
|
};
|
||||||
IfNode.prototype.compileStatement = function(o) {
|
IfNode.prototype.compileStatement = function(o) {
|
||||||
var body, child, comDent, condO, elsePart, ifDent, ifPart, top;
|
var body, child, comDent, condO, elsePart, ifDent, ifPart, top;
|
||||||
if (this.switchSubject) {
|
|
||||||
this.rewriteSwitch(o);
|
|
||||||
}
|
|
||||||
top = del(o, 'top');
|
top = del(o, 'top');
|
||||||
child = del(o, 'chainChild');
|
child = del(o, 'chainChild');
|
||||||
condO = merge(o);
|
condO = merge(o);
|
||||||
|
|||||||
@@ -1461,15 +1461,15 @@ exports.SwitchNode = class SwitchNode extends BaseNode
|
|||||||
|
|
||||||
#### IfNode
|
#### IfNode
|
||||||
|
|
||||||
# *If/else* statements. Our *switch/when* will be compiled into this. Acts as an
|
# *If/else* statements. Acts as an expression by pushing down requested returns
|
||||||
# expression by pushing down requested returns to the last line of each clause.
|
# to the last line of each clause.
|
||||||
#
|
#
|
||||||
# Single-expression **IfNodes** are compiled into ternary operators if possible,
|
# Single-expression **IfNodes** are compiled into ternary operators if possible,
|
||||||
# because ternaries are already proper expressions, and don't need conversion.
|
# because ternaries are already proper expressions, and don't need conversion.
|
||||||
exports.IfNode = class IfNode extends BaseNode
|
exports.IfNode = class IfNode extends BaseNode
|
||||||
|
|
||||||
class: 'IfNode'
|
class: 'IfNode'
|
||||||
children: ['condition', 'switchSubject', 'body', 'elseBody', 'assigner']
|
children: ['condition', 'body', 'elseBody', 'assigner']
|
||||||
|
|
||||||
topSensitive: -> true
|
topSensitive: -> true
|
||||||
|
|
||||||
@@ -1490,28 +1490,6 @@ exports.IfNode = class IfNode extends BaseNode
|
|||||||
@tags.statement = true
|
@tags.statement = true
|
||||||
this
|
this
|
||||||
|
|
||||||
# Tag a chain of **IfNodes** with their object(s) to switch on for equality
|
|
||||||
# tests. `rewriteSwitch` will perform the actual change at compile time.
|
|
||||||
switchesOver: (expression) ->
|
|
||||||
@switchSubject = expression
|
|
||||||
this
|
|
||||||
|
|
||||||
# Rewrite a chain of **IfNodes** with their switch condition for equality.
|
|
||||||
# Ensure that the switch expression isn't evaluated more than once.
|
|
||||||
rewriteSwitch: (o) ->
|
|
||||||
@assigner = @switchSubject
|
|
||||||
unless (@switchSubject.unwrap() instanceof LiteralNode)
|
|
||||||
variable = literal(o.scope.freeVariable())
|
|
||||||
@assigner = new AssignNode(variable, @switchSubject)
|
|
||||||
@switchSubject = variable
|
|
||||||
@condition = for cond, i in flatten [@condition]
|
|
||||||
cond = new ParentheticalNode(cond) if cond instanceof OpNode
|
|
||||||
new OpNode('==', (if i is 0 then @assigner else @switchSubject), cond)
|
|
||||||
@elseBodyNode().switchesOver(@switchSubject) if @isChain
|
|
||||||
# prevent this rewrite from happening again
|
|
||||||
@switchSubject = undefined
|
|
||||||
this
|
|
||||||
|
|
||||||
# Rewrite a chain of **IfNodes** to add a default case as the final *else*.
|
# Rewrite a chain of **IfNodes** to add a default case as the final *else*.
|
||||||
addElse: (elseBody, statement) ->
|
addElse: (elseBody, statement) ->
|
||||||
if @isChain
|
if @isChain
|
||||||
@@ -1548,7 +1526,6 @@ exports.IfNode = class IfNode extends BaseNode
|
|||||||
# Compile the **IfNode** as a regular *if-else* statement. Flattened chains
|
# Compile the **IfNode** as a regular *if-else* statement. Flattened chains
|
||||||
# force inner *else* bodies into statement form.
|
# force inner *else* bodies into statement form.
|
||||||
compileStatement: (o) ->
|
compileStatement: (o) ->
|
||||||
@rewriteSwitch(o) if @switchSubject
|
|
||||||
top = del o, 'top'
|
top = del o, 'top'
|
||||||
child = del o, 'chainChild'
|
child = del o, 'chainChild'
|
||||||
condO = merge o
|
condO = merge o
|
||||||
|
|||||||
Reference in New Issue
Block a user