diff --git a/lib/nodes.js b/lib/nodes.js index d3dfa21b..1e3bd864 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -70,9 +70,6 @@ } return [src, tmp]; }; - Base.prototype.idt = function(tabs) { - return (this.tab || '') + Array((tabs || 0) + 1).join(TAB); - }; Base.prototype.makeReturn = function() { return new Return(this); }; @@ -637,7 +634,7 @@ } return "" + fun + ".apply(" + ref + ", " + splatArgs + ")"; } - idt = this.idt(1); + idt = this.tab + TAB; return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + this.tab + "})(" + (this.variable.compile(o, LEVEL_LIST)) + ", " + splatArgs + ", function() {})"; }; return Call; @@ -716,7 +713,7 @@ break; } } - o.indent = idt = this.idt(1); + idt = o.indent += TAB; nonComments = ((function() { _ref2 = this.properties; _result = []; @@ -811,7 +808,7 @@ Arr.prototype.children = ['objects']; Arr.prototype.compileNode = function(o) { var code, i, obj, objects, _len, _ref; - o.indent = this.idt(1); + o.indent += TAB; if (code = Splat.compileSplattedArray(o, this.objects)) { return code; } @@ -1097,7 +1094,7 @@ var close, code, comm, exprs, func, i, idt, lit, open, p, param, ref, scope, sharedScope, splats, v, val, vars, wasEmpty, _i, _j, _len, _len2, _len3, _ref, _ref2, _result, _this; sharedScope = del(o, 'sharedScope'); o.scope = scope = sharedScope || new Scope(o.scope, this.body, this); - o.indent = this.idt(1); + o.indent += TAB; delete o.bare; delete o.globals; vars = []; @@ -1157,10 +1154,10 @@ } } comm = this.comment ? this.comment.compile(o) + '\n' : ''; + idt = o.indent; if (this.className) { - o.indent = this.idt(2); + o.indent += TAB; } - idt = this.idt(1); code = this.body.isEmpty() ? '' : "\n" + (this.body.compileWithDeclarations(o)) + "\n"; if (this.className) { open = "(function() {\n" + comm + idt + "function " + this.className + "("; @@ -1317,7 +1314,7 @@ }; While.prototype.compileNode = function(o) { var body, code, rvar, set; - o.indent = this.idt(1); + o.indent += TAB; set = ''; body = this.body; if (body.isEmpty()) { @@ -1540,7 +1537,7 @@ }; Try.prototype.compileNode = function(o) { var catchPart, errorPart; - o.indent = this.idt(1); + o.indent += TAB; errorPart = this.error ? " (" + (this.error.compile(o)) + ") " : ' '; catchPart = this.recovery ? " catch" + errorPart + "{\n" + (this.recovery.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : !(this.ensure || this.recovery) ? ' catch (_e) {}' : void 0; return ("" + this.tab + "try {\n" + (this.attempt.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" + (catchPart || '')) + (this.ensure ? " finally {\n" + (this.ensure.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : ''); @@ -1668,7 +1665,7 @@ index = (_ref2 = this.index) != null ? _ref2.compile(o) : void 0; ivar = !index ? scope.freeVariable('i') : index; varPart = guardPart = defPart = retPart = ''; - idt = this.idt(1); + idt = o.indent + TAB; if (!hasCode) { if (name) { scope.find(name, true); @@ -1838,8 +1835,8 @@ }; Switch.prototype.compileNode = function(o) { var block, body, code, cond, conditions, expr, i, idt1, idt2, _i, _j, _len, _len2, _ref, _ref2, _ref3, _ref4; - idt1 = this.idt(1); - idt2 = o.indent = this.idt(2); + idt1 = o.indent + TAB; + idt2 = o.indent = idt1 + TAB; code = this.tab + ("switch (" + (((_ref = this.subject) != null ? _ref.compile(o, LEVEL_PAREN) : void 0) || false) + ") {\n"); for (i = 0, _len = this.cases.length; i < _len; i++) { _ref2 = this.cases[i], conditions = _ref2[0], block = _ref2[1]; @@ -1934,7 +1931,7 @@ var body, child, cond, ifPart; child = del(o, 'chainChild'); cond = this.condition.compile(o, LEVEL_PAREN); - o.indent = this.idt(1); + o.indent += TAB; body = this.ensureExpressions(this.body).compile(o); if (body) { body = "\n" + body + "\n" + this.tab; diff --git a/src/nodes.coffee b/src/nodes.coffee index 3aaf6e8f..93cdcd66 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -71,10 +71,6 @@ exports.Base = class Base src = "#{ tmp = o.scope.freeVariable name } = #{src}" [src, tmp] - # Convenience method to grab the current indentation level, plus tabbing in. - idt: (tabs) -> - (@tab or '') + Array((tabs or 0) + 1).join TAB - # Construct a node that returns the current node's result. # Note that this is overridden for smarter behavior for # many statement nodes (eg If, For)... @@ -506,7 +502,7 @@ exports.Call = class Call extends Base fun = ref = base.compile o, LEVEL_ACCESS fun += name.compile o if name return "#{fun}.apply(#{ref}, #{splatArgs})" - idt = @idt 1 + idt = @tab + TAB """ (function(func, args, ctor) { #{idt}ctor.prototype = func.prototype; @@ -577,7 +573,7 @@ exports.Obj = class Obj extends Base for prop, i in @properties when (prop.variable or prop).base instanceof Parens rest = @properties.splice i break - o.indent = idt = @idt 1 + idt = o.indent += TAB nonComments = (prop for prop in @properties when prop not instanceof Comment) lastNoncom = last nonComments props = for prop, i in @properties @@ -635,7 +631,7 @@ exports.Arr = class Arr extends Base @objects = objs or [] compileNode: (o) -> - o.indent = @idt 1 + o.indent += TAB return code if code = Splat.compileSplattedArray o, @objects objects = [] for obj, i in @objects @@ -876,7 +872,7 @@ exports.Code = class Code extends Base compileNode: (o) -> sharedScope = del o, 'sharedScope' o.scope = scope = sharedScope or new Scope o.scope, @body, this - o.indent = @idt 1 + o.indent += TAB delete o.bare delete o.globals vars = [] @@ -904,10 +900,9 @@ exports.Code = class Code extends Base @body.makeReturn() unless wasEmpty or @noReturn scope.parameter vars[i] = v.compile o for v, i in vars unless splats comm = if @comment then @comment.compile(o) + '\n' else '' - o.indent = @idt 2 if @className - idt = @idt 1 - code = if @body.isEmpty() then '' - else "\n#{ @body.compileWithDeclarations o }\n" + idt = o.indent + o.indent += TAB if @className + code = if @body.isEmpty() then '' else "\n#{ @body.compileWithDeclarations o }\n" if @className open = "(function() {\n#{comm}#{idt}function #{@className}(" close = "#{ code and idt }}\n#{idt}return #{@className};\n#{@tab}})()" @@ -1021,7 +1016,7 @@ exports.While = class While extends Base # *while* can be used as a part of a larger expression -- while loops may # return an array containing the computed result of each iteration. compileNode: (o) -> - o.indent = @idt 1 + o.indent += TAB set = '' {body} = this if body.isEmpty() @@ -1183,7 +1178,7 @@ exports.Try = class Try extends Base # Compilation is more or less as you would expect -- the *finally* clause # is optional, the *catch* is not. compileNode: (o) -> - o.indent = @idt 1 + o.indent += TAB errorPart = if @error then " (#{ @error.compile o }) " else ' ' catchPart = if @recovery " catch#{errorPart}{\n#{ @recovery.compile o, LEVEL_TOP }\n#{@tab}}" @@ -1311,7 +1306,7 @@ exports.For = class For extends Base index = @index?.compile o ivar = if not index then scope.freeVariable 'i' else index varPart = guardPart = defPart = retPart = '' - idt = @idt 1 + idt = o.indent + TAB unless hasCode scope.find(name, yes) if name scope.find(index, yes) if index @@ -1422,8 +1417,8 @@ exports.Switch = class Switch extends Base this compileNode: (o) -> - idt1 = @idt 1 - idt2 = o.indent = @idt 2 + idt1 = o.indent + TAB + idt2 = o.indent = idt1 + TAB code = @tab + "switch (#{ @subject?.compile(o, LEVEL_PAREN) or false }) {\n" for [conditions, block], i in @cases for cond in flatten [conditions] @@ -1488,7 +1483,7 @@ exports.If = class If extends Base compileStatement: (o) -> child = del o, 'chainChild' cond = @condition.compile o, LEVEL_PAREN - o.indent = @idt 1 + o.indent += TAB body = @ensureExpressions(@body).compile o body = "\n#{body}\n#{@tab}" if body ifPart = "if (#{cond}) {#{body}}"