ruby2js 3.5.0 → 3.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,68 @@
1
1
  require 'ruby2js'
2
2
 
3
+ Ruby2JS.module_default = :esm
4
+
3
5
  module Ruby2JS
4
6
  module Filter
5
7
  module ESM
6
8
  include SEXP
7
9
 
8
- def initialize(*args)
10
+ def options=(options)
11
+ super
12
+ @esm_autoexports = options[:autoexports] && !@disable_autoexports
13
+ @esm_autoimports = options[:autoimports]
14
+ @esm_explicit_tokens = Set.new
15
+ end
16
+
17
+ def process(node)
18
+ return super unless @esm_autoexports
19
+ @esm_autoexports = false
20
+
21
+ list = [node]
22
+ while list.length == 1 and list.first.type == :begin
23
+ list = list.first.children.dup
24
+ end
25
+
26
+ list.map! do |child|
27
+ replacement = child
28
+
29
+ if [:module, :class].include? child.type and
30
+ child.children.first.type == :const and
31
+ child.children.first.children.first == nil \
32
+ then
33
+ replacement = s(:export, child)
34
+ elsif child.type == :casgn and child.children.first == nil
35
+ replacement = s(:export, child)
36
+ elsif child.type == :def
37
+ replacement = s(:export, child)
38
+ end
39
+
40
+ if replacement != child and @comments[child]
41
+ @comments[replacement] = @comments[child]
42
+ end
43
+
44
+ replacement
45
+ end
46
+
47
+ process s(:begin, *list)
48
+ end
49
+
50
+ def on_class(node)
51
+ @esm_explicit_tokens << node.children.first.children.last
52
+
53
+ super
54
+ end
55
+
56
+ def on_def(node)
57
+ @esm_explicit_tokens << node.children.first
58
+
59
+ super
60
+ end
61
+
62
+ def on_lvasgn(node)
63
+ @esm_explicit_tokens << node.children.first
64
+
9
65
  super
10
- @esm = true # signal for other filters
11
66
  end
12
67
 
13
68
  def on_send(node)
@@ -36,6 +91,8 @@ module Ruby2JS
36
91
  args[0].children[2].children[2].type == :str
37
92
  # import name from "file.js"
38
93
  # => import name from "file.js"
94
+ @esm_explicit_tokens << args[0].children[1]
95
+
39
96
  s(:import,
40
97
  [args[0].children[2].children[2].children[0]],
41
98
  process(s(:attr, nil, args[0].children[1])))
@@ -49,17 +106,52 @@ module Ruby2JS
49
106
  # => import Stuff as * from "file.js"
50
107
  # import [ Some, Stuff ], from: "file.js"
51
108
  # => import { Some, Stuff } from "file.js"
52
- imports = (args[0].type == :const || args[0].type == :send) ?
53
- process(args[0]) :
109
+ imports = if args[0].type == :const || args[0].type == :send
110
+ @esm_explicit_tokens << args[0].children.last
111
+ process(args[0])
112
+ else
113
+ args[0].children.each {|i| @esm_explicit_tokens << i.children.last}
54
114
  process_all(args[0].children)
115
+ end
116
+
55
117
  s(:import, args[1].children, imports) unless args[1].nil?
56
118
  end
57
119
  elsif method == :export
58
120
  s(:export, *process_all(args))
121
+ elsif target.nil? and found_import = find_autoimport(method)
122
+ prepend_list << s(:import, found_import[0], found_import[1])
123
+ super
59
124
  else
60
125
  super
61
126
  end
62
127
  end
128
+
129
+ def on_const(node)
130
+ if node.children.first == nil and found_import = find_autoimport(node.children.last)
131
+ prepend_list << s(:import, found_import[0], found_import[1])
132
+ end
133
+
134
+ super
135
+ end
136
+
137
+ def on_export(node)
138
+ s(:export, *process_all(node.children))
139
+ end
140
+ end
141
+
142
+ private
143
+
144
+ def find_autoimport(token)
145
+ return nil if @esm_autoimports.nil?
146
+ return nil if @esm_explicit_tokens.include?(token)
147
+
148
+ token = camelCase(token) if respond_to?(:camelCase)
149
+
150
+ if @esm_autoimports[token]
151
+ [@esm_autoimports[token], s(:const, nil, token)]
152
+ elsif found_key = @esm_autoimports.keys.find {|key| key.is_a?(Array) && key.include?(token)}
153
+ [@esm_autoimports[found_key], found_key.map {|key| s(:const, nil, key)}]
154
+ end
63
155
  end
