ruby2js 3.3.5 → 3.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ require 'ruby2js'
2
+
3
+ module Ruby2JS
4
+ module Filter
5
+ module ESMMigration
6
+ include SEXP
7
+
8
+ def initialize(*args)
9
+ @esm_include = nil
10
+ super
11
+ end
12
+
13
+ def process(node)
14
+ return super if @esm_include
15
+ @esm_include = Set.new
16
+ @esm_exclude = Set.new
17
+ @esm_export = nil
18
+ result = super
19
+
20
+ esm_walk(result)
21
+
22
+ inventory = (@esm_include - @esm_exclude).to_a.sort
23
+
24
+ if inventory.empty? and not @esm_export
25
+ result
26
+ else
27
+ list = inventory.map do |name|
28
+ if name == "React" and defined? Ruby2JS::Filter::React
29
+ s(:import, "#{name.downcase}", s(:const, nil, name))
30
+ elsif not %w(JSON Object).include? name
31
+ s(:import, "./#{name.downcase}.js", s(:const, nil, name))
32
+ end
33
+ end
34
+
35
+ list.push result
36
+
37
+ if @esm_export
38
+ list.push s(:export, :default, s(:const, nil, @esm_export))
39
+ end
40
+
41
+ s(:begin, *list.compact)
42
+ end
43
+ end
44
+
45
+ # gather constants
46
+ def esm_walk(node)
47
+ # extract ivars and cvars
48
+ if node.type == :const and node.children.first == nil
49
+ @esm_include << node.children.last.to_s
50
+ elsif node.type == :xnode
51
+ name = node.children.first
52
+ @esm_include << name unless name.empty? or name =~ /^[a-z]/
53
+ elsif node.type == :casgn and node.children.first == nil
54
+ @esm_exclude << node.children[1].to_s
55
+ elsif node.type == :class and node.children.first.type == :const
56
+ if node.children.first.children.first == nil
57
+ name = node.children.first.children.last.to_s
58
+ @esm_exclude << name
59
+ @esm_export ||= name
60
+ end
61
+ end
62
+
63
+ # recurse
64
+ node.children.each do |child|
65
+ esm_walk(child) if Parser::AST::Node === child
66
+ end
67
+ end
68
+ end
69
+
70
+ DEFAULTS.push ESMMigration
71
+ end
72
+ end
@@ -0,0 +1,23 @@
1
+ require 'ruby2js'
2
+
3
+ module Ruby2JS
4
+ module Filter
5
+ module Fast_Deep_Equal
6
+ include SEXP
7
+
8
+ SCALAR = [ :float, :int, :nil, :str ]
9
+
10
+ def on_send(node)
11
+ return super unless node.children.length == 3
12
+ left, method, right = node.children
13
+ return super unless method == :==
14
+ return super if SCALAR.include? left.type
15
+ return super if SCALAR.include? right.type
16
+
17
+ node.updated nil, [nil, :$eq, left, right]
18
+ end
19
+ end
20
+
21
+ DEFAULTS.push Fast_Deep_Equal
22
+ end
23
+ end
@@ -528,6 +528,18 @@ module Ruby2JS
528
528
  elsif method == :new and target == s(:const, nil, :Exception)
529
529
  process S(:send, s(:const, nil, :Error), :new, *args)
530
530
 
531
+ elsif method == :block_given? and target == nil and args.length == 0
532
+ process process s(:lvar, "_implicitBlockYield")
533
+
534
+ elsif method == :abs and args.length == 0
535
+ process S(:send, s(:const, nil, :Math), :abs, target)
536
+
537
+ elsif method == :ceil and args.length == 0
538
+ process S(:send, s(:const, nil, :Math), :ceil, target)
539
+
540
+ elsif method == :floor and args.length == 0
541
+ process S(:send, s(:const, nil, :Math), :floor, target)
542
+
531
543
  else
532
544
  super
533
545
  end
@@ -538,7 +550,7 @@ module Ruby2JS
538
550
  method = call.children[1]
