diff --git a/lib/nodes.js b/lib/nodes.js index 4a8f3b6e..4e837909 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1,5 +1,5 @@ (function() { - var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, Literal, NO, NUMBER, ObjectLiteral, Op, Param, Parens, Push, Range, Return, SIMPLENUM, Scope, Slice, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, _ref, compact, del, ends, flatten, include, indexOf, last, merge, starts, utility; + var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, Literal, NO, NUMBER, ObjectLiteral, Op, Param, Parens, Push, Range, Return, SIMPLENUM, Scope, Slice, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, _ref, compact, del, ends, flatten, include, last, merge, starts, utility; var __extends = function(child, parent) { var ctor = function() {}; ctor.prototype = parent.prototype; @@ -9,7 +9,7 @@ child.__super__ = parent.prototype; }; Scope = require('./scope').Scope; - _ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, merge = _ref.merge, del = _ref.del, include = _ref.include, indexOf = _ref.indexOf, starts = _ref.starts, ends = _ref.ends, last = _ref.last; + _ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, merge = _ref.merge, del = _ref.del, include = _ref.include, starts = _ref.starts, ends = _ref.ends, last = _ref.last; YES = function() { return true; }; @@ -843,24 +843,22 @@ return Splat.compileSplattedArray(this.objects, o); }; ArrayLiteral.prototype.compileNode = function(o) { - var _len, _ref2, code, i, obj, objects; + var _i, _len, _len2, _ref2, _ref3, code, i, obj, objects; o.indent = this.idt(1); - objects = []; - for (i = 0, _len = (_ref2 = this.objects).length; i < _len; i++) { - obj = _ref2[i]; - code = obj.compile(o); + for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; _i++) { + obj = _ref2[_i]; if (obj instanceof Splat) { return this.compileSplatLiteral(o); - } else if (obj instanceof Comment) { - objects.push("\n" + code + "\n" + (o.indent)); - } else if (i === this.objects.length - 1) { - objects.push(code); - } else { - objects.push("" + code + ", "); } } + objects = []; + for (i = 0, _len2 = (_ref3 = this.objects).length; i < _len2; i++) { + obj = _ref3[i]; + code = obj.compile(o); + objects.push(obj instanceof Comment ? ("\n" + code + "\n" + (o.indent)) : (i === this.objects.length - 1 ? code : code + ', ')); + } objects = objects.join(''); - return indexOf(objects, '\n') >= 0 ? ("[\n" + (this.idt(1)) + objects + "\n" + (this.tab) + "]") : ("[" + objects + "]"); + return 0 < objects.indexOf('\n') ? ("[\n" + (o.indent) + objects + "\n" + (this.tab) + "]") : ("[" + objects + "]"); }; return ArrayLiteral; })(); @@ -919,7 +917,7 @@ func.name = className; func.body.push(new Return(new Literal('this'))); variable = new Value(variable); - variable.namespaced = include(func.name, '.'); + variable.namespaced = 0 < className.indexOf('.'); constructor = func; continue; } @@ -1383,7 +1381,7 @@ if (this.isChainable() && this.first.unwrap() instanceof Op && this.first.unwrap().isChainable()) { return this.compileChain(o); } - if (indexOf(this.ASSIGNMENT, this.operator) >= 0) { + if (include(this.ASSIGNMENT, this.operator)) { return this.compileAssignment(o); } if (this.isUnary()) { @@ -1426,12 +1424,9 @@ }; Op.prototype.compileUnary = function(o) { var parts, space; - space = indexOf(this.PREFIX_OPERATORS, this.operator) >= 0 ? ' ' : ''; + space = include(this.PREFIX_OPERATORS, this.operator) ? ' ' : ''; parts = [this.operator, space, this.first.compile(o)]; - if (this.flip) { - parts = parts.reverse(); - } - return parts.join(''); + return (this.flip ? parts.reverse() : parts).join(''); }; return Op; })(); diff --git a/src/nodes.coffee b/src/nodes.coffee index e677828c..dafe5cec 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -6,7 +6,7 @@ {Scope} = require './scope' # Import the helpers we plan to use. -{compact, flatten, merge, del, include, indexOf, starts, ends, last} = require './helpers' +{compact, flatten, merge, del, include, starts, ends, last} = require './helpers' # Constant functions for nodes that don't need customization. YES = -> yes @@ -738,20 +738,21 @@ exports.ArrayLiteral = class ArrayLiteral extends Base compileNode: (o) -> o.indent = @idt 1 + for obj in @objects when obj instanceof Splat + return @compileSplatLiteral o objects = [] for obj, i in @objects - code = obj.compile(o) - if obj instanceof Splat - return @compileSplatLiteral o - else if obj instanceof Comment - objects.push "\n#{code}\n#{o.indent}" + code = obj.compile o + objects.push (if obj instanceof Comment + "\n#{code}\n#{o.indent}" else if i is @objects.length - 1 - objects.push code + code else - objects.push "#{code}, " - objects = objects.join('') - if indexOf(objects, '\n') >= 0 - "[\n#{@idt(1)}#{objects}\n#{@tab}]" + code + ', ' + ) + objects = objects.join '' + if 0 < objects.indexOf '\n' + "[\n#{o.indent}#{objects}\n#{@tab}]" else "[#{objects}]" @@ -808,7 +809,7 @@ exports.Class = class Class extends Base func.name = className func.body.push new Return new Literal 'this' variable = new Value variable - variable.namespaced = include func.name, '.' + variable.namespaced = 0 < className.indexOf '.' constructor = func continue if func instanceof Code and func.bound @@ -1204,7 +1205,7 @@ exports.Op = class Op extends Base compileNode: (o) -> return @compileChain(o) if @isChainable() and @first.unwrap() instanceof Op and @first.unwrap().isChainable() - return @compileAssignment(o) if indexOf(@ASSIGNMENT, @operator) >= 0 + return @compileAssignment(o) if include @ASSIGNMENT, @operator return @compileUnary(o) if @isUnary() return @compileExistence(o) if @operator is '?' @first = new Parens @first if @first instanceof Op and @first.isMutator() @@ -1241,10 +1242,9 @@ exports.Op = class Op extends Base # Compile a unary **Op**. compileUnary: (o) -> - space = if indexOf(@PREFIX_OPERATORS, @operator) >= 0 then ' ' else '' + space = if include @PREFIX_OPERATORS, @operator then ' ' else '' parts = [@operator, space, @first.compile(o)] - parts = parts.reverse() if @flip - parts.join('') + (if @flip then parts.reverse() else parts).join '' #### In exports.In = class In extends Base