mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
adding weepy's suggestion to use (for .. in) for array comprehensions, which means that they're now object comprehensions as well
This commit is contained in:
@@ -325,10 +325,10 @@ module CoffeeScript
|
|||||||
@exclusive ? '>' : '>='
|
@exclusive ? '>' : '>='
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile(o, fv, tv)
|
def compile(o, fv)
|
||||||
fvv, tvv = @from.compile(o), @to.compile(o)
|
fvv, tvv = @from.compile(o), @to.compile(o)
|
||||||
vars = "#{fv}=#{fvv}, #{tv}=#{tvv}"
|
vars = "#{fv}=#{fvv}"
|
||||||
compare = "(#{fvv} <= #{tvv} ? #{fv} #{less_operator} #{tv} : #{fv} #{greater_operator} #{tv})"
|
compare = "(#{fvv} <= #{tvv} ? #{fv} #{less_operator} #{tvv} : #{fv} #{greater_operator} #{tvv})"
|
||||||
incr = "(#{fvv} <= #{tvv} ? #{fv} += 1 : #{fv} -= 1)"
|
incr = "(#{fvv} <= #{tvv} ? #{fv} += 1 : #{fv} -= 1)"
|
||||||
"#{vars}; #{compare}; #{incr}"
|
"#{vars}; #{compare}; #{incr}"
|
||||||
end
|
end
|
||||||
@@ -554,22 +554,22 @@ module CoffeeScript
|
|||||||
name_found = scope.find(@name)
|
name_found = scope.find(@name)
|
||||||
index_found = @index && scope.find(@index)
|
index_found = @index && scope.find(@index)
|
||||||
svar = scope.free_variable
|
svar = scope.free_variable
|
||||||
ivar = range ? name : scope.free_variable
|
ivar = range ? name : @index ? @index : scope.free_variable
|
||||||
lvar = scope.free_variable
|
|
||||||
rvar = scope.free_variable
|
rvar = scope.free_variable
|
||||||
index_name = @index ? @index : nil
|
|
||||||
if range
|
if range
|
||||||
source_part = ''
|
body_dent = o[:indent] + TAB
|
||||||
var_part = ''
|
source_part, var_part = '', '', ''
|
||||||
index_part = ''
|
pre_cond, post_cond = '', ''
|
||||||
index_var = scope.free_variable
|
index_var = scope.free_variable
|
||||||
for_part = "#{index_var}=0, #{@source.compile(o, ivar, lvar)}, #{index_var}++"
|
for_part = "#{index_var}=0, #{@source.compile(o, ivar)}, #{index_var}++"
|
||||||
else
|
else
|
||||||
index_var = nil
|
index_var = nil
|
||||||
|
body_dent = o[:indent] + TAB + TAB
|
||||||
source_part = "#{svar} = #{@source.compile(o)};\n#{o[:indent]}"
|
source_part = "#{svar} = #{@source.compile(o)};\n#{o[:indent]}"
|
||||||
for_part = "#{ivar}=0, #{lvar}=#{svar}.length; #{ivar}<#{lvar}; #{ivar}++"
|
for_part = "#{ivar} in #{svar}"
|
||||||
var_part = "\n#{o[:indent] + TAB}#{@name} = #{svar}[#{ivar}];"
|
pre_cond = "\n#{o[:indent] + TAB}if (#{svar}.hasOwnProperty(#{ivar})) {"
|
||||||
index_part = @index ? "\n#{o[:indent] + TAB}#{index_name} = #{ivar};" : ''
|
var_part = "\n#{body_dent}#{@name} = #{svar}[#{ivar}];"
|
||||||
|
post_cond = "\n#{o[:indent] + TAB}}"
|
||||||
end
|
end
|
||||||
body = @body
|
body = @body
|
||||||
suffix = ';'
|
suffix = ';'
|
||||||
@@ -593,9 +593,8 @@ module CoffeeScript
|
|||||||
end
|
end
|
||||||
|
|
||||||
return_result = "\n#{o[:indent]}#{return_result};"
|
return_result = "\n#{o[:indent]}#{return_result};"
|
||||||
indent = o[:indent] + TAB
|
body = body.compile(o.merge(:indent => body_dent))
|
||||||
body = body.compile(o.merge(:indent => indent))
|
write("#{source_part}#{set_result}for (#{for_part}) {#{pre_cond}#{var_part}\n#{body_dent}#{save_result}#{body}#{suffix}#{post_cond}\n#{o[:indent]}}#{return_result}")
|
||||||
write("#{source_part}#{set_result}for (#{for_part}) {#{var_part}#{index_part}\n#{indent}#{save_result}#{body}#{suffix}\n#{o[:indent]}}#{return_result}")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -694,7 +693,7 @@ module CoffeeScript
|
|||||||
@else_body ? @else_body << eb : @else_body = eb
|
@else_body ? @else_body << eb : @else_body = eb
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def force_statement
|
def force_statement
|
||||||
@tags[:statement] = true
|
@tags[:statement] = true
|
||||||
self
|
self
|
||||||
|
|||||||
29
test/fixtures/generation/each.js
vendored
29
test/fixtures/generation/each.js
vendored
@@ -3,28 +3,31 @@
|
|||||||
// The cornerstone, an each implementation.
|
// The cornerstone, an each implementation.
|
||||||
// Handles objects implementing forEach, arrays, and raw objects.
|
// Handles objects implementing forEach, arrays, and raw objects.
|
||||||
_.each = function each(obj, iterator, context) {
|
_.each = function each(obj, iterator, context) {
|
||||||
var __a, __b, __c, __d, __e, __f, __g, __h, i, index, item, key;
|
var __a, __b, __c, __d, __e, i, index, item, key;
|
||||||
index = 0;
|
index = 0;
|
||||||
try {
|
try {
|
||||||
if (obj.forEach) {
|
if (obj.forEach) {
|
||||||
obj.forEach(iterator, context);
|
obj.forEach(iterator, context);
|
||||||
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
||||||
__a = obj;
|
__a = obj;
|
||||||
__d = [];
|
__b = [];
|
||||||
for (__b=0, __c=__a.length; __b<__c; __b++) {
|
for (i in __a) {
|
||||||
item = __a[__b];
|
if (__a.hasOwnProperty(i)) {
|
||||||
i = __b;
|
item = __a[i];
|
||||||
__d[__b] = iterator.call(context, item, i, obj);
|
__b[i] = iterator.call(context, item, i, obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
__d;
|
__b;
|
||||||
} else {
|
} else {
|
||||||
__e = _.keys(obj);
|
__c = _.keys(obj);
|
||||||
__h = [];
|
__e = [];
|
||||||
for (__f=0, __g=__e.length; __f<__g; __f++) {
|
for (__d in __c) {
|
||||||
key = __e[__f];
|
if (__c.hasOwnProperty(__d)) {
|
||||||
__h[__f] = iterator.call(context, obj[key], key, obj);
|
key = __c[__d];
|
||||||
|
__e[__d] = iterator.call(context, obj[key], key, obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
__h;
|
__e;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e !== breaker) {
|
if (e !== breaker) {
|
||||||
|
|||||||
29
test/fixtures/generation/each_no_wrap.js
vendored
29
test/fixtures/generation/each_no_wrap.js
vendored
@@ -2,28 +2,31 @@
|
|||||||
// The cornerstone, an each implementation.
|
// The cornerstone, an each implementation.
|
||||||
// Handles objects implementing forEach, arrays, and raw objects.
|
// Handles objects implementing forEach, arrays, and raw objects.
|
||||||
_.each = function each(obj, iterator, context) {
|
_.each = function each(obj, iterator, context) {
|
||||||
var __a, __b, __c, __d, __e, __f, __g, __h, i, index, item, key;
|
var __a, __b, __c, __d, __e, i, index, item, key;
|
||||||
index = 0;
|
index = 0;
|
||||||
try {
|
try {
|
||||||
if (obj.forEach) {
|
if (obj.forEach) {
|
||||||
obj.forEach(iterator, context);
|
obj.forEach(iterator, context);
|
||||||
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
||||||
__a = obj;
|
__a = obj;
|
||||||
__d = [];
|
__b = [];
|
||||||
for (__b=0, __c=__a.length; __b<__c; __b++) {
|
for (i in __a) {
|
||||||
item = __a[__b];
|
if (__a.hasOwnProperty(i)) {
|
||||||
i = __b;
|
item = __a[i];
|
||||||
__d[__b] = iterator.call(context, item, i, obj);
|
__b[i] = iterator.call(context, item, i, obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
__d;
|
__b;
|
||||||
} else {
|
} else {
|
||||||
__e = _.keys(obj);
|
__c = _.keys(obj);
|
||||||
__h = [];
|
__e = [];
|
||||||
for (__f=0, __g=__e.length; __f<__g; __f++) {
|
for (__d in __c) {
|
||||||
key = __e[__f];
|
if (__c.hasOwnProperty(__d)) {
|
||||||
__h[__f] = iterator.call(context, obj[key], key, obj);
|
key = __c[__d];
|
||||||
|
__e[__d] = iterator.call(context, obj[key], key, obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
__h;
|
__e;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e !== breaker) {
|
if (e !== breaker) {
|
||||||
|
|||||||
Reference in New Issue
Block a user