diff --git a/documentation/js/array_comprehensions.js b/documentation/js/array_comprehensions.js index 636ccb49..edc73121 100644 --- a/documentation/js/array_comprehensions.js +++ b/documentation/js/array_comprehensions.js @@ -1,20 +1,24 @@ (function(){ - var __a, __b, __c, __d, __e, __f, __g, __h, food, i, lunch, row; + var __a, __b, __c, __d, __e, __f, __g, food, i, lunch, row; // Eat lunch. __a = ['toast', 'cheese', 'wine']; - __d = []; - for (__b=0, __c=__a.length; __b<__c; __b++) { - food = __a[__b]; - __d[__b] = food.eat(); + __c = []; + for (__b in __a) { + if (__a.hasOwnProperty(__b)) { + food = __a[__b]; + __d = food.eat(); + __c.push(__d); + } } - lunch = __d; + lunch = __c; // Zebra-stripe a table. __e = table; - __h = []; - for (__f=0, __g=__e.length; __f<__g; __f++) { - row = __e[__f]; - i = __f; - __h[__f] = i % 2 === 0 ? highlight(row) : null; + __f = []; + for (i in __e) { + if (__e.hasOwnProperty(i)) { + row = __e[i]; + i % 2 === 0 ? highlight(row) : null; + } } - __h; + __f; })(); \ No newline at end of file diff --git a/documentation/js/overview.js b/documentation/js/overview.js index dbcd550d..36379ca7 100644 --- a/documentation/js/overview.js +++ b/documentation/js/overview.js @@ -23,10 +23,13 @@ }; // Array comprehensions: __a = list; - __d = []; - for (__b=0, __c=__a.length; __b<__c; __b++) { - num = __a[__b]; - __d[__b] = math.cube(num); + __c = []; + for (__b in __a) { + if (__a.hasOwnProperty(__b)) { + num = __a[__b]; + __d = math.cube(num); + __c.push(__d); + } } - cubed_list = __d; + cubed_list = __c; })(); \ No newline at end of file diff --git a/documentation/js/scope.js b/documentation/js/scope.js index 23b0c8e9..38d5e939 100644 --- a/documentation/js/scope.js +++ b/documentation/js/scope.js @@ -4,7 +4,7 @@ change_numbers = function change_numbers() { var new_num; num = 2; - return (new_num = 3); +return (new_num = 3); }; new_num = change_numbers(); })(); \ No newline at end of file diff --git a/documentation/js/super.js b/documentation/js/super.js index 11135d23..040d6d2e 100644 --- a/documentation/js/super.js +++ b/documentation/js/super.js @@ -6,7 +6,7 @@ return alert(this.name + " moved " + meters + "m."); }; Snake = function Snake(name) { - return (this.name = name); +return (this.name = name); }; Snake.__superClass__ = Animal.prototype; Snake.prototype = new Animal(); @@ -16,7 +16,7 @@ return Snake.__superClass__.move.call(this, 5); }; Horse = function Horse(name) { - return (this.name = name); +return (this.name = name); }; Horse.__superClass__ = Animal.prototype; Horse.prototype = new Animal(); diff --git a/index.html b/index.html index c413e81b..55c4e961 100644 --- a/index.html +++ b/index.html @@ -111,12 +111,15 @@ math = { }; // Array comprehensions: __a = list; -__d = []; -for (__b=0, __c=__a.length; __b<__c; __b++) { - num = __a[__b]; - __d[__b] = math.cube(num); +__c = []; +for (__b in __a) { + if (__a.hasOwnProperty(__b)) { + num = __a[__b]; + __d = math.cube(num); + __c.push(__d); + } } -cubed_list = __d; +cubed_list = __c;

Installation and Usage

@@ -373,7 +379,7 @@ num = 1; change_numbers = function change_numbers() { var new_num; num = 2; - return (new_num = 3); +return (new_num = 3); }; new_num = change_numbers();
@@ -579,24 +585,28 @@ lunch: food.eat() for # Zebra-stripe a table. highlight(row) for row, i in table when i % 2 is 0 -
var __a, __b, __c, __d, __e, __f, __g, __h, food, i, lunch, row;
+
var __a, __b, __c, __d, __e, __f, __g, food, i, lunch, row;
 // Eat lunch.
 __a = ['toast', 'cheese', 'wine'];
-__d = [];
-for (__b=0, __c=__a.length; __b<__c; __b++) {
-  food = __a[__b];
-  __d[__b] = food.eat();
+__c = [];
+for (__b in __a) {
+  if (__a.hasOwnProperty(__b)) {
+    food = __a[__b];
+    __d = food.eat();
+    __c.push(__d);
+  }
 }
-lunch = __d;
+lunch = __c;
 // Zebra-stripe a table.
 __e = table;
-__h = [];
-for (__f=0, __g=__e.length; __f<__g; __f++) {
-  row = __e[__f];
-  i = __f;
-  __h[__f] = i % 2 === 0 ? highlight(row) : null;
+__f = [];
+for (i in __e) {
+  if (__e.hasOwnProperty(i)) {
+    row = __e[i];
+    i % 2 === 0 ? highlight(row) : null;
+  }
 }
-__h;
+__f;
 

If you're not iterating over an actual array, you can use a range to @@ -678,7 +688,7 @@ Animal = function return alert(this.name + " moved " + meters + "m."); }; Snake = function Snake(name) { - return (this.name = name); +return (this.name = name); }; Snake.__superClass__ = Animal.prototype; Snake.prototype = new Animal(); @@ -688,7 +698,7 @@ Snake.__superClass__ = Animal.return Snake.__superClass__.move.call(this, 5); }; Horse = function Horse(name) { - return (this.name = name); +return (this.name = name); }; Horse.__superClass__ = Animal.prototype; Horse.prototype = new Animal(); @@ -708,7 +718,7 @@ Animal.prototype.move = function move(meters) { return alert(this.name + " moved " + meters + "m."); }; Snake = function Snake(name) { - return (this.name = name); +return (this.name = name); }; Snake.__superClass__ = Animal.prototype; Snake.prototype = new Animal(); @@ -718,7 +728,7 @@ Snake.prototype.move = function move() { return Snake.__superClass__.move.call(this, 5); }; Horse = function Horse(name) { - return (this.name = name); +return (this.name = name); }; Horse.__superClass__ = Animal.prototype; Horse.prototype = new Animal(); diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index fed7e70f..060ae24d 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -94,21 +94,21 @@ module CoffeeScript if last?(node) && (o[:return] || o[:assign]) if o[:return] if node.statement? || node.custom_return? - "#{o[:indent]}#{node.compile(o)}#{node.line_ending}" + "#{node.compile(o)}#{node.line_ending}" else o.delete(:return) "#{o[:indent]}return #{node.compile(o)}#{node.line_ending}" end elsif o[:assign] if node.statement? || node.custom_assign? - "#{o[:indent]}#{node.compile(o)}#{node.line_ending}" + "#{node.compile(o)}#{node.line_ending}" else "#{o[:indent]}#{AssignNode.new(o[:assign], node).compile(o)};" end end else o.delete(:return) and o.delete(:assign) - indent = node.statement? ? '' : o[:indent] + indent = node.unwrap.statement? ? '' : o[:indent] "#{indent}#{node.compile(o)}#{node.line_ending}" end end @@ -140,7 +140,8 @@ module CoffeeScript def compile(o={}) o = super(o) - write(@value.to_s) + indent = statement? ? o[:indent] : '' + write(indent + @value.to_s) end end @@ -163,7 +164,7 @@ module CoffeeScript o = super(o) return write(@expression.compile(o.merge(:return => true))) if @expression.custom_return? compiled = @expression.compile(o) - write(@expression.statement? ? "#{compiled}\n#{indent}return null" : "return #{compiled}") + write(@expression.statement? ? "#{compiled}\n#{o[:indent]}return null" : "#{o[:indent]}return #{compiled}") end end @@ -530,7 +531,7 @@ module CoffeeScript o.delete(:return) indent = o[:indent] + TAB cond = @condition.compile(o) - write("while (#{cond}) {\n#{@body.compile(o.merge(:indent => indent))}\n#{o[:indent]}}") + write("#{o[:indent]}while (#{cond}) {\n#{@body.compile(o.merge(:indent => indent))}\n#{o[:indent]}}") end end @@ -572,7 +573,7 @@ module CoffeeScript else index_var = nil body_dent = o[:indent] + TAB + TAB - source_part = "#{svar} = #{@source.compile(o)};\n#{o[:indent]}" + source_part = "#{o[:indent]}#{svar} = #{@source.compile(o)};\n#{o[:indent]}" for_part = "#{ivar} in #{svar}" pre_cond = "\n#{o[:indent] + TAB}if (#{svar}.hasOwnProperty(#{ivar})) {" var_part = "\n#{body_dent}#{@name} = #{svar}[#{ivar}];" @@ -593,7 +594,7 @@ module CoffeeScript o.delete(:return) body = IfNode.new(@filter, body, nil, :statement => true) if @filter elsif @filter - body = IfNode.new(@filter, @body) + body = Expressions.wrap(IfNode.new(@filter, @body)) end return_result = "\n#{o[:indent]}#{return_result};" @@ -625,7 +626,7 @@ module CoffeeScript error_part = @error ? " (#{@error}) " : ' ' catch_part = @recovery && " catch#{error_part}{\n#{@recovery.compile(o)}\n#{indent}}" finally_part = @finally && " finally {\n#{@finally.compile(o.merge(:assign => nil, :return => nil))}\n#{indent}}" - write("try {\n#{@try.compile(o)}\n#{indent}}#{catch_part}#{finally_part}") + write("#{indent}try {\n#{@try.compile(o)}\n#{indent}}#{catch_part}#{finally_part}") end end @@ -641,7 +642,7 @@ module CoffeeScript def compile(o={}) o = super(o) - write("throw #{@expression.compile(o)}") + write("#{o[:indent]}throw #{@expression.compile(o)}") end end @@ -748,14 +749,16 @@ module CoffeeScript # force sub-else bodies into statement form. def compile_statement(o) indent = o[:indent] + child = o.delete(:chain_child) cond_o = o.dup cond_o.delete(:assign) cond_o.delete(:return) o[:indent] += TAB - if_part = "if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{indent}}" + if_dent = child ? '' : indent + if_part = "#{if_dent}if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{indent}}" return if_part unless @else_body else_part = chain? ? - " else #{@else_body.compile(o.merge(:indent => indent))}" : + " else #{@else_body.compile(o.merge(:indent => indent, :chain_child => true))}" : " else {\n#{Expressions.wrap(@else_body).compile(o)}\n#{indent}}" if_part + else_part end diff --git a/test/fixtures/generation/each.js b/test/fixtures/generation/each.js index f61587a7..9e3decfd 100644 --- a/test/fixtures/generation/each.js +++ b/test/fixtures/generation/each.js @@ -3,7 +3,7 @@ // The cornerstone, an each implementation. // Handles objects implementing forEach, arrays, and raw objects. _.each = function each(obj, iterator, context) { - var __a, __b, __c, __d, __e, i, index, item, key; + var __a, __b, __c, __d, __e, __f, __g, i, index, item, key; index = 0; try { if (obj.forEach) { @@ -14,20 +14,22 @@ for (i in __a) { if (__a.hasOwnProperty(i)) { item = __a[i]; - __b.push(iterator.call(context, item, i, obj)); + __c = iterator.call(context, item, i, obj); + __b.push(__c); } } __b; } else { - __c = _.keys(obj); - __e = []; - for (__d in __c) { - if (__c.hasOwnProperty(__d)) { - key = __c[__d]; - __e.push(iterator.call(context, obj[key], key, obj)); + __d = _.keys(obj); + __f = []; + for (__e in __d) { + if (__d.hasOwnProperty(__e)) { + key = __d[__e]; + __g = iterator.call(context, obj[key], key, obj); + __f.push(__g); } } - __e; + __f; } } catch (e) { if (e !== breaker) { diff --git a/test/fixtures/generation/each_no_wrap.js b/test/fixtures/generation/each_no_wrap.js index b6f6ad7d..4fb7dede 100644 --- a/test/fixtures/generation/each_no_wrap.js +++ b/test/fixtures/generation/each_no_wrap.js @@ -2,7 +2,7 @@ // The cornerstone, an each implementation. // Handles objects implementing forEach, arrays, and raw objects. _.each = function each(obj, iterator, context) { - var __a, __b, __c, __d, __e, i, index, item, key; + var __a, __b, __c, __d, __e, __f, __g, i, index, item, key; index = 0; try { if (obj.forEach) { @@ -13,20 +13,22 @@ _.each = function each(obj, iterator, context) { for (i in __a) { if (__a.hasOwnProperty(i)) { item = __a[i]; - __b.push(iterator.call(context, item, i, obj)); + __c = iterator.call(context, item, i, obj); + __b.push(__c); } } __b; } else { - __c = _.keys(obj); - __e = []; - for (__d in __c) { - if (__c.hasOwnProperty(__d)) { - key = __c[__d]; - __e.push(iterator.call(context, obj[key], key, obj)); + __d = _.keys(obj); + __f = []; + for (__e in __d) { + if (__d.hasOwnProperty(__e)) { + key = __d[__e]; + __g = iterator.call(context, obj[key], key, obj); + __f.push(__g); } } - __e; + __f; } } catch (e) { if (e !== breaker) {