Skip to content

Commit de18d2b

Browse files
committed
support Ruby 3.1 shorthand hash syntax
Fixes #152
1 parent f189692 commit de18d2b

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

lib/ruby2js/converter/match.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
module Ruby2JS
22
class Converter
33
handle :match_pattern do |value, name|
4-
parse @ast.updated(:lvasgn, [name.children.first, value]), @state
4+
if name.type == :match_var
5+
parse @ast.updated(:lvasgn, [name.children.first, value]), @state
6+
elsif name.type == :hash_pattern and name.children.all? {|child| child.type == :match_var}
7+
if es2015
8+
put 'let { '
9+
put name.children.map {|child| child.children[0].to_s}.join(', ')
10+
put ' } = '
11+
parse value
12+
else
13+
raise Error.new("destructuring requires es2015")
14+
end
15+
else
16+
raise Error.new("complex match patterns are not supported")
17+
end
518
end
619
end
720
end

spec/es2015_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ def to_js_fn(string)
114114
it "should implement max with spread operators" do
115115
to_js_fn( 'a.max()' ).must_equal('Math.max(...a)')
116116
end
117+
118+
unless (RUBY_VERSION.split('.').map(&:to_i) <=> [3, 0, 0]) == -1
119+
it "should support => operator with simple destructuring" do
120+
to_js('hash => {a:, b:}').must_equal 'let {a, b} = hash'
121+
end
122+
end
117123
end
118124

119125
describe :arguments do

spec/transliteration_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ def to_js( string, opts={} )
975975
end
976976

977977
unless (RUBY_VERSION.split('.').map(&:to_i) <=> [3, 0, 0]) == -1
978-
it "should support => operator" do
978+
it "should support => operator with simple left hand sides" do
979979
to_js('0 => x').must_equal 'var x = 0'
980980
end
981981
end

0 commit comments

Comments
 (0)