mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-04-11 03:00:13 -04:00
better formatting for top-level closured-values.
This commit is contained in:
20
lib/nodes.js
20
lib/nodes.js
@@ -285,19 +285,18 @@
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
ReturnNode.prototype.children = ['expression'];
|
ReturnNode.prototype.children = ['expression'];
|
||||||
ReturnNode.prototype.topSensitive = function() {
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
ReturnNode.prototype.makeReturn = function() {
|
ReturnNode.prototype.makeReturn = function() {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
ReturnNode.prototype.compileNode = function(o) {
|
ReturnNode.prototype.compile = function(o) {
|
||||||
var expr;
|
var expr;
|
||||||
expr = this.expression.makeReturn();
|
expr = this.expression.makeReturn();
|
||||||
if (!(expr instanceof ReturnNode)) {
|
if (!(expr instanceof ReturnNode)) {
|
||||||
return expr.compile(o);
|
return expr.compile(o);
|
||||||
}
|
}
|
||||||
del(o, 'top');
|
return ReturnNode.__superClass__.compile.call(this, o);
|
||||||
|
};
|
||||||
|
ReturnNode.prototype.compileNode = function(o) {
|
||||||
if (this.expression.isStatement()) {
|
if (this.expression.isStatement()) {
|
||||||
o.asStatement = true;
|
o.asStatement = true;
|
||||||
}
|
}
|
||||||
@@ -354,6 +353,9 @@
|
|||||||
}
|
}
|
||||||
return node === this;
|
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) {
|
ValueNode.prototype.compileNode = function(o) {
|
||||||
var _b, _c, baseline, complete, i, only, op, part, prop, props, temp;
|
var _b, _c, baseline, complete, i, only, op, part, prop, props, temp;
|
||||||
only = del(o, 'onlyFirst');
|
only = del(o, 'onlyFirst');
|
||||||
@@ -1310,11 +1312,15 @@
|
|||||||
ParentheticalNode.prototype.makeReturn = function() {
|
ParentheticalNode.prototype.makeReturn = function() {
|
||||||
return this.expression.makeReturn();
|
return this.expression.makeReturn();
|
||||||
};
|
};
|
||||||
|
ParentheticalNode.prototype.topSensitive = function() {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
ParentheticalNode.prototype.compileNode = function(o) {
|
ParentheticalNode.prototype.compileNode = function(o) {
|
||||||
var code, l;
|
var code, l, top;
|
||||||
|
top = del(o, 'top');
|
||||||
code = this.expression.compile(o);
|
code = this.expression.compile(o);
|
||||||
if (this.isStatement()) {
|
if (this.isStatement()) {
|
||||||
return code;
|
return (top ? ("" + this.tab + code + ";") : code);
|
||||||
}
|
}
|
||||||
l = code.length;
|
l = code.length;
|
||||||
if (code.substr(l - 1, 1) === ';') {
|
if (code.substr(l - 1, 1) === ';') {
|
||||||
|
|||||||
@@ -266,16 +266,15 @@ exports.ReturnNode: class ReturnNode extends BaseNode
|
|||||||
constructor: (expression) ->
|
constructor: (expression) ->
|
||||||
@expression: expression
|
@expression: expression
|
||||||
|
|
||||||
topSensitive: ->
|
|
||||||
true
|
|
||||||
|
|
||||||
makeReturn: ->
|
makeReturn: ->
|
||||||
this
|
this
|
||||||
|
|
||||||
compileNode: (o) ->
|
compile: (o) ->
|
||||||
expr: @expression.makeReturn()
|
expr: @expression.makeReturn()
|
||||||
return expr.compile(o) unless expr instanceof ReturnNode
|
return expr.compile o unless expr instanceof ReturnNode
|
||||||
del o, 'top'
|
super o
|
||||||
|
|
||||||
|
compileNode: (o) ->
|
||||||
o.asStatement: true if @expression.isStatement()
|
o.asStatement: true if @expression.isStatement()
|
||||||
"${@tab}return ${@expression.compile(o)};"
|
"${@tab}return ${@expression.compile(o)};"
|
||||||
|
|
||||||
@@ -336,15 +335,19 @@ exports.ValueNode: class ValueNode extends BaseNode
|
|||||||
while node instanceof CallNode then node: node.variable
|
while node instanceof CallNode then node: node.variable
|
||||||
node is this
|
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.
|
# We compile a value to JavaScript by compiling and joining each property.
|
||||||
# Things get much more insteresting if the chain of properties has *soak*
|
# Things get much more insteresting if the chain of properties has *soak*
|
||||||
# operators `?.` interspersed. Then we have to take care not to accidentally
|
# operators `?.` interspersed. Then we have to take care not to accidentally
|
||||||
# evaluate a anything twice when building the soak chain.
|
# evaluate a anything twice when building the soak chain.
|
||||||
compileNode: (o) ->
|
compileNode: (o) ->
|
||||||
only: del(o, 'onlyFirst')
|
only: del o, 'onlyFirst'
|
||||||
op: del(o, 'operation')
|
op: del o, 'operation'
|
||||||
props: if only then @properties[0...@properties.length - 1] else @properties
|
props: if only then @properties[0...@properties.length - 1] else @properties
|
||||||
o.chainRoot: or this
|
o.chainRoot: or this
|
||||||
baseline: @base.compile o
|
baseline: @base.compile o
|
||||||
baseline: "($baseline)" if @hasProperties() and (@base instanceof ObjectNode or @isNumber())
|
baseline: "($baseline)" if @hasProperties() and (@base instanceof ObjectNode or @isNumber())
|
||||||
complete: @last: baseline
|
complete: @last: baseline
|
||||||
@@ -1175,9 +1178,14 @@ exports.ParentheticalNode: class ParentheticalNode extends BaseNode
|
|||||||
makeReturn: ->
|
makeReturn: ->
|
||||||
@expression.makeReturn()
|
@expression.makeReturn()
|
||||||
|
|
||||||
|
topSensitive: ->
|
||||||
|
yes
|
||||||
|
|
||||||
compileNode: (o) ->
|
compileNode: (o) ->
|
||||||
|
top: del o, 'top'
|
||||||
code: @expression.compile(o)
|
code: @expression.compile(o)
|
||||||
return code if @isStatement()
|
if @isStatement()
|
||||||
|
return (if top then "$@tab$code;" else code)
|
||||||
l: code.length
|
l: code.length
|
||||||
code: code.substr(o, l-1) if code.substr(l-1, 1) is ';'
|
code: code.substr(o, l-1) if code.substr(l-1, 1) is ';'
|
||||||
if @expression instanceof AssignNode then code else "($code)"
|
if @expression instanceof AssignNode then code else "($code)"
|
||||||
|
|||||||
Reference in New Issue
Block a user