fixed misdentation in a?.b = c

This commit is contained in:
satyr
2010-10-11 12:31:54 +09:00
parent acafb1b53a
commit fffa01933d
2 changed files with 31 additions and 43 deletions

View File

@@ -974,7 +974,7 @@
return this.variable instanceof Value; return this.variable instanceof Value;
}; };
Assign.prototype.compileNode = function(o) { Assign.prototype.compileNode = function(o) {
var isValue, match, name, node, stmt, top, val; var ifn, isValue, match, name, stmt, top, val;
if (isValue = this.isValue()) { if (isValue = this.isValue()) {
if (this.variable.isArray() || this.variable.isObject()) { if (this.variable.isArray() || this.variable.isObject()) {
return this.compilePatternMatch(o); return this.compilePatternMatch(o);
@@ -982,8 +982,9 @@
if (this.variable.isSplice()) { if (this.variable.isSplice()) {
return this.compileSplice(o); return this.compileSplice(o);
} }
if (node = Value.unfoldSoak(o, this, 'variable')) { if (ifn = Value.unfoldSoak(o, this, 'variable')) {
return node.compile(o); delete o.top;
return ifn.compile(o);
} }
} }
top = del(o, 'top'); top = del(o, 'top');
@@ -1792,9 +1793,9 @@
var _ref2; var _ref2;
return (((_ref2 = this.elseBody) != null) ? _ref2.unwrap() : undefined); return (((_ref2 = this.elseBody) != null) ? _ref2.unwrap() : undefined);
}; };
If.prototype.addElse = function(elseBody, statement) { If.prototype.addElse = function(elseBody) {
if (this.isChain) { if (this.isChain) {
this.elseBodyNode().addElse(elseBody, statement); this.elseBodyNode().addElse(elseBody);
} else { } else {
this.isChain = elseBody instanceof If; this.isChain = elseBody instanceof If;
this.elseBody = this.ensureExpressions(elseBody); this.elseBody = this.ensureExpressions(elseBody);
@@ -1802,22 +1803,12 @@
return this; return this;
}; };
If.prototype.isStatement = function(o) { If.prototype.isStatement = function(o) {
return this.statement || (this.statement = !!((o && o.top) || this.bodyNode().isStatement(o) || (this.elseBody && this.elseBodyNode().isStatement(o)))); var _ref2;
return this.statement || (this.statement = ((o != null) ? o.top : undefined) || this.bodyNode().isStatement(o) || (((_ref2 = this.elseBodyNode()) != null) ? _ref2.isStatement(o) : undefined));
}; };
If.prototype.compileCondition = function(o) { If.prototype.compileCondition = function(o) {
var _i, _len, _result, cond, conditions; this.condition.parenthetical = true;
conditions = flatten([this.condition]); return this.condition.compile(o);
if (conditions.length === 1) {
conditions[0].parenthetical = true;
}
return (function() {
_result = [];
for (_i = 0, _len = conditions.length; _i < _len; _i++) {
cond = conditions[_i];
_result.push(cond.compile(o));
}
return _result;
})().join(' || ');
}; };
If.prototype.compileNode = function(o) { If.prototype.compileNode = function(o) {
return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o); return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o);
@@ -1835,24 +1826,23 @@
return node instanceof Expressions ? node : new Expressions([node]); return node instanceof Expressions ? node : new Expressions([node]);
}; };
If.prototype.compileStatement = function(o) { If.prototype.compileStatement = function(o) {
var body, child, comDent, condO, elsePart, ifDent, ifPart, top; var child, condO, ifPart, top;
top = del(o, 'top'); top = del(o, 'top');
child = del(o, 'chainChild'); child = del(o, 'chainChild');
condO = merge(o); condO = merge(o);
o.indent = this.idt(1); o.indent = this.idt(1);
o.top = true; o.top = true;
ifDent = child || (top && !this.isStatement(o)) ? '' : this.idt(); ifPart = ("if (" + (this.compileCondition(condO)) + ") {\n" + (this.body.compile(o)) + "\n" + (this.tab) + "}");
comDent = child ? this.idt() : ''; if (!child) {
body = this.body.compile(o); ifPart = this.tab + ifPart;
ifPart = ("" + ifDent + "if (" + (this.compileCondition(condO)) + ") {\n" + body + "\n" + (this.tab) + "}"); }
if (!this.elseBody) { if (!this.elseBody) {
return ifPart; return ifPart;
} }
elsePart = this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, { return ifPart + (this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, {
indent: this.idt(), indent: this.tab,
chainChild: true chainChild: true
})) : (" else {\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}"); })) : (" else {\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}"));
return "" + ifPart + elsePart;
}; };
If.prototype.compileExpression = function(o) { If.prototype.compileExpression = function(o) {
var code, elsePart, ifPart; var code, elsePart, ifPart;

View File

@@ -863,7 +863,9 @@ exports.Assign = class Assign extends Base
if isValue = @isValue() if isValue = @isValue()
return @compilePatternMatch(o) if @variable.isArray() or @variable.isObject() return @compilePatternMatch(o) if @variable.isArray() or @variable.isObject()
return @compileSplice(o) if @variable.isSplice() return @compileSplice(o) if @variable.isSplice()
return node.compile o if node = Value.unfoldSoak o, this, 'variable' if ifn = Value.unfoldSoak o, this, 'variable'
delete o.top
return ifn.compile o
top = del o, 'top' top = del o, 'top'
stmt = del o, 'asStatement' stmt = del o, 'asStatement'
name = @variable.compile(o) name = @variable.compile(o)
@@ -1526,23 +1528,22 @@ exports.If = class If extends Base
elseBodyNode: -> @elseBody?.unwrap() elseBodyNode: -> @elseBody?.unwrap()
# Rewrite a chain of **Ifs** to add a default case as the final *else*. # Rewrite a chain of **Ifs** to add a default case as the final *else*.
addElse: (elseBody, statement) -> addElse: (elseBody) ->
if @isChain if @isChain
@elseBodyNode().addElse elseBody, statement @elseBodyNode().addElse elseBody
else else
@isChain = elseBody instanceof If @isChain = elseBody instanceof If
@elseBody = @ensureExpressions elseBody @elseBody = @ensureExpressions elseBody
this this
# The **If** only compiles into a statement if either of its bodies needs # The **If** only compiles into a statement if either of its bodies needs
# to be a statement. Otherwise a conditional operator is safe. # to be a statement. Otherwise a conditional operator is safe.
isStatement: (o) -> isStatement: (o) ->
@statement or= !!((o and o.top) or @bodyNode().isStatement(o) or (@elseBody and @elseBodyNode().isStatement(o))) @statement or= o?.top or @bodyNode().isStatement(o) or @elseBodyNode()?.isStatement(o)
compileCondition: (o) -> compileCondition: (o) ->
conditions = flatten [@condition] @condition.parenthetical = yes
conditions[0].parenthetical = yes if conditions.length is 1 @condition.compile o
(cond.compile(o) for cond in conditions).join(' || ')
compileNode: (o) -> compileNode: (o) ->
if @isStatement o then @compileStatement o else @compileExpression o if @isStatement o then @compileStatement o else @compileExpression o
@@ -1566,16 +1567,13 @@ exports.If = class If extends Base
condO = merge o condO = merge o
o.indent = @idt 1 o.indent = @idt 1
o.top = true o.top = true
ifDent = if child or (top and not @isStatement(o)) then '' else @idt() ifPart = "if (#{ @compileCondition condO }) {\n#{ @body.compile o }\n#{@tab}}"
comDent = if child then @idt() else '' ifPart = @tab + ifPart unless child
body = @body.compile(o)
ifPart = "#{ifDent}if (#{ @compileCondition(condO) }) {\n#{body}\n#{@tab}}"
return ifPart unless @elseBody return ifPart unless @elseBody
elsePart = if @isChain ifPart + if @isChain
' else ' + @elseBodyNode().compile(merge(o, {indent: @idt(), chainChild: true})) ' else ' + @elseBodyNode().compile merge o, indent: @tab, chainChild: true
else else
" else {\n#{ @elseBody.compile(o) }\n#{@tab}}" " else {\n#{ @elseBody.compile(o) }\n#{@tab}}"
"#{ifPart}#{elsePart}"
# Compile the If as a conditional operator. # Compile the If as a conditional operator.
compileExpression: (o) -> compileExpression: (o) ->