ruby2js 3.4.0 → 3.6.0

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,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
@@ -7,33 +7,7 @@ module Ruby2JS
7
7
  include SEXP
8
8
  extend SEXP
9
9
 
10
- CJS_SETUP = {
11
- jsdom: s(:casgn, nil, :JSDOM,
12
- s(:attr, s(:send, nil, :require, s(:str, "jsdom")), :JSDOM))
13
- }
14
-
15
- ESM_SETUP = {
16
- jsdom: s(:import, ["jsdom"], [s(:attr, nil, :JSDOM)])
17
- }
18
-
19
- def initialize(*args)
20
- @nokogiri_setup = nil
21
- super
22
- end
23
-
24
- def process(node)
25
- return super if @nokogiri_setup
26
- @nokogiri_setup = Set.new
27
- result = super
28
-
29
- if @nokogiri_setup.empty?
30
- result
31
- else
32
- setup = @esm ? ESM_SETUP : CJS_SETUP;
33
- s(:begin,
34
- *@nokogiri_setup.to_a.map {|token| setup[token]}, result)
35
- end
36
- end
10
+ IMPORT_JSDOM = s(:import, ["jsdom"], [s(:attr, nil, :JSDOM)])
37
11
 
38
12
  def on_send(node)
39
13
  target, method, *args = node.children
@@ -55,7 +29,7 @@ module Ruby2JS
55
29
  [:HTML, :HTML5].include? method and
56
30
  target == s(:const, nil, :Nokogiri)
57
31
  then
58
- @nokogiri_setup << :jsdom
32
+ prepend_list << IMPORT_JSDOM
59
33
  S(:attr, s(:attr, s(:send, s(:const, nil, :JSDOM), :new,
60
34
  *process_all(args)), :window), :document)
61
35
 
@@ -65,7 +39,7 @@ module Ruby2JS
65
39
  target.children.first == s(:const, nil, :Nokogiri) and
66
40
  [:HTML, :HTML5].include? target.children.last
67
41
  then
68
- @nokogiri_setup << :jsdom
42
+ prepend_list << IMPORT_JSDOM
69
43
  S(:attr, s(:attr, s(:send, s(:const, nil, :JSDOM), :new,
70
44
  *process_all(args)), :window), :document)
71
45
 
@@ -21,6 +21,8 @@ module Ruby2JS
21
21
  def on_def(node)
22
22
  node = super
23
23
  return node unless node.type == :def
24
+ return node if [:constructor, :initialize].include?(node.children.first)
25
+
24
26
  children = node.children[1..-1]
25
27
 
26
28
  children[-1] = s(:nil) if children.last == nil
@@ -0,0 +1,33 @@
1
+ require 'ruby2js'
2
+ require 'set'
3
+
4
+ # Experimental secure random support
5
+
6
+ module Ruby2JS
7
+ module Filter
8
+ module SecureRandom
9
+ include SEXP
10
+ extend SEXP
11
+
12
+ IMPORT_BASE62_RANDOM = s(:import, ['base62-random'],
13
+ s(:attr, nil, :base62_random))
14
+
15
+ def on_send(node)
16
+ target, method, *args = node.children
17
+
18
+ if target == s(:const, nil, :SecureRandom)
19
+ if method == :alphanumeric and args.length == 1
20
+ prepend_list << IMPORT_BASE62_RANDOM
21
+ node.updated(nil, [nil, :base62_random, *args])
22
+ else
23
+ super
24
+ end
25
+ else
26
+ super
27
+ end
28
+ end
29
+ end
30
+
31
+ DEFAULTS.push SecureRandom
32
+ end
33
+ end
@@ -106,6 +106,7 @@ module Ruby2JS
106
106
  computed = []
107
107
  setters = []
108
108
  options = []
109
+ watch = nil
109
110
  el = nil
110
111
  mixins = []
111
112
 
@@ -205,6 +206,9 @@ module Ruby2JS
205
206
  end
206
207
 
207
208
  @vue_h = args.children.first.children.last
209
+ elsif method == :watch and args.children.length == 0 and block.type == :hash
210
+ watch = process(block)
211
+ next
208
212
  elsif method == :initialize
209
213
  method = :data
210
214
 
@@ -332,6 +336,11 @@ module Ruby2JS
332
336
  hash << s(:pair, s(:sym, :computed), s(:hash, *computed))
333
337
  end
334
338
 
339
+ # append watch to hash
340
+ if watch
341
+ hash << s(:pair, s(:sym, :watch), watch)
342
+ end
343
+
335
344
  # convert class name to camel case
336
345
  cname = cname.children.last
337
346
  camel = cname.to_s.gsub(/[^\w]/, '-').