1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Pulling out a lastNonComment method.

This commit is contained in:
Jeremy Ashkenas 2010-12-16 11:35:51 -05:00
parent a7158ec69c
commit c3943d21d0
2 changed files with 23 additions and 20 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 = [];
@ -1936,8 +1934,8 @@
if (i === this.cases.length - 1 && !this.otherwise) {
break;
}
expr = last(block.expressions);
if (!expr || !expr.isPureStatement() || expr instanceof Comment) {
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) ->
@ -678,8 +684,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
''
@ -1524,8 +1529,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
expr = last block.expressions
if not expr or not expr.isPureStatement() or expr instanceof Comment
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 + '}'