1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

expressions nested in expressions made for some indentation issues -- statements are now responsible for their own leading indentation

This commit is contained in:
Jeremy Ashkenas 2009-12-31 16:50:46 -05:00
parent bfd7455db4
commit f299972713
8 changed files with 102 additions and 78 deletions

View file

@ -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;
})();

View file

@ -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;
})();

View file

@ -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();
})();

View file

@ -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();

View file

@ -111,12 +111,15 @@ math <span class="Keyword">=</span> {
};
<span class="Comment"><span class="Comment">//</span> Array comprehensions:</span>
__a <span class="Keyword">=</span> list;
__d <span class="Keyword">=</span> [];
<span class="Keyword">for</span> (__b<span class="Keyword">=</span><span class="Number">0</span>, __c<span class="Keyword">=</span>__a.<span class="LibraryConstant">length</span>; __b<span class="Keyword">&lt;</span>__c; __b<span class="Keyword">++</span>) {
num <span class="Keyword">=</span> __a[__b];
__d[__b] <span class="Keyword">=</span> math.cube(num);
__c <span class="Keyword">=</span> [];
<span class="Keyword">for</span> (__b <span class="Keyword">in</span> __a) {
<span class="Keyword">if</span> (__a.hasOwnProperty(__b)) {
num <span class="Keyword">=</span> __a[__b];
__d <span class="Keyword">=</span> math.cube(num);
__c.<span class="LibraryFunction">push</span>(__d);
}
}
cubed_list <span class="Keyword">=</span> __d;
cubed_list <span class="Keyword">=</span> __c;
</pre><button onclick='javascript: var __a, __b, __c, __d, cubed_list, list, math, num, number, opposite_day, square;
// Assignment:
number = 42;
@ -141,12 +144,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;
;alert(cubed_list);'>run: cubed_list</button><br class='clear' /></div>
<h2 id="installation">Installation and Usage</h2>
@ -373,7 +379,7 @@ num <span class="Keyword">=</span> <span class="Number">1</span>;
change_numbers <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">change_numbers</span>() {
<span class="Storage">var</span> new_num;
num <span class="Keyword">=</span> <span class="Number">2</span>;
<span class="Keyword">return</span> (new_num <span class="Keyword">=</span> <span class="Number">3</span>);
<span class="Keyword">return</span> (new_num <span class="Keyword">=</span> <span class="Number">3</span>);
};
new_num <span class="Keyword">=</span> change_numbers();
</pre><button onclick='javascript: var change_numbers, new_num, num;
@ -381,7 +387,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();
;alert(new_num);'>run: new_num</button><br class='clear' /></div>
@ -579,24 +585,28 @@ lunch<span class="Keyword">:</span> food.eat() <span class="Keyword">for</span>
<span class="Comment"><span class="Comment">#</span> Zebra-stripe a table.</span>
highlight(row) <span class="Keyword">for</span> row, i <span class="Keyword">in</span> table <span class="Keyword">when</span> i <span class="Keyword">%</span> <span class="Number">2</span> <span class="Keyword">is</span> <span class="Number">0</span>
</pre><pre class="idle"><span class="Storage">var</span> __a, __b, __c, __d, __e, __f, __g, __h, food, i, lunch, row;
</pre><pre class="idle"><span class="Storage">var</span> __a, __b, __c, __d, __e, __f, __g, food, i, lunch, row;
<span class="Comment"><span class="Comment">//</span> Eat lunch.</span>
__a <span class="Keyword">=</span> [<span class="String"><span class="String">'</span>toast<span class="String">'</span></span>, <span class="String"><span class="String">'</span>cheese<span class="String">'</span></span>, <span class="String"><span class="String">'</span>wine<span class="String">'</span></span>];
__d <span class="Keyword">=</span> [];
<span class="Keyword">for</span> (__b<span class="Keyword">=</span><span class="Number">0</span>, __c<span class="Keyword">=</span>__a.<span class="LibraryConstant">length</span>; __b<span class="Keyword">&lt;</span>__c; __b<span class="Keyword">++</span>) {
food <span class="Keyword">=</span> __a[__b];
__d[__b] <span class="Keyword">=</span> food.eat();
__c <span class="Keyword">=</span> [];
<span class="Keyword">for</span> (__b <span class="Keyword">in</span> __a) {
<span class="Keyword">if</span> (__a.hasOwnProperty(__b)) {
food <span class="Keyword">=</span> __a[__b];
__d <span class="Keyword">=</span> food.eat();
__c.<span class="LibraryFunction">push</span>(__d);
}
}
lunch <span class="Keyword">=</span> __d;
lunch <span class="Keyword">=</span> __c;
<span class="Comment"><span class="Comment">//</span> Zebra-stripe a table.</span>
__e <span class="Keyword">=</span> table;
__h <span class="Keyword">=</span> [];
<span class="Keyword">for</span> (__f<span class="Keyword">=</span><span class="Number">0</span>, __g<span class="Keyword">=</span>__e.<span class="LibraryConstant">length</span>; __f<span class="Keyword">&lt;</span>__g; __f<span class="Keyword">++</span>) {
row <span class="Keyword">=</span> __e[__f];
i <span class="Keyword">=</span> __f;
__h[__f] <span class="Keyword">=</span> i <span class="Keyword">%</span> <span class="Number">2</span> <span class="Keyword">===</span> <span class="Number">0</span> ? highlight(row) : <span class="BuiltInConstant">null</span>;
__f <span class="Keyword">=</span> [];
<span class="Keyword">for</span> (i <span class="Keyword">in</span> __e) {
<span class="Keyword">if</span> (__e.hasOwnProperty(i)) {
row <span class="Keyword">=</span> __e[i];
i <span class="Keyword">%</span> <span class="Number">2</span> <span class="Keyword">===</span> <span class="Number">0</span> ? highlight(row) : <span class="BuiltInConstant">null</span>;
}
}
__h;
__f;
</pre><br class='clear' /></div>
<p>
If you're not iterating over an actual array, you can use a range to
@ -678,7 +688,7 @@ Animal <span class="Keyword">=</span> <span class="Storage">function</span> <spa
<span class="Keyword">return</span> <span class="LibraryFunction">alert</span>(<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span> moved <span class="String">&quot;</span></span> <span class="Keyword">+</span> meters <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span>m.<span class="String">&quot;</span></span>);
};
Snake <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">Snake</span>(<span class="FunctionArgument">name</span>) {
<span class="Keyword">return</span> (<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name);
<span class="Keyword">return</span> (<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name);
};
Snake.__superClass__ <span class="Keyword">=</span> Animal.<span class="LibraryConstant">prototype</span>;
<span class="LibraryClassType">Snake</span>.<span class="LibraryConstant">prototype</span> = <span class="Keyword">new</span> <span class="TypeName">Animal</span>();
@ -688,7 +698,7 @@ Snake.__superClass__ <span class="Keyword">=</span> Animal.<span class="LibraryC
<span class="Keyword">return</span> Snake.__superClass__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">5</span>);
};
Horse <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">Horse</span>(<span class="FunctionArgument">name</span>) {
<span class="Keyword">return</span> (<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name);
<span class="Keyword">return</span> (<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name);
};
Horse.__superClass__ <span class="Keyword">=</span> Animal.<span class="LibraryConstant">prototype</span>;
<span class="LibraryClassType">Horse</span>.<span class="LibraryConstant">prototype</span> = <span class="Keyword">new</span> <span class="TypeName">Animal</span>();
@ -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();

View file

@ -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

View file

@ -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) {

View file

@ -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) {