Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

caught a close call bug with object comprehensions and not specifying…

… the value
  • Loading branch information...
commit 24408c785af1cc9c71f707a8b8cdd35b7ea54e4b 1 parent bb5bf7f
@jashkenas jashkenas authored
View
2  documentation/coffee/expressions_comprehension.coffee
@@ -1,3 +1,3 @@
# The first ten global properties.
-globals: (name for property, name in window)[0...10]
+globals: (name for name ino window)[0...10]
View
9 documentation/js/expressions_comprehension.js
@@ -1,11 +1,12 @@
(function(){
- var __a, __b, globals, name, property;
+ var __a, __b, globals, name;
// The first ten global properties.
globals = ((function() {
__b = []; __a = window;
- for (name=0; name<__a.length; name++) {
- property = __a[name];
- __b.push(name);
+ for (name in __a) {
+ if (__a.hasOwnProperty(name)) {
+ __b.push(name);
+ }
}
return __b;
})()).slice(0, 10);
View
20 index.html
@@ -927,24 +927,26 @@ <h2 id="installation">Installation and Usage</h2>
</p>
<div class='code'><pre class="idle"><span class="Comment"><span class="Comment">#</span> The first ten global properties.</span>
-<span class="FunctionName">globals</span><span class="Keyword">:</span> (name <span class="Keyword">for</span> property, name <span class="Keyword">in</span> window)[<span class="Number">0</span>...<span class="Number">10</span>]
-</pre><pre class="idle"><span class="Storage">var</span> __a, __b, globals, name, property;
+<span class="FunctionName">globals</span><span class="Keyword">:</span> (name <span class="Keyword">for</span> name <span class="Keyword">ino</span> window)[<span class="Number">0</span>...<span class="Number">10</span>]
+</pre><pre class="idle"><span class="Storage">var</span> __a, __b, globals, name;
<span class="Comment"><span class="Comment">//</span> The first ten global properties.</span>
globals <span class="Keyword">=</span> ((<span class="Storage">function</span>() {
__b <span class="Keyword">=</span> []; __a <span class="Keyword">=</span> <span class="LibraryClassType">window</span>;
- <span class="Keyword">for</span> (name<span class="Keyword">=</span><span class="Number">0</span>; name<span class="Keyword">&lt;</span>__a.<span class="LibraryConstant">length</span>; name<span class="Keyword">++</span>) {
- property <span class="Keyword">=</span> __a[name];
- __b.<span class="LibraryFunction">push</span>(name);
+ <span class="Keyword">for</span> (name <span class="Keyword">in</span> __a) {
+ <span class="Keyword">if</span> (__a.hasOwnProperty(name)) {
+ __b.<span class="LibraryFunction">push</span>(name);
+ }
}
<span class="Keyword">return</span> __b;
})()).<span class="LibraryFunction">slice</span>(<span class="Number">0</span>, <span class="Number">10</span>);
-</pre><button onclick='javascript: var __a, __b, globals, name, property;
+</pre><button onclick='javascript: var __a, __b, globals, name;
// The first ten global properties.
globals = ((function() {
__b = []; __a = window;
- for (name=0; name<__a.length; name++) {
- property = __a[name];
- __b.push(name);
+ for (name in __a) {
+ if (__a.hasOwnProperty(name)) {
+ __b.push(name);
+ }
}
return __b;
})()).slice(0, 10);
View
8 lib/coffee_script/nodes.rb
@@ -643,7 +643,7 @@ def compile_node(o)
range = @source.is_a?(ValueNode) && @source.literal.is_a?(RangeNode) && @source.properties.empty?
source = range ? @source.literal : @source
scope = o[:scope]
- scope.find(@name)
+ name_found = @name && scope.find(@name)
index_found = @index && scope.find(@index)
body_dent = idt(1)
svar = scope.free_variable
@@ -658,8 +658,8 @@ def compile_node(o)
index_var = nil
source_part = "#{svar} = #{source.compile(o)};\n#{idt}"
for_part = "#{ivar}=0; #{ivar}<#{svar}.length; #{ivar}++"
- for_part = "#{@index} in #{svar}" if @object
- var_part = "#{body_dent}#{@name} = #{svar}[#{ivar}];\n"
+ for_part = "#{ivar} in #{svar}" if @object
+ var_part = @name ? "#{body_dent}#{@name} = #{svar}[#{ivar}];\n" : ''
end
body = @body
set_result = rvar ? "#{idt}#{rvar} = []; " : idt
@@ -680,7 +680,7 @@ def compile_node(o)
end
if @object
body = Expressions.wrap(IfNode.new(
- CallNode.new(ValueNode.new(LiteralNode.wrap(svar), [AccessorNode.new(Value.new('hasOwnProperty'))]), [LiteralNode.wrap(@index)]),
+ CallNode.new(ValueNode.new(LiteralNode.wrap(svar), [AccessorNode.new(Value.new('hasOwnProperty'))]), [LiteralNode.wrap(ivar)]),
Expressions.wrap(body),
nil,
{:statement => true}
View
2  test/fixtures/execution/test_array_comprehension.coffee
@@ -5,7 +5,7 @@ print(results.join(',') is '2,18')
obj: {one: 1, two: 2, three: 3}
-names: key + '!' for key, value ino obj
+names: key + '!' for key ino obj
odds: key + '!' for key, value ino obj when value % 2 isnt 0
print(names.join(' ') is "one! two! three!")
Please sign in to comment.
Something went wrong with that request. Please try again.