mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
coco fd028a0: closes #13; leading comments/literals are now placed before variable declarations
This commit is contained in:
28
lib/nodes.js
28
lib/nodes.js
@@ -257,17 +257,31 @@
|
||||
}
|
||||
};
|
||||
Expressions.prototype.compileWithDeclarations = function(o) {
|
||||
var code, scope;
|
||||
var code, exp, i, post, rest, scope, _len, _ref;
|
||||
code = post = '';
|
||||
_ref = this.expressions;
|
||||
for (i = 0, _len = _ref.length; i < _len; i++) {
|
||||
exp = _ref[i];
|
||||
exp = exp.unwrap();
|
||||
if (!(exp instanceof Comment || exp instanceof Literal)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
o.level = LEVEL_TOP;
|
||||
code = this.compileNode(o);
|
||||
if (i) {
|
||||
rest = this.expressions.splice(i, this.expressions.length);
|
||||
code = this.compileNode(o);
|
||||
this.expressions = rest;
|
||||
}
|
||||
post = this.compileNode(o);
|
||||
scope = o.scope;
|
||||
if (scope.hasAssignments(this)) {
|
||||
code = "" + this.tab + "var " + (multident(scope.compiledAssignments(), this.tab)) + ";\n" + code;
|
||||
}
|
||||
if (!o.globals && o.scope.hasDeclarations(this)) {
|
||||
code = "" + this.tab + "var " + (scope.compiledDeclarations()) + ";\n" + code;
|
||||
code += "" + this.tab + "var " + (scope.compiledDeclarations()) + ";\n";
|
||||
}
|
||||
return code;
|
||||
if (scope.hasAssignments(this)) {
|
||||
code += "" + this.tab + "var " + (multident(scope.compiledAssignments(), this.tab)) + ";\n";
|
||||
}
|
||||
return code + post;
|
||||
};
|
||||
Expressions.wrap = function(nodes) {
|
||||
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
|
||||
|
||||
@@ -230,14 +230,22 @@ exports.Expressions = class Expressions extends Base
|
||||
# Compile the expressions body for the contents of a function, with
|
||||
# declarations of all inner variables pushed up to the top.
|
||||
compileWithDeclarations: (o) ->
|
||||
code = post = ''
|
||||
for exp, i in @expressions
|
||||
exp = exp.unwrap()
|
||||
break unless exp instanceof Comment or exp instanceof Literal
|
||||
o.level = LEVEL_TOP
|
||||
code = @compileNode o
|
||||
if i
|
||||
rest = @expressions.splice i, @expressions.length
|
||||
code = @compileNode o
|
||||
@expressions = rest
|
||||
post = @compileNode o
|
||||
{scope} = o
|
||||
if scope.hasAssignments this
|
||||
code = "#{@tab}var #{ multident scope.compiledAssignments(), @tab };\n#{code}"
|
||||
if not o.globals and o.scope.hasDeclarations this
|
||||
code = "#{@tab}var #{ scope.compiledDeclarations() };\n#{code}"
|
||||
code
|
||||
code += "#{@tab}var #{ scope.compiledDeclarations() };\n"
|
||||
if scope.hasAssignments this
|
||||
code += "#{@tab}var #{ multident scope.compiledAssignments(), @tab };\n"
|
||||
code + post
|
||||
|
||||
# Wrap up the given nodes as an **Expressions**, unless it already happens
|
||||
# to be one.
|
||||
|
||||
Reference in New Issue
Block a user