539
551
  return super if excluded?(method)
540
552
 
541
- if [:setInterval, :setTimeout].include? method
553
+ if [:setInterval, :setTimeout, :set_interval, :set_timeout].include? method
542
554
  return super unless call.children.first == nil
543
555
  block = process s(:block, s(:send, nil, :proc), *node.children[1..-1])
544
556
  on_send call.updated nil, [*call.children[0..1], block,
@@ -703,6 +715,17 @@ module Ruby2JS
703
715
  process call.updated(nil, [*call.children, s(:block,
704
716
  s(:send, nil, :proc), *node.children[1..-1])])
705
717
 
718
+ elsif method == :yield_self and call.children.length == 2
719
+ process node.updated(:send, [s(:block, s(:send, nil, :proc),
720
+ node.children[1], s(:autoreturn, node.children[2])),
721
+ :[], call.children[0]])
722
+
723
+ elsif method == :tap and call.children.length == 2
724
+ process node.updated(:send, [s(:block, s(:send, nil, :proc),
725
+ node.children[1], s(:begin, node.children[2],
726
+ s(:return, s(:lvar, node.children[1].children[0].children[0])))),
727
+ :[], call.children[0]])
728
+
706
729
  else
707
730
  super
708
731
  end
@@ -1,37 +1,21 @@
1
1
  require 'ruby2js'
2
2
  require 'set'
3
3
 
4
+ Ruby2JS.module_default ||= :cjs
5
+
4
6
  module Ruby2JS
5
7
  module Filter
6
8
  module Node
7
9
  include SEXP
8
10
  extend SEXP
9
11
 
10
- NODE_SETUP = {
11
- child_process: s(:casgn, nil, :child_process,
12
- s(:send, nil, :require, s(:str, "child_process"))),
13
- fs: s(:casgn, nil, :fs, s(:send, nil, :require, s(:str, "fs"))),
14
- ARGV: s(:lvasgn, :ARGV, s(:send, s(:attr,
15
- s(:attr, nil, :process), :argv), :slice, s(:int, 2)))
16
- }
17
-
18
- def initialize(*args)
19
- @node_setup = nil
20
- super
21
- end
12
+ IMPORT_CHILD_PROCESS = s(:import, ['child_process'],
13
+ s(:attr, nil, :child_process))
22
14
 
23
- def process(node)
24
- return super if @node_setup
25
- @node_setup = Set.new
26
- result = super
15
+ IMPORT_FS = s(:import, ['fs'], s(:attr, nil, :fs))
27
16
 
28
- if @node_setup.empty?
29
- result
30
- else
31
- s(:begin, *@node_setup.to_a.map {|token| NODE_SETUP[token]},
32
- result)
33
- end
34
- end
17
+ SETUP_ARGV = s(:lvasgn, :ARGV, s(:send, s(:attr,
18
+ s(:attr, nil, :process), :argv), :slice, s(:int, 2)))
35
19
 
36
20
  def on_send(node)
37
21
  target, method, *args = node.children
@@ -44,7 +28,7 @@ module Ruby2JS
44
28
  s(:send, s(:attr, nil, :process), :exit, *process_all(args));
45
29
 
46
30
  elsif method == :system
47
- @node_setup << :child_process
31
+ prepend_list << IMPORT_CHILD_PROCESS
48
32
 
49
33
  if args.length == 1
50
34
  s(:send, s(:attr, nil, :child_process), :execSync,
@@ -72,38 +56,38 @@ module Ruby2JS
72
56
  target.type == :const and target.children.first == nil
73
57
  then
74
58
  if method == :read and args.length == 1
75
- @node_setup << :fs
59
+ prepend_list << IMPORT_FS
76
60
  s(:send, s(:attr, nil, :fs), :readFileSync, *process_all(args),
77
61
  s(:str, 'utf8'))
78
62
 
79
63
  elsif method == :write and args.length == 2
80
- @node_setup << :fs
64
+ prepend_list << IMPORT_FS
81
65
  S(:send, s(:attr, nil, :fs), :writeFileSync, *process_all(args))
82
66
 
83
67
  elsif target.children.last == :IO
84
68
  super
85
69
 
86
70
  elsif [:exist?, :exists?].include? method and args.length == 1
87
- @node_setup << :fs
71
+ prepend_list << IMPORT_FS
88
72
  S(:send, s(:attr, nil, :fs), :existsSync, process(args.first))
89
73
 
90
74
  elsif method == :readlink and args.length == 1
91
- @node_setup << :fs
75
+ prepend_list << IMPORT_FS
92
76
  S(:send, s(:attr, nil, :fs), :readlinkSync, process(args.first))
93
77
 
94
78
  elsif method == :realpath and args.length == 1
95
- @node_setup << :fs
79
+ prepend_list << IMPORT_FS
96
80
  S(:send, s(:attr, nil, :fs), :realpathSync, process(args.first))
97
81
 
98
82
  elsif method == :rename and args.length == 2
99
- @node_setup << :fs
83
+ prepend_list << IMPORT_FS
100
84
  S(:send, s(:attr, nil, :fs), :renameSync, *process_all(args))
101
85
 
102
86
  elsif \
103
87
  [:chmod, :lchmod].include? method and
104
88
  args.length > 1 and args.first.type == :int
105
89
  then
106
- @node_setup << :fs
90
+ prepend_list << IMPORT_FS
107
91
 
108
92
  S(:begin, *args[1..-1].map{|file|
109
93
  S(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
@@ -114,7 +98,7 @@ module Ruby2JS
114
98
  [:chown, :lchown].include? method and args.length > 2 and
115
99
  args[0].type == :int and args[1].type == :int
116
100
  then
117
- @node_setup << :fs
101
+ prepend_list << IMPORT_FS
118
102
 
119
103
  S(:begin, *args[2..-1].map{|file|
120
104
  s(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
@@ -122,24 +106,24 @@ module Ruby2JS
122
106
  })
123
107
 
124
108
  elsif [:ln, :link].include? method and args.length == 2
125
- @node_setup << :fs
109
+ prepend_list << IMPORT_FS
126
110
  s(:send, s(:attr, nil, :fs), :linkSync, *process_all(args))
127
111
 
128
112
  elsif method == :symlink and args.length == 2
129
- @node_setup << :fs
113
+ prepend_list << IMPORT_FS
130
114
  s(:send, s(:attr, nil, :fs), :symlinkSync, *process_all(args))
131
115
 
132
116
  elsif method == :truncate and args.length == 2
133
- @node_setup << :fs
117
+ prepend_list << IMPORT_FS
134
118
  s(:send, s(:attr, nil, :fs), :truncateSync, *process_all(args))
135
119
 
136
120
  elsif [:stat, :lstat].include? method and args.length == 1
137
- @node_setup << :fs
121
+ prepend_list << IMPORT_FS
138
122
  s(:send, s(:attr, nil, :fs), method.to_s + 'Sync',
139
123
  process(args.first))
140
124
 
141
125
  elsif method == :unlink and args.length == 1
142
- @node_setup << :fs
126
+ prepend_list << IMPORT_FS
143
127
  S(:begin, *args.map{|file|
144
128
  S(:send, s(:attr, nil, :fs), :unlinkSync, process(file))
145
129
  })
@@ -162,15 +146,15 @@ module Ruby2JS
162
146
  end
163
147
 
164
148
  if [:cp, :copy].include? method and args.length == 2
165
- @node_setup << :fs
149
+ prepend_list << IMPORT_FS
166
150
  s(:send, s(:attr, nil, :fs), :copyFileSync, *process_all(args))
167
151
 
168
152
  elsif [:mv, :move].include? method and args.length == 2
169
- @node_setup << :fs
153
+ prepend_list << IMPORT_FS
170
154
  S(:send, s(:attr, nil, :fs), :renameSync, *process_all(args))
171
155
 
172
156
  elsif method == :mkdir and args.length == 1
173
- @node_setup << :fs
157
+ prepend_list << IMPORT_FS
174
158
  S(:begin, *list[args.last].map {|file|
175
159
  s(:send, s(:attr, nil, :fs), :mkdirSync, process(file))
176
160
  })
@@ -182,21 +166,21 @@ module Ruby2JS
182
166
  s(:send, s(:attr, nil, :process), :cwd)
183
167
 
184
168
  elsif method == :rmdir and args.length == 1
185
- @node_setup << :fs
169
+ prepend_list << IMPORT_FS
186
170
  S(:begin, *list[args.last].map {|file|
187
171
  s(:send, s(:attr, nil, :fs), :rmdirSync, process(file))
188
172
  })
189
173
 
190
174
  elsif method == :ln and args.length == 2
191
- @node_setup << :fs
175
+ prepend_list << IMPORT_FS
192
176
  s(:send, s(:attr, nil, :fs), :linkSync, *process_all(args))
193
177
 
194
178
  elsif method == :ln_s and args.length == 2
195
- @node_setup << :fs
179
+ prepend_list << IMPORT_FS
196
180
  s(:send, s(:attr, nil, :fs), :symlinkSync, *process_all(args))
197
181
 
198
182
  elsif method == :rm and args.length == 1
199
- @node_setup << :fs
183
+ prepend_list << IMPORT_FS
200
184
  S(:begin, *list[args.last].map {|file|
201
185
  s(:send, s(:attr, nil, :fs), :unlinkSync, process(file))
202
186
  })
@@ -204,7 +188,7 @@ module Ruby2JS
204
188
  elsif \
205
189
  method == :chmod and args.length == 2 and args.first.type == :int
206
190
  then
207
- @node_setup << :fs
191
+ prepend_list << IMPORT_FS
208
192
 
209
193
  S(:begin, *list[args.last].map {|file|
210
194
  S(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
@@ -215,14 +199,14 @@ module Ruby2JS
215
199
  method == :chown and args.length == 3 and
216
200
  args[0].type == :int and args[1].type == :int
217
201
  then
218
- @node_setup << :fs
202
+ prepend_list << IMPORT_FS
219
203
 
220
204
  S(:begin, *list[args.last].map {|file|
221
205
  s(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
222
206
  *process_all(args[0..1]))})
223
207
 
224
208
  elsif method == :touch
225
- @node_setup << :fs
209
+ prepend_list << IMPORT_FS
226
210
 
227
211
  S(:begin, *list[args.first].map {|file|
228
212
  S(:send, s(:attr, nil, :fs), :closeSync,
@@ -242,16 +226,16 @@ module Ruby2JS
242
226
  elsif method == :pwd and args.length == 0
243
227
  s(:send, s(:attr, nil, :process), :cwd)
244
228
  elsif method == :entries
245
- @node_setup << :fs
229
+ prepend_list << IMPORT_FS
246
230
  s(:send, s(:attr, nil, :fs), :readdirSync, *process_all(args))
247
231
  elsif method == :mkdir and args.length == 1
248
- @node_setup << :fs
232
+ prepend_list << IMPORT_FS
249
233
  s(:send, s(:attr, nil, :fs), :mkdirSync, process(args.first))
250
234
  elsif method == :rmdir and args.length == 1
251
- @node_setup << :fs
235
+ prepend_list << IMPORT_FS
252
236
  s(:send, s(:attr, nil, :fs), :rmdirSync, process(args.first))
253
237
  elsif method == :mktmpdir and args.length <=1
254
- @node_setup << :fs
238
+ prepend_list << IMPORT_FS
255
239
  if args.length == 0
256
240
  prefix = s(:str, 'd')
257
241
  elsif args.first.type == :array
@@ -291,7 +275,7 @@ module Ruby2JS
291
275
 
292
276
  def on_const(node)
293
277
  if node.children == [nil, :ARGV]
294
- @node_setup << :ARGV
278
+ prepend_list << SETUP_ARGV
295
279
  super
296
280
  elsif node.children == [nil, :ENV]
297
281
  S(:attr, s(:attr, nil, :process), :env)
@@ -319,7 +303,7 @@ module Ruby2JS
319
303
  end
320
304
 
321
305
  def on_xstr(node)
322
- @node_setup << :child_process
306
+ prepend_list << IMPORT_CHILD_PROCESS
323
307
 
324
308
  children = node.children.dup
325
309
  command = children.shift
@@ -7,29 +7,7 @@ module Ruby2JS
7
7
  include SEXP
8
8
  extend SEXP
9
9
 
10
- NOKOGIRI_SETUP = {
11
- jsdom: s(:casgn, nil, :JSDOM,
12
- s(:attr, s(:send, nil, :require, s(:str, "jsdom")), :JSDOM))
13
- }
14
-
15
- def initialize(*args)
16
- @nokogiri_setup = nil
17
- super
18
- end
19
-
20
- def process(node)
21
- return super if @nokogiri_setup
22
- @nokogiri_setup = Set.new
23
- result = super
24
-
25
- if @nokogiri_setup.empty?
26
- result
27
- else
28
- s(:begin,
29
- *@nokogiri_setup.to_a.map {|token| NOKOGIRI_SETUP[token]},
30
- result)
31
- end
32
- end
10
+ IMPORT_JSDOM = s(:import, ["jsdom"], [s(:attr, nil, :JSDOM)])
33
11
 
34
12
  def on_send(node)
35
13
  target, method, *args = node.children
@@ -51,7 +29,7 @@ module Ruby2JS
51
29
  [:HTML, :HTML5].include? method and
52
30
  target == s(:const, nil, :Nokogiri)
53
31
  then
54
- @nokogiri_setup << :jsdom
32
+ prepend_list << IMPORT_JSDOM
55
33
  S(:attr, s(:attr, s(:send, s(:const, nil, :JSDOM), :new,
56
34
  *process_all(args)), :window), :document)
57
35
 
@@ -61,7 +39,7 @@ module Ruby2JS
61
39
  target.children.first == s(:const, nil, :Nokogiri) and
62
40
  [:HTML, :HTML5].include? target.children.last
63
41
  then
64
- @nokogiri_setup << :jsdom
42
+ prepend_list << IMPORT_JSDOM
65
43
  S(:attr, s(:attr, s(:send, s(:const, nil, :JSDOM), :new,
66
44
  *process_all(args)), :window), :document)
67
45
 
@@ -38,8 +38,6 @@ module Ruby2JS
38
38
 
39
39
  filename = File.join(dirname, basename)
40
40
 
41
- segments = basename.split(/[\/\\]/)
42
-
43
41
  if not File.file? filename and File.file? filename+".rb"
44
42
  filename += '.rb'
45
43
  elsif not File.file? filename and File.file? filename+".js.rb"
@@ -8,16 +8,22 @@ module Ruby2JS
8
8
  EXPRESSIONS = [ :array, :float, :hash, :if, :int, :lvar, :nil, :send ]
9
9
 
10
10
  def on_block(node)
11
- children = process_all(node.children)
11
+ node = super
12
+ return node unless node.type == :block
13
+ children = node.children.dup
12
14
 
13
15
  children[-1] = s(:nil) if children.last == nil
14
16
 
15
- node.updated nil, [*node.children[0..1],
17
+ node.updated nil, [*children[0..1],
16
18
  s(:autoreturn, *children[2..-1])]
17
19
  end
18
20
 
19
21
  def on_def(node)
20
- children = process_all(node.children[1..-1])
22
+ node = super
23
+ return node unless node.type == :def
24
+ return node if [:constructor, :initialize].include?(node.children.first)
25
+
26
+ children = node.children[1..-1]
21
27
 
22
28
  children[-1] = s(:nil) if children.last == nil
23
29