Skip to content

Commit 5000ed9

Browse files
committed
improve each_pair and each destructuring
1 parent 5dd0204 commit 5000ed9

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

lib/ruby2js/filter/functions.rb

+18-7
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,24 @@ def on_block(node)
534534
call.children[0].children[0], node.children[2])
535535

536536
elsif
537-
[:each, :each_value].include? method and
538-
node.children[1].children.length == 1
537+
[:each, :each_value].include? method
539538
then
540539
if es2015
541-
process node.updated(:for_of,
542-
[s(:lvasgn, node.children[1].children[0].children[0]),
543-
node.children[0].children[0], node.children[2]])
540+
if node.children[1].children.length > 1
541+
process node.updated(:for_of,
542+
[s(:mlhs, *node.children[1].children.map {|child|
543+
s(:lvasgn, child.children[0])}),
544+
node.children[0].children[0], node.children[2]])
545+
elsif node.children[1].children[0].type == :mlhs
546+
process node.updated(:for_of,
547+
[s(:mlhs, *node.children[1].children[0].children.map {|child|
548+
s(:lvasgn, child.children[0])}),
549+
node.children[0].children[0], node.children[2]])
550+
else
551+
process node.updated(:for_of,
552+
[s(:lvasgn, node.children[1].children[0].children[0]),
553+
node.children[0].children[0], node.children[2]])
554+
end
544555
elsif method == :each
545556
process node.updated(nil, [s(:send, call.children[0],
546557
:forEach), *node.children[1..2]])
@@ -572,8 +583,8 @@ def on_block(node)
572583
if es2017
573584
# Object.entries(a).forEach(([key, value]) => {})
574585
process node.updated(nil, [s(:send, s(:send,
575-
s(:const, nil, :Object), :entries, call.children[0]), :forEach),
576-
s(:args, s(:mlhs, *node.children[1].children)), node.children[2]])
586+
s(:const, nil, :Object), :entries, call.children[0]), :each),
587+
node.children[1], node.children[2]])
577588
else
578589
# Object.keys(a).forEach(function(key) {var value = a[key]; ...})
579590
process node.updated(nil, [s(:send, s(:send,

spec/es2015_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def to_js_fn(string)
7676
must_equal 'for (let v of a) {x += v}'
7777
end
7878

79+
it "should convert array.each multiple to a for...of" do
80+
to_js_fn( 'a.each {|(v,w)| x+=v}' ).
81+
must_equal 'for (let [v, w] of a) {x += v}'
82+
end
83+
7984
it "should handle conditional returns in an each block" do
8085
to_js_fn( 'a.each {|i| return i if true}' ).
8186
must_equal 'for (let i of a) {if (true) return i}'

spec/es2017_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def to_js_fn(string)
2525
describe :Hash do
2626
it "should convert hash.each_pair" do
2727
to_js_fn( 'h.each_pair {|k,v| x+=v}' ).
28-
must_equal 'Object.entries(h).forEach(([k, v]) => {x += v})'
28+
must_equal 'for (let [k, v] of Object.entries(h)) {x += v}'
2929
end
3030

3131
it "should convert hash.entries()" do

0 commit comments

Comments
 (0)