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

better formatting for top-level closured-values.

This commit is contained in:
Jeremy Ashkenas 2010-07-10 09:49:01 -04:00
parent 7a16db9ad3
commit 6f32fe27f8
2 changed files with 31 additions and 17 deletions

View file

@ -285,19 +285,18 @@
return true;
};
ReturnNode.prototype.children = ['expression'];
ReturnNode.prototype.topSensitive = function() {
return true;
};
ReturnNode.prototype.makeReturn = function() {
return this;
};
ReturnNode.prototype.compileNode = function(o) {
ReturnNode.prototype.compile = function(o) {
var expr;
expr = this.expression.makeReturn();
if (!(expr instanceof ReturnNode)) {
return expr.compile(o);
}
del(o, 'top');
return ReturnNode.__superClass__.compile.call(this, o);
};
ReturnNode.prototype.compileNode = function(o) {
if (this.expression.isStatement()) {
o.asStatement = true;
}
@ -354,6 +353,9 @@
}
return node === this;
};
ValueNode.prototype.compile = function(o) {
return !o.top || this.properties.length ? ValueNode.__superClass__.compile.call(this, o) : this.base.compile(o);
};
ValueNode.prototype.compileNode = function(o) {
var _b, _c, baseline, complete, i, only, op, part, prop, props, temp;
only = del(o, 'onlyFirst');
@ -1310,11 +1312,15 @@
ParentheticalNode.prototype.makeReturn = function() {
return this.expression.makeReturn();
};
ParentheticalNode.prototype.topSensitive = function() {
return true;
};
ParentheticalNode.prototype.compileNode = function(o) {
var code, l;
var code, l, top;
top = del(o, 'top');
code = this.expression.compile(o);
if (this.isStatement()) {
return code;
return (top ? ("" + this.tab + code + ";") : code);
}
l = code.length;
if (code.substr(l - 1, 1) === ';') {

View file

@ -266,16 +266,15 @@ exports.ReturnNode: class ReturnNode extends BaseNode
constructor: (expression) ->
@expression: expression
topSensitive: ->
true
makeReturn: ->
this
compileNode: (o) ->
compile: (o) ->
expr: @expression.makeReturn()
return expr.compile(o) unless expr instanceof ReturnNode
del o, 'top'
return expr.compile o unless expr instanceof ReturnNode
super o
compileNode: (o) ->
o.asStatement: true if @expression.isStatement()
"${@tab}return ${@expression.compile(o)};"
@ -336,15 +335,19 @@ exports.ValueNode: class ValueNode extends BaseNode
while node instanceof CallNode then node: node.variable
node is this
# Override compile to unwrap the value when possible.
compile: (o) ->
if not o.top or @properties.length then super(o) else @base.compile(o)
# We compile a value to JavaScript by compiling and joining each property.
# Things get much more insteresting if the chain of properties has *soak*
# operators `?.` interspersed. Then we have to take care not to accidentally
# evaluate a anything twice when building the soak chain.
compileNode: (o) ->
only: del(o, 'onlyFirst')
op: del(o, 'operation')
only: del o, 'onlyFirst'
op: del o, 'operation'
props: if only then @properties[0...@properties.length - 1] else @properties
o.chainRoot: or this
o.chainRoot: or this
baseline: @base.compile o
baseline: "($baseline)" if @hasProperties() and (@base instanceof ObjectNode or @isNumber())
complete: @last: baseline
@ -1175,9 +1178,14 @@ exports.ParentheticalNode: class ParentheticalNode extends BaseNode
makeReturn: ->
@expression.makeReturn()
topSensitive: ->
yes
compileNode: (o) ->
top: del o, 'top'
code: @expression.compile(o)
return code if @isStatement()
if @isStatement()
return (if top then "$@tab$code;" else code)
l: code.length
code: code.substr(o, l-1) if code.substr(l-1, 1) is ';'
if @expression instanceof AssignNode then code else "($code)"