64
156
 
65
157
  DEFAULTS.push ESM
@@ -26,9 +26,15 @@ module Ruby2JS
26
26
  return super if excluded?(method)
27
27
 
28
28
  if [:max, :min].include? method and args.length == 0
29
- return super unless node.is_method?
30
- process S(:send, s(:const, nil, :Math), node.children[1],
31
- s(:splat, target))
29
+ if target.type == :array
30
+ process S(:send, s(:const, nil, :Math), node.children[1],
31
+ *target.children)
32
+ elsif node.is_method?
33
+ process S(:send, s(:const, nil, :Math), node.children[1],
34
+ s(:splat, target))
35
+ else
36
+ return super
37
+ end
32
38
 
33
39
  elsif method == :call and target and target.type == :ivar
34
40
  process S(:send, s(:self), "_#{target.children.first.to_s[1..-1]}",
@@ -531,6 +537,20 @@ module Ruby2JS
531
537
  elsif method == :block_given? and target == nil and args.length == 0
532
538
  process process s(:lvar, "_implicitBlockYield")
533
539
 
540
+ elsif method == :abs and args.length == 0
541
+ process S(:send, s(:const, nil, :Math), :abs, target)
542
+
543
+ elsif method == :ceil and args.length == 0
544
+ process S(:send, s(:const, nil, :Math), :ceil, target)
545
+
546
+ elsif method == :floor and args.length == 0
547
+ process S(:send, s(:const, nil, :Math), :floor, target)
548
+
549
+ elsif method == :sum and args.length == 0
550
+ process S(:send, target, :reduce, s(:block, s(:send, nil, :proc),
551
+ s(:args, s(:arg, :a), s(:arg, :b)),
552
+ s(:send, s(:lvar, :a), :+, s(:lvar, :b))), s(:int, 0))
553
+
534
554
  else
535
555
  super
536
556
  end
@@ -1,44 +1,25 @@
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
- CJS_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
- ESM_SETUP = {
19
- child_process: s(:import, ['child_process'],
20
- s(:attr, nil, :child_process)),
21
- fs: s(:import, ['fs'], s(:attr, nil, :fs)),
22
- ARGV: CJS_SETUP[:ARGV]
23
- }
24
-
25
- def initialize(*args)
26
- @node_setup = nil
27
- super
28
- end
12
+ IMPORT_CHILD_PROCESS = s(:import, ['child_process'],
13
+ s(:attr, nil, :child_process))
29
14
 
30
- def process(node)
31
- return super if @node_setup
32
- @node_setup = Set.new
33
- result = super
15
+ IMPORT_FS = s(:import, ['fs'], s(:attr, nil, :fs))
34
16
 
35
- if @node_setup.empty?
36
- result
37
- else
38
- setup = @esm ? ESM_SETUP : CJS_SETUP;
39
- s(:begin, *@node_setup.to_a.map {|token| setup[token]}, result)
40
- end
41
- end
17
+ IMPORT_OS = s(:import, ['os'], s(:attr, nil, :os))
18
+
19
+ IMPORT_PATH = s(:import, ['path'], s(:attr, nil, :path))
20
+
21
+ SETUP_ARGV = s(:lvasgn, :ARGV, s(:send, s(:attr,
22
+ s(:attr, nil, :process), :argv), :slice, s(:int, 2)))
42
23
 
43
24
  def on_send(node)
44
25
  target, method, *args = node.children
@@ -51,14 +32,14 @@ module Ruby2JS
51
32
  s(:send, s(:attr, nil, :process), :exit, *process_all(args));
52
33
 
53
34
  elsif method == :system
54
- @node_setup << :child_process
35
+ prepend_list << IMPORT_CHILD_PROCESS
55
36
 
56
37
  if args.length == 1
57
- s(:send, s(:attr, nil, :child_process), :execSync,
38
+ S(:send, s(:attr, nil, :child_process), :execSync,
58
39
  process(args.first),
59
40
  s(:hash, s(:pair, s(:sym, :stdio), s(:str, 'inherit'))))
60
41
  else
61
- s(:send, s(:attr, nil, :child_process), :execFileSync,
42
+ S(:send, s(:attr, nil, :child_process), :execFileSync,
62
43
  process(args.first), s(:array, *process_all(args[1..-1])),
63
44
  s(:hash, s(:pair, s(:sym, :stdio), s(:str, 'inherit'))))
64
45
  end
@@ -79,38 +60,38 @@ module Ruby2JS
79
60
  target.type == :const and target.children.first == nil
80
61
  then
81
62
  if method == :read and args.length == 1
82
- @node_setup << :fs
83
- s(:send, s(:attr, nil, :fs), :readFileSync, *process_all(args),
63
+ prepend_list << IMPORT_FS
64
+ S(:send, s(:attr, nil, :fs), :readFileSync, *process_all(args),
84
65
  s(:str, 'utf8'))
85
66
 
86
67
  elsif method == :write and args.length == 2
87
- @node_setup << :fs
68
+ prepend_list << IMPORT_FS
88
69
  S(:send, s(:attr, nil, :fs), :writeFileSync, *process_all(args))
89
70
 
90
71
  elsif target.children.last == :IO
91
72
  super
92
73
 
93
74
  elsif [:exist?, :exists?].include? method and args.length == 1
94
- @node_setup << :fs
75
+ prepend_list << IMPORT_FS
95
76
  S(:send, s(:attr, nil, :fs), :existsSync, process(args.first))
96
77
 
97
78
  elsif method == :readlink and args.length == 1
98
- @node_setup << :fs
79
+ prepend_list << IMPORT_FS
99
80
  S(:send, s(:attr, nil, :fs), :readlinkSync, process(args.first))
100
81
 
101
82
  elsif method == :realpath and args.length == 1
102
- @node_setup << :fs
83
+ prepend_list << IMPORT_FS
103
84
  S(:send, s(:attr, nil, :fs), :realpathSync, process(args.first))
104
85
 
105
86
  elsif method == :rename and args.length == 2
106
- @node_setup << :fs
87
+ prepend_list << IMPORT_FS
107
88
  S(:send, s(:attr, nil, :fs), :renameSync, *process_all(args))
108
89
 
109
90
  elsif \
110
91
  [:chmod, :lchmod].include? method and
111
92
  args.length > 1 and args.first.type == :int
112
93
  then
113
- @node_setup << :fs
94
+ prepend_list << IMPORT_FS
114
95
 
115
96
  S(:begin, *args[1..-1].map{|file|
116
97
  S(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
@@ -121,7 +102,7 @@ module Ruby2JS
121
102
  [:chown, :lchown].include? method and args.length > 2 and
122
103
  args[0].type == :int and args[1].type == :int
123
104
  then
124
- @node_setup << :fs
105
+ prepend_list << IMPORT_FS
125
106
 
126
107
  S(:begin, *args[2..-1].map{|file|
127
108
  s(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
@@ -129,28 +110,51 @@ module Ruby2JS
129
110
  })
130
111
 
131
112
  elsif [:ln, :link].include? method and args.length == 2
132
- @node_setup << :fs
113
+ prepend_list << IMPORT_FS
133
114
  s(:send, s(:attr, nil, :fs), :linkSync, *process_all(args))
134
115
 
135
116
  elsif method == :symlink and args.length == 2
136
- @node_setup << :fs
137
- s(:send, s(:attr, nil, :fs), :symlinkSync, *process_all(args))
117
+ prepend_list << IMPORT_FS
118
+ S(:send, s(:attr, nil, :fs), :symlinkSync, *process_all(args))
138
119
 
139
120
  elsif method == :truncate and args.length == 2
140
- @node_setup << :fs
141
- s(:send, s(:attr, nil, :fs), :truncateSync, *process_all(args))
121
+ prepend_list << IMPORT_FS
122
+ S(:send, s(:attr, nil, :fs), :truncateSync, *process_all(args))
142
123
 
143
124
  elsif [:stat, :lstat].include? method and args.length == 1
144
- @node_setup << :fs
145
- s(:send, s(:attr, nil, :fs), method.to_s + 'Sync',
125
+ prepend_list << IMPORT_FS
126
+ S(:send, s(:attr, nil, :fs), method.to_s + 'Sync',
146
127
  process(args.first))
147
128
 
148
129
  elsif method == :unlink and args.length == 1
149
- @node_setup << :fs
130
+ prepend_list << IMPORT_FS
150
131
  S(:begin, *args.map{|file|
151
132
  S(:send, s(:attr, nil, :fs), :unlinkSync, process(file))
152
133
  })
153
134
 
135
+ elsif target.children.last == :File
136
+ if method == :absolute_path
137
+ prepend_list << IMPORT_PATH
138
+ S(:send, s(:attr, nil, :path), :resolve,
139
+ *process_all(args.reverse))
140
+ elsif method == :absolute_path?
141
+ prepend_list << IMPORT_PATH
142
+ S(:send, s(:attr, nil, :path), :isAbsolute, *process_all(args))
143
+ elsif method == :basename
144
+ prepend_list << IMPORT_PATH
145
+ S(:send, s(:attr, nil, :path), :basename, *process_all(args))
146
+ elsif method == :dirname
147
+ prepend_list << IMPORT_PATH
148
+ S(:send, s(:attr, nil, :path), :dirname, *process_all(args))
149
+ elsif method == :extname
150
+ prepend_list << IMPORT_PATH
151
+ S(:send, s(:attr, nil, :path), :extname, *process_all(args))
152
+ elsif method == :join
153
+ prepend_list << IMPORT_PATH
154
+ S(:send, s(:attr, nil, :path), :join, *process_all(args))
155
+ else
156
+ super
157
+ end
154
158
  else
155
159
  super
156
160
  end
@@ -169,15 +173,15 @@ module Ruby2JS
169
173
  end
170
174
 
171
175
  if [:cp, :copy].include? method and args.length == 2
172
- @node_setup << :fs
176
+ prepend_list << IMPORT_FS
173
177
  s(:send, s(:attr, nil, :fs), :copyFileSync, *process_all(args))
174
178
 
175
179
  elsif [:mv, :move].include? method and args.length == 2
176
- @node_setup << :fs
180
+ prepend_list << IMPORT_FS
177
181
  S(:send, s(:attr, nil, :fs), :renameSync, *process_all(args))
178
182
 
179
183
  elsif method == :mkdir and args.length == 1
180
- @node_setup << :fs
184
+ prepend_list << IMPORT_FS
181
185
  S(:begin, *list[args.last].map {|file|
182
186
  s(:send, s(:attr, nil, :fs), :mkdirSync, process(file))
183
187
  })
@@ -186,24 +190,24 @@ module Ruby2JS
186
190
  S(:send, s(:attr, nil, :process), :chdir, *process_all(args))
187
191
 
188
192
  elsif method == :pwd and args.length == 0
189
- s(:send, s(:attr, nil, :process), :cwd)
193
+ S(:send!, s(:attr, nil, :process), :cwd)
190
194
 
191
195
  elsif method == :rmdir and args.length == 1
192
- @node_setup << :fs
196
+ prepend_list << IMPORT_FS
193
197
  S(:begin, *list[args.last].map {|file|
194
198
  s(:send, s(:attr, nil, :fs), :rmdirSync, process(file))
195
199
  })
196
200
 
197
201
  elsif method == :ln and args.length == 2
198
- @node_setup << :fs
199
- s(:send, s(:attr, nil, :fs), :linkSync, *process_all(args))
202
+ prepend_list << IMPORT_FS
203
+ S(:send, s(:attr, nil, :fs), :linkSync, *process_all(args))
200
204
 
201
205
  elsif method == :ln_s and args.length == 2
202
- @node_setup << :fs
203
- s(:send, s(:attr, nil, :fs), :symlinkSync, *process_all(args))
206
+ prepend_list << IMPORT_FS
207
+ S(:send, s(:attr, nil, :fs), :symlinkSync, *process_all(args))
204
208
 
205
209
  elsif method == :rm and args.length == 1
206
- @node_setup << :fs
210
+ prepend_list << IMPORT_FS
207
211
  S(:begin, *list[args.last].map {|file|
208
212
  s(:send, s(:attr, nil, :fs), :unlinkSync, process(file))
209
213
  })
@@ -211,7 +215,7 @@ module Ruby2JS
211
215
  elsif \
212
216
  method == :chmod and args.length == 2 and args.first.type == :int
213
217
  then
214
- @node_setup << :fs
218
+ prepend_list << IMPORT_FS
215
219
 
216
220
  S(:begin, *list[args.last].map {|file|
217
221
  S(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
@@ -222,14 +226,14 @@ module Ruby2JS
222
226
  method == :chown and args.length == 3 and
223
227
  args[0].type == :int and args[1].type == :int
224
228
  then
225
- @node_setup << :fs
229
+ prepend_list << IMPORT_FS
226
230
 
227
231
  S(:begin, *list[args.last].map {|file|
228
232
  s(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
229
233
  *process_all(args[0..1]))})
230
234
 
231
235
  elsif method == :touch
232
- @node_setup << :fs
236
+ prepend_list << IMPORT_FS
233
237
 
234
238
  S(:begin, *list[args.first].map {|file|
235
239
  S(:send, s(:attr, nil, :fs), :closeSync,
@@ -247,18 +251,18 @@ module Ruby2JS
247
251
  if method == :chdir and args.length == 1
248
252
  S(:send, s(:attr, nil, :process), :chdir, *process_all(args))
249
253
  elsif method == :pwd and args.length == 0
250
- s(:send, s(:attr, nil, :process), :cwd)
254
+ S(:send!, s(:attr, nil, :process), :cwd)
251
255
  elsif method == :entries
252
- @node_setup << :fs
253
- s(:send, s(:attr, nil, :fs), :readdirSync, *process_all(args))
256
+ prepend_list << IMPORT_FS
257
+ S(:send, s(:attr, nil, :fs), :readdirSync, *process_all(args))
254
258
  elsif method == :mkdir and args.length == 1
255
- @node_setup << :fs
256
- s(:send, s(:attr, nil, :fs), :mkdirSync, process(args.first))
259
+ prepend_list << IMPORT_FS
260
+ S(:send, s(:attr, nil, :fs), :mkdirSync, process(args.first))
257
261
  elsif method == :rmdir and args.length == 1
258
- @node_setup << :fs
259
- s(:send, s(:attr, nil, :fs), :rmdirSync, process(args.first))
262
+ prepend_list << IMPORT_FS
263
+ S(:send, s(:attr, nil, :fs), :rmdirSync, process(args.first))
260
264
  elsif method == :mktmpdir and args.length <=1
261
- @node_setup << :fs
265
+ prepend_list << IMPORT_FS
262
266
  if args.length == 0
263
267
  prefix = s(:str, 'd')
264
268
  elsif args.first.type == :array
@@ -267,7 +271,14 @@ module Ruby2JS
267
271
  prefix = args.first
268
272
  end
269
273
 
270
- s(:send, s(:attr, nil, :fs), :mkdtempSync, process(prefix))
274
+ S(:send, s(:attr, nil, :fs), :mkdtempSync, process(prefix))
275
+ elsif method == :home and args.length == 0
276
+ prepend_list << IMPORT_OS
277
+ S(:send!, s(:attr, nil, :os), :homedir)
278
+ elsif method == :tmpdir and args.length == 0
279
+ prepend_list << IMPORT_OS
280
+ S(:send!, s(:attr, nil, :os), :tmpdir)
281
+
271
282
  else
272
283
  super
273
284
  end
@@ -298,7 +309,7 @@ module Ruby2JS
298
309
 
299
310
  def on_const(node)
300
311
  if node.children == [nil, :ARGV]
301
- @node_setup << :ARGV
312
+ prepend_list << SETUP_ARGV
302
313
  super
303
314
  elsif node.children == [nil, :ENV]
304
315
  S(:attr, s(:attr, nil, :process), :env)
@@ -308,6 +319,16 @@ module Ruby2JS
308
319
  S(:attr, s(:attr, nil, :process), :stdout)
309
320
  elsif node.children == [nil, :STDERR]
310
321
  S(:attr, s(:attr, nil, :process), :stderr)
322
+ elsif node.children.first == s(:const, nil, :File)
323
+ if node.children.last == :SEPARATOR
324
+ prepend_list << IMPORT_PATH
325
+ S(:attr, s(:attr, nil, :path), :sep)
326
+ elsif node.children.last == :PATH_SEPARATOR
327
+ prepend_list << IMPORT_PATH
328
+ S(:attr, s(:attr, nil, :path), :delimiter)
329
+ else
330
+ super
331
+ end
311
332
  else
312
333
  super
313
334
  end
@@ -326,7 +347,7 @@ module Ruby2JS
326
347
  end
327
348
 
328
349
  def on_xstr(node)
329
- @node_setup << :child_process
350
+ prepend_list << IMPORT_CHILD_PROCESS
330
351
 
331
352
  children = node.children.dup
332
353
  command = children.shift