Merge branch 'master' of git://github.com/jashkenas/coffee-script into refactorTests

This commit is contained in:
Michael Ficarra
2010-12-16 12:41:55 -05:00
2 changed files with 24 additions and 21 deletions

View File

@@ -94,6 +94,16 @@
return node.isPureStatement() && !(node instanceof Comment);
});
};
Base.prototype.lastNonComment = function(list) {
var i;
i = list.length;
while (i--) {
if (!(list[i] instanceof Comment)) {
return list[i];
}
}
return null;
};
Base.prototype.toString = function(idt, name) {
var tree;
if (idt == null) {
@@ -804,7 +814,7 @@
}
Obj.prototype.children = ['properties'];
Obj.prototype.compileNode = function(o) {
var i, idt, indent, join, lastNoncom, nonComments, obj, prop, props;
var i, idt, indent, join, lastNoncom, obj, prop, props;
props = this.properties;
if (!props.length) {
if (this.front) {
@@ -814,19 +824,7 @@
}
}
idt = o.indent += TAB;
nonComments = (function() {
var _i, _len, _ref, _results;
_ref = this.properties;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
prop = _ref[_i];
if (!(prop instanceof Comment)) {
_results.push(prop);
}
}
return _results;
}.call(this));
lastNoncom = last(nonComments);
lastNoncom = this.lastNonComment(this.properties);
props = function() {
var _len, _results;
_results = [];
@@ -1915,7 +1913,7 @@
return this;
};
Switch.prototype.compileNode = function(o) {
var block, body, code, cond, conditions, exprs, i, idt1, idt2, _i, _len, _len2, _ref, _ref2, _ref3, _ref4;
var block, body, code, cond, conditions, expr, i, idt1, idt2, _i, _len, _len2, _ref, _ref2, _ref3, _ref4;
idt1 = o.indent + TAB;
idt2 = o.indent = idt1 + TAB;
code = this.tab + ("switch (" + (((_ref = this.subject) != null ? _ref.compile(o, LEVEL_PAREN) : void 0) || false) + ") {\n");
@@ -1936,8 +1934,8 @@
if (i === this.cases.length - 1 && !this.otherwise) {
break;
}
exprs = block.expressions;
if (!exprs.length || !last(exprs).isPureStatement()) {
expr = this.lastNonComment(block.expressions);
if (!expr || !expr.isPureStatement()) {
code += idt2 + 'break;\n';
}
}

View File

@@ -100,6 +100,12 @@ exports.Base = class Base
@isPureStatement() or @contains (node) ->
node.isPureStatement() and node not instanceof Comment
# Pull out the last non-comment node of a node list.
lastNonComment: (list) ->
i = list.length
return list[i] while i-- when list[i] not instanceof Comment
null
# `toString` representation of the node, for inspecting the parse tree.
# This is what `coffee --nodes` prints out.
toString: (idt = '', name = @constructor.name) ->
@@ -687,8 +693,7 @@ exports.Obj = class Obj extends Base
props = @properties
return (if @front then '({})' else '{}') unless props.length
idt = o.indent += TAB
nonComments = (prop for prop in @properties when prop not instanceof Comment)
lastNoncom = last nonComments
lastNoncom = @lastNonComment @properties
props = for prop, i in props
join = if i is props.length - 1
''
@@ -1533,8 +1538,8 @@ exports.Switch = class Switch extends Base
code += idt1 + "case #{ cond.compile o, LEVEL_PAREN }:\n"
code += body + '\n' if body = block.compile o, LEVEL_TOP
break if i is @cases.length - 1 and not @otherwise
exprs = block.expressions
if not exprs.length or not last(exprs).isPureStatement()
expr = @lastNonComment block.expressions
if not expr or not expr.isPureStatement()
code += idt2 + 'break;\n'
code += idt1 + "default:\n#{ @otherwise.compile o, LEVEL_TOP }\n" if @otherwise
code + @tab + '}'