From fffa01933df9e6453cc2618b9281d25f2a3f8c8c Mon Sep 17 00:00:00 2001 From: satyr Date: Mon, 11 Oct 2010 12:31:54 +0900 Subject: [PATCH] fixed misdentation in `a?.b = c` --- lib/nodes.js | 46 ++++++++++++++++++---------------------------- src/nodes.coffee | 28 +++++++++++++--------------- 2 files changed, 31 insertions(+), 43 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index 4e837909..79442cce 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -974,7 +974,7 @@ return this.variable instanceof Value; }; 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 (this.variable.isArray() || this.variable.isObject()) { return this.compilePatternMatch(o); @@ -982,8 +982,9 @@ if (this.variable.isSplice()) { return this.compileSplice(o); } - if (node = Value.unfoldSoak(o, this, 'variable')) { - return node.compile(o); + if (ifn = Value.unfoldSoak(o, this, 'variable')) { + delete o.top; + return ifn.compile(o); } } top = del(o, 'top'); @@ -1792,9 +1793,9 @@ var _ref2; return (((_ref2 = this.elseBody) != null) ? _ref2.unwrap() : undefined); }; - If.prototype.addElse = function(elseBody, statement) { + If.prototype.addElse = function(elseBody) { if (this.isChain) { - this.elseBodyNode().addElse(elseBody, statement); + this.elseBodyNode().addElse(elseBody); } else { this.isChain = elseBody instanceof If; this.elseBody = this.ensureExpressions(elseBody); @@ -1802,22 +1803,12 @@ return this; }; 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) { - var _i, _len, _result, cond, conditions; - conditions = flatten([this.condition]); - 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(' || '); + this.condition.parenthetical = true; + return this.condition.compile(o); }; If.prototype.compileNode = function(o) { return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o); @@ -1835,24 +1826,23 @@ return node instanceof Expressions ? node : new Expressions([node]); }; If.prototype.compileStatement = function(o) { - var body, child, comDent, condO, elsePart, ifDent, ifPart, top; + var child, condO, ifPart, top; top = del(o, 'top'); child = del(o, 'chainChild'); condO = merge(o); o.indent = this.idt(1); o.top = true; - ifDent = child || (top && !this.isStatement(o)) ? '' : this.idt(); - comDent = child ? this.idt() : ''; - body = this.body.compile(o); - ifPart = ("" + ifDent + "if (" + (this.compileCondition(condO)) + ") {\n" + body + "\n" + (this.tab) + "}"); + ifPart = ("if (" + (this.compileCondition(condO)) + ") {\n" + (this.body.compile(o)) + "\n" + (this.tab) + "}"); + if (!child) { + ifPart = this.tab + ifPart; + } if (!this.elseBody) { return ifPart; } - elsePart = this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, { - indent: this.idt(), + return ifPart + (this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, { + indent: this.tab, chainChild: true - })) : (" else {\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}"); - return "" + ifPart + elsePart; + })) : (" else {\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}")); }; If.prototype.compileExpression = function(o) { var code, elsePart, ifPart; diff --git a/src/nodes.coffee b/src/nodes.coffee index dafe5cec..31e17a56 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -863,7 +863,9 @@ exports.Assign = class Assign extends Base if isValue = @isValue() return @compilePatternMatch(o) if @variable.isArray() or @variable.isObject() 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' stmt = del o, 'asStatement' name = @variable.compile(o) @@ -1526,23 +1528,22 @@ exports.If = class If extends Base elseBodyNode: -> @elseBody?.unwrap() # Rewrite a chain of **Ifs** to add a default case as the final *else*. - addElse: (elseBody, statement) -> + addElse: (elseBody) -> if @isChain - @elseBodyNode().addElse elseBody, statement + @elseBodyNode().addElse elseBody else - @isChain = elseBody instanceof If + @isChain = elseBody instanceof If @elseBody = @ensureExpressions elseBody this # The **If** only compiles into a statement if either of its bodies needs # to be a statement. Otherwise a conditional operator is safe. 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) -> - conditions = flatten [@condition] - conditions[0].parenthetical = yes if conditions.length is 1 - (cond.compile(o) for cond in conditions).join(' || ') + @condition.parenthetical = yes + @condition.compile o compileNode: (o) -> if @isStatement o then @compileStatement o else @compileExpression o @@ -1566,16 +1567,13 @@ exports.If = class If extends Base condO = merge o o.indent = @idt 1 o.top = true - ifDent = if child or (top and not @isStatement(o)) then '' else @idt() - comDent = if child then @idt() else '' - body = @body.compile(o) - ifPart = "#{ifDent}if (#{ @compileCondition(condO) }) {\n#{body}\n#{@tab}}" + ifPart = "if (#{ @compileCondition condO }) {\n#{ @body.compile o }\n#{@tab}}" + ifPart = @tab + ifPart unless child return ifPart unless @elseBody - elsePart = if @isChain - ' else ' + @elseBodyNode().compile(merge(o, {indent: @idt(), chainChild: true})) + ifPart + if @isChain + ' else ' + @elseBodyNode().compile merge o, indent: @tab, chainChild: true else " else {\n#{ @elseBody.compile(o) }\n#{@tab}}" - "#{ifPart}#{elsePart}" # Compile the If as a conditional operator. compileExpression: (o) ->