We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 11fb0ee commit afa4addCopy full SHA for afa4add
README.md
@@ -236,6 +236,7 @@ the script.
236
* `.last` becomes `[*.length-1]`
237
* `.last(n)` becomes `.slice(*.length-1, *.length)`
238
* `.max` becomes `Math.max.apply(Math)`
239
+ * `.merge` becomes `Object.assign({}, ...)`
240
* `.merge!` becomes `Object.assign()`
241
* `.min` becomes `Math.min.apply(Math)`
242
* `.nil?` becomes `== null`
@@ -566,6 +567,14 @@ conversion is made by the `functions` filter:
566
567
568
* `.each_entry` becomes `Object.entries().forEach`
569
570
+ES2018 support
571
+---
572
+
573
+When option `eslevel: 2018` is provided, the following additional
574
+conversion is made by the `functions` filter:
575
576
+* `.merge` becomes `{...a, ...b}`
577
578
Picking a Ruby to JS mapping tool
579
---
580
demo/ruby2js.rb
@@ -17,6 +17,7 @@
17
# --es2015
18
# --es2016
19
# --es2017
20
+# --es2018
21
# --strict
22
# ---filter filter
23
# -f filter
@@ -95,15 +96,15 @@
95
96
_input type: 'checkbox', name: 'ast', id: 'ast', checked: !!@ast
97
_label 'Show AST', for: 'ast'
98
- _input type: 'checkbox', name: 'es2017', id: 'es2017', checked: !!@es2017
99
- _label 'ES2017', for: 'es2017'
+ _input type: 'checkbox', name: 'es2018', id: 'es2018', checked: !!@es2018
100
+ _label 'ES2018', for: 'es2018'
101
end
102
103
if @ruby
104
_div_? do
105
raise $load_error if $load_error
106
- options[:eslevel] = 2017 if @es2017
107
+ options[:eslevel] = 2018 if @es2018
108
109
ruby = Ruby2JS.convert(@ruby, options)
110
lib/ruby2js.rb
@@ -81,6 +81,10 @@ def es2017
81
@options[:eslevel] >= 2017
82
83
84
+ def es2018
85
+ @options[:eslevel] >= 2018
86
+ end
87
88
def process(node)
89
ast, @ast = @ast, node
90
replacement = super
lib/ruby2js/converter.rb
@@ -138,6 +138,10 @@ def es2017
138
@eslevel >= 2017
139
140
141
142
+ @eslevel >= 2018
143
144
145
@@handlers = []
146
def self.handle(*types, &block)
147
types.each do |type|
lib/ruby2js/converter/hash.rb
@@ -12,10 +12,26 @@ class Converter
12
13
(singleton ? put('{') : puts('{'))
14
15
- pairs.each_with_index do |node, index|
16
- raise Error.new("kwsplat", @ast) if node.type == :kwsplat
-
+ index = 0
+ while pairs.length > 0
+ node = pairs.shift
(singleton ? put(', ') : put(",#@ws")) unless index == 0
+ index += 1
+ if node.type == :kwsplat
+ if es2018
+ if node.children.first.type == :hash
24
+ pairs.unshift *node.children.first.children
25
26
+ else
27
+ puts '...'; parse node.children.first
28
29
30
+ next
31
32
+ raise Error.new("kwsplat", @ast)
33
34
35
36
if not @comments[node].empty?
37
(puts ''; singleton = false) if singleton
lib/ruby2js/es2018.rb
@@ -0,0 +1,5 @@
1
+require 'ruby2js'
2
3
+if Ruby2JS.eslevel_default < 2018
4
+ Ruby2JS.eslevel_default = 2018
5
+end
lib/ruby2js/es2018/strict.rb
@@ -0,0 +1,3 @@
+require 'ruby2js/es2018'
+Ruby2JS.strict_default = true
lib/ruby2js/filter/functions.rb
@@ -35,6 +35,27 @@ def on_send(node)
elsif method == :keys and args.length == 0 and node.is_method?
process S(:send, s(:const, nil, :Object), :keys, target)
38
+ elsif method == :merge
39
+ args.unshift target
40
41
+ if es2015
42
43
+ process S(:hash, *args.map {|arg| s(:kwsplat, arg)})
44
45
+ process S(:send, s(:const, nil, :Object), :assign, s(:hash),
46
+ *args)
47
48
49
+ copy = [s(:gvasgn, :$$, s(:hash))]
50
51
+ s(:send, s(:block, s(:send, nil, :lambda), s(:args),
52
+ s(:begin, *copy, *args.map {|modname|
53
+ s(:for, s(:lvasgn, :$_), modname,
54
+ s(:send, s(:gvar, :$$), :[]=,
55
+ s(:lvar, :$_), s(:send, modname, :[], s(:lvar, :$_))))
56
+ }, s(:return, s(:gvar, :$$)))), :[])
57
58
59
elsif method == :merge!
60
if es2015
61
process S(:send, s(:const, nil, :Object), :assign, target, *args)
spec/es2015_spec.rb
@@ -179,6 +179,10 @@ def to_js_fn(string)
179
to_js( 'a=1; {a:a}' ).must_equal 'let a = 1; {a}'
180
181
182
+ it "should handle merge" do
183
+ to_js_fn( 'a.merge(b)' ).must_equal 'Object.assign({}, a, b)'
184
185
186
it "should handle merge!" do
187
to_js_fn( 'a.merge!(b)' ).must_equal 'Object.assign(a, b)'
188
spec/es2018_spec.rb
@@ -0,0 +1,17 @@
+gem 'minitest'
+require 'minitest/autorun'
+describe "ES2018 support" do
6
+ def to_js_fn(string)
7
+ Ruby2JS.convert(string, eslevel: 2018,
8
+ filters: [Ruby2JS::Filter::Functions]).to_s
9
10
11
+ describe :Hash_Spread do
+ it "should convert merge to Object spread" do
+ to_js_fn( 'a.merge(b)' ).must_equal '{...a, ...b}'
+ to_js_fn( 'a.merge(b: 1)' ).must_equal '{...a, b: 1}'
0 commit comments