ruby2js 3.0.11 → 3.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6da7644d192eea8de1e3a6fe6b8550ef9b373912aed034a88d50cb0b0e27b988
4
- data.tar.gz: 558b980ae2a39e47cb4b17f354a5378b183606acd5700ed30f44070b5e180131
3
+ metadata.gz: 33e8a99086fe503dbff4afab72211baf206981e2b0dd68174aa8cc2ea9a1d361
4
+ data.tar.gz: 9c86b76630c9c2dbe32032280db32e52b0569ba56ccfcafee0729f8f6d9d351c
5
5
  SHA512:
6
- metadata.gz: a0819a2eeecd764f301f0bc8ec1d50cb4014dc715e850849e572fd0962247f46e2799a9265e474d79837f2cb17de201bb205762da018100eaa0b62eb7cb8a069
7
- data.tar.gz: bc64c4729076b98dc9589e4e27a7d873c5a1185604eaca6ee38923b560f57dfcb51509e1f4e55caa4a9ec2fb5b20c539146a2cd5d4e365d9507356e22b9e5989
6
+ metadata.gz: b8a0f54b473fa2494ac736232e8e60487a96923d9cd4df5635329c532726836d15a7b465744e7441016e811b153dcc22993d3e0b7013fa99696c18674f3f66fe
7
+ data.tar.gz: c7806d2aefa3449e3f2e07438fdea60a1b222d0bb866f51035ddd95da67a44a009df109ba39bf9ab2da6ba866c8bf4a8fb9652fca5bfea43e5c688307b9141af
data/README.md CHANGED
@@ -157,6 +157,20 @@ to `forEach`, `each!` will pass through the filter. The final code that emits
157
157
  JavaScript function calls and parameter accesses will strip off these
158
158
  suffixes.
159
159
 
160
+ This approach works well if it is an occasional change, but if the usage is
161
+ pervasive, most filters support options to `exclude` a list of mappings,
162
+ for example:
163
+
164
+ ```ruby
165
+ puts Ruby2JS.convert('jQuery("li").each {|index| ...}', exclude: :each)
166
+ ```
167
+
168
+ Alternatively, you can change the default:
169
+
170
+ ```ruby
171
+ Ruby2JS::Filter.exclude :each
172
+ ```
173
+
160
174
  Static transformations and runtime libraries aren't aren’t mutually exclusive.
161
175
  With enough of each, one could reproduce any functionality desired. Just be
162
176
  forewarned, that implementing a function like `method_missing` would require a
@@ -262,6 +276,11 @@ the script.
262
276
  of `Error` instead; and default constructors will be provided
263
277
  * `loop do...end` will be replaced with `while (true) {...}`
264
278
 
279
+ Additionally, there is one mapping that will only be done if explicitly
280
+ <a href="https://github.com/rubys/ruby2js/blob/master/lib/ruby2js/filter.rb">included</a>:
281
+
282
+ * `.class` becomes `.constructor`
283
+
265
284
  * <a id="rubyjs" href="https://github.com/rubys/ruby2js/blob/master/spec/rubyjs_spec.rb">rubyjs</a>
266
285
  * `.at()` becomes `_a.at()`
267
286
  * `.between?()` becomes `R().between()`
data/lib/ruby2js.rb CHANGED
@@ -9,6 +9,7 @@ ensure
9
9
  end
10
10
 
11
11
  require 'ruby2js/converter'
12
+ require 'ruby2js/filter'
12
13
 
13
14
  module Ruby2JS
14
15
  class SyntaxError < RuntimeError
@@ -48,6 +49,7 @@ module Ruby2JS
48
49
  end
49
50
 
50
51
  class Processor < Parser::AST::Processor
52
+ include Ruby2JS::Filter
51
53
  BINARY_OPERATORS = Converter::OPERATORS[2..-1].flatten
52
54
 
53
55
  def initialize(comments)
@@ -57,6 +59,14 @@ module Ruby2JS
57
59
 
58
60
  def options=(options)
59
61
  @options = options
62
+
63
+ @included = @@included
64
+ @excluded = @@excluded
65
+
66
+ include_all if options[:include_all]
67
+ include_only(options[:include_only]) if options[:include_only]
68
+ include(options[:include]) if options[:include]
69
+ exclude(options[:exclude]) if options[:exclude]
60
70
  end
61
71
 
62
72
  def es2015
@@ -148,11 +158,13 @@ module Ruby2JS
148
158
  filters = options[:filters] || Filter::DEFAULTS
149
159
 
150
160
  unless filters.empty?
161
+
151
162
  filter = Filter::Processor
152
163
  filters.reverse.each do |mod|
153
164
  filter = Class.new(filter) {include mod}
154
165
  end
155
166
  filter = filter.new(comments)
167
+
156
168
  filter.options = options
157
169
  ast = filter.process(ast)
158
170
  end
@@ -28,11 +28,11 @@ module Ruby2JS
28
28
  put "; "; parse var;
29
29
  if call.children[2].type == :int and call.children[2].children[0] < 0
30
30
  put " #{comp.sub('<', '>')} "; parse expression.children.last
31
- put "; "; parse var; put " -= "
32
- parse s(:int, -call.children[2].children[0])
31
+ put "; "; parse s(:op_asgn, var, :-,
32
+ s(:int, -call.children[2].children[0])), :statement
33
33
  else
34
34
  put " #{comp} "; parse expression.children.last
35
- put "; "; parse var; put " += "; parse call.children[2]
35
+ put "; "; parse s(:op_asgn, var, :+, call.children[2]), :statement
36
36
  end
37
37
  puts ") {"
38
38
  scope block
@@ -87,6 +87,11 @@ module Ruby2JS
87
87
  then
88
88
  @prop = left.children.first
89
89
  parse right, :method
90
+ elsif
91
+ es2015 and left.type == :sym and right.type == :lvar and
92
+ left.children == right.children
93
+ then
94
+ parse right
90
95
  else
91
96
  if not [:str, :sym].include? left.type and es2015
92
97
  put '['
@@ -4,7 +4,7 @@ module Ruby2JS
4
4
  # (nthref 1)
5
5
 
6
6
  handle :nth_ref do |var|
7
- put "$#{var}"
7
+ put "RegExp.$#{var}"
8
8
  end
9
9
  end
10
10
  end
@@ -12,7 +12,14 @@ module Ruby2JS
12
12
  var = s(:lvar, var.children.first) if var.type == :lvasgn
13
13
  var = s(:cvar, var.children.first) if var.type == :cvasgn
14
14
 
15
- if [:+, :-].include?(op) and value.type==:int and value.children==[1]
15
+ if
16
+ [:+, :-].include?(op) and value.type==:int and
17
+ (value.children==[1] or value.children==[-1])
18
+ then
19
+ if value.children.first == -1
20
+ op = (op == :+ ? :- : :+)
21
+ end
22
+
16
23
  if @state == :statement
17
24
  parse var; put "#{ op }#{ op }"
18
25
  else
@@ -0,0 +1,96 @@
1
+ #
2
+ # Manage a list of methods to be included or excluded. This allows fine
3
+ # grained control over filters.
4
+ #
5
+
6
+ module Ruby2JS
7
+ module Filter
8
+
9
+ #
10
+ # module level defaults
11
+ #
12
+
13
+ @@included = nil
14
+ @@excluded = []
15
+
16
+ # indicate that the specified methods are not to be processed
17
+ def self.exclude(*methods)
18
+ if @@included
19
+ @@included -= methods.flatten
20
+ else
21
+ @@excluded += methods.flatten
22
+ end
23
+ end
24
+
25
+ # indicate that all methods are to be processed
26
+ def self.include_all
27
+ @@included = nil
28
+ @@excluded = []
29
+ end
30
+
31
+ # indicate that only the specified methods are to be processed
32
+ def self.include_only(*methods)
33
+ @@included = methods.flatten
34
+ end
35
+
36
+ # indicate that the specified methods are to be processed
37
+ def self.include(*methods)
38
+ if @@included
39
+ @@included += methods.flatten
40
+ else
41
+ @@excluded -= methods.flatten
42
+ end
43
+ end
44
+
45
+ # indicate that the specified methods are not to be processed
46
+ def self.exclude(*methods)
47
+ if @@included
48
+ @@included -= methods.flatten
49
+ else
50
+ @@excluded += methods.flatten
51
+ end
52
+ end
53
+
54
+ #
55
+ # instance level overrides
56
+ #
57
+
58
+ # determine if a method is NOT to be processed
59
+ def excluded?(method)
60
+ if @included
61
+ not @included.include? method
62
+ else
63
+ @excluded.include? method
64
+ end
65
+ end
66
+
67
+ # indicate that all methods are to be processed
68
+ def include_all
69
+ @included = nil
70
+ @excluded = []
71
+ end
72
+
73
+ # indicate that only the specified methods are to be processed
74
+ def include_only(*methods)
75
+ @included = methods.flatten
76
+ end
77
+
78
+ # indicate that the specified methods are to be processed
79
+ def include(*methods)
80
+ if @included
81
+ @included += methods.flatten
82
+ else
83
+ @excluded -= methods.flatten
84
+ end
85
+ end
86
+
87
+ # indicate that the specified methods are not to be processed
88
+ def exclude(*methods)
89
+ if @included
90
+ @included -= methods.flatten
91
+ else
92
+ @excluded += methods.flatten
93
+ end
94
+ end
95
+ end
96
+ end
@@ -5,6 +5,9 @@ module Ruby2JS
5
5
  module Functions
6
6
  include SEXP
7
7
 
8
+ # require explicit opt-in to to class => constructor mapping
9
+ Filter.exclude :class
10
+
8
11
  VAR_TO_ASSIGN = {
9
12
  lvar: :lvasgn,
10
13
  ivar: :ivasgn,
@@ -14,6 +17,7 @@ module Ruby2JS
14
17
 
15
18
  def on_send(node)
16
19
  target, method, *args = node.children
20
+ return super if excluded?(method)
17
21
 
18
22
  if [:max, :min].include? method and args.length == 0
19
23
  return super unless node.is_method?
@@ -320,6 +324,9 @@ module Ruby2JS
320
324
  elsif es2017 and method==:ljust
321
325
  process node.updated(nil, [target, :padEnd, *args])
322
326
 
327
+ elsif method == :class and args.length==0 and not node.is_method?
328
+ process node.updated(:attr, [target, :constructor])
329
+
323
330
  else
324
331
  super
325
332
  end
@@ -327,50 +334,53 @@ module Ruby2JS
327
334
 
328
335
  def on_block(node)
329
336
  call = node.children.first
330
- if [:setInterval, :setTimeout].include? call.children[1]
337
+ method = call.children[1]
338
+ return super if excluded?(method)
339
+
340
+ if [:setInterval, :setTimeout].include? method
331
341
  return super unless call.children.first == nil
332
342
  block = process s(:block, s(:send, nil, :proc), *node.children[1..-1])
333
343
  on_send call.updated nil, [*call.children[0..1], block,
334
344
  *call.children[2..-1]]
335
345
 
336
- elsif [:sub, :gsub, :sub!, :gsub!, :sort!].include? call.children[1]
346
+ elsif [:sub, :gsub, :sub!, :gsub!, :sort!].include? method
337
347
  return super if call.children.first == nil
338
348
  block = s(:block, s(:send, nil, :proc), node.children[1],
339
349
  s(:autoreturn, *node.children[2..-1]))
340
350
  process call.updated(nil, [*call.children, block])
341
351
 
342
- elsif call.children[1] == :select and call.children.length == 2
352
+ elsif method == :select and call.children.length == 2
343
353
  call = call.updated nil, [call.children.first, :filter]
344
354
  node.updated nil, [process(call), process(node.children[1]),
345
355
  s(:autoreturn, *process_all(node.children[2..-1]))]
346
356
 
347
- elsif call.children[1] == :any? and call.children.length == 2
357
+ elsif method == :any? and call.children.length == 2
348
358
  call = call.updated nil, [call.children.first, :some]
349
359
  node.updated nil, [process(call), process(node.children[1]),
350
360
  s(:autoreturn, *process_all(node.children[2..-1]))]
351
361
 
352
- elsif call.children[1] == :all? and call.children.length == 2
362
+ elsif method == :all? and call.children.length == 2
353
363
  call = call.updated nil, [call.children.first, :every]
354
364
  node.updated nil, [process(call), process(node.children[1]),
355
365
  s(:autoreturn, *process_all(node.children[2..-1]))]
356
366
 
357
- elsif call.children[1] == :find and call.children.length == 2
367
+ elsif method == :find and call.children.length == 2
358
368
  node.updated nil, [process(call), process(node.children[1]),
359
369
  s(:autoreturn, *process_all(node.children[2..-1]))]
360
370
 
361
- elsif call.children[1] == :find_index and call.children.length == 2
371
+ elsif method == :find_index and call.children.length == 2
362
372
  call = call.updated nil, [call.children.first, :findIndex]
363
373
  node.updated nil, [process(call), process(node.children[1]),
364
374
  s(:autoreturn, *process_all(node.children[2..-1]))]
365
375
 
366
- elsif call.children[1] == :map and call.children.length == 2
376
+ elsif method == :map and call.children.length == 2
367
377
  node.updated nil, [process(call), process(node.children[1]),
368
378
  s(:autoreturn, *process_all(node.children[2..-1]))]
369
379
 
370
- elsif [:map!, :select!].include? call.children[1]
380
+ elsif [:map!, :select!].include? method
371
381
  # input: a.map! {expression}
372
382
  # output: a.splice(0, a.length, *a.map {expression})
373
- method = (call.children[1] == :map! ? :map : :select)
383
+ method = (method == :map! ? :map : :select)
374
384
  target = call.children.first
375
385
  process call.updated(:send, [target, :splice, s(:splat, s(:send,
376
386
  s(:array, s(:int, 0), s(:attr, target, :length)), :concat,
@@ -382,7 +392,7 @@ module Ruby2JS
382
392
  # output: while(true) {statements}
383
393
  S(:while, s(:true), node.children[2])
384
394
 
385
- elsif call.children[1] == :delete
395
+ elsif method == :delete
386
396
  # restore delete methods that are prematurely mapped to undef
387
397
  result = super
388
398
 
@@ -401,18 +411,18 @@ module Ruby2JS
401
411
 
402
412
  result
403
413
 
404
- elsif call.children[1] == :downto
414
+ elsif method == :downto
405
415
  range = s(:irange, call.children[0], call.children[2])
406
416
  call = call.updated(nil, [s(:begin, range), :step, s(:int, -1)])
407
417
  process node.updated(nil, [call, *node.children[1..-1]])
408
418
 
409
- elsif call.children[1] == :upto
419
+ elsif method == :upto
410
420
  range = s(:irange, call.children[0], call.children[2])
411
421
  call = call.updated(nil, [s(:begin, range), :step, s(:int, 1)])
412
422
  process node.updated(nil, [call, *node.children[1..-1]])
413
423
 
414
424
  elsif
415
- call.children[1] == :each and call.children[0].type == :send and
425
+ method == :each and call.children[0].type == :send and
416
426
  call.children[0].children[1] == :step
417
427
  then
418
428
  # i.step(j, n).each {|v| ...}
@@ -425,7 +435,7 @@ module Ruby2JS
425
435
 
426
436
  elsif
427
437
  # (a..b).each {|v| ...}
428
- call.children[1] == :each and
438
+ method == :each and
429
439
  call.children[0].type == :begin and
430
440
  call.children[0].children.length == 1 and
431
441
  [:irange, :erange].include? call.children[0].children[0].type and
@@ -435,7 +445,7 @@ module Ruby2JS
435
445
  call.children[0].children[0], node.children[2])
436
446
 
437
447
  elsif
438
- [:each, :each_value].include? call.children[1] and
448
+ [:each, :each_value].include? method and
439
449
  node.children[1].children.length == 1
440
450
  then
441
451
  if es2015
@@ -448,20 +458,20 @@ module Ruby2JS
448
458
  end
449
459
 
450
460
  elsif
451
- call.children[1] == :each_key and
452
- [:each, :each_key].include? call.children[1] and
461
+ method == :each_key and
462
+ [:each, :each_key].include? method and
453
463
  node.children[1].children.length == 1
454
464
  then
455
465
  process node.updated(:for,
456
466
  [s(:lvasgn, node.children[1].children[0].children[0]),
457
467
  node.children[0].children[0], node.children[2]])
458
468
 
459
- elsif es2015 and call.children[1] == :inject
469
+ elsif es2015 and method == :inject
460
470
  process node.updated(:send, [call.children[0], :reduce,
461
471
  s(:block, s(:send, nil, :lambda), *node.children[1..2]),
462
472
  *call.children[2..-1]])
463
473
 
464
- elsif es2017 and call.children[1] == :each_pair
474
+ elsif es2017 and method == :each_pair
465
475
  process node.updated(nil, [s(:send, s(:send, s(:const, nil, :Object),
466
476
  :entries, call.children[0]), :forEach), s(:args, s(:mlhs,
467
477
  *node.children[1].children)), node.children[2]])
@@ -11,6 +11,9 @@ module Ruby2JS
11
11
  return super if target and [:_s, :_a, :_h, :_n, :_i, :_t].
12
12
  include? target.children[1]
13
13
 
14
+ method = node.children[1]
15
+ return super if excluded?(method)
16
+
14
17
  # leave classic ("OO") style call chains alone
15
18
  while target and target.type == :send
16
19
  return super if target.children[1] == :R
@@ -19,33 +22,33 @@ module Ruby2JS
19
22
 
20
23
  if
21
24
  [:capitalize, :center, :chomp, :ljust, :lstrip, :rindex, :rjust,
22
- :rstrip, :scan, :swapcase, :tr].include? node.children[1]
25
+ :rstrip, :scan, :swapcase, :tr].include? method
23
26
  then
24
27
  # map selected string functions
25
- s(:send, s(:lvar, :_s), node.children[1],
28
+ s(:send, s(:lvar, :_s), method,
26
29
  *process_all([node.children[0], *node.children[2..-1]]))
27
30
 
28
31
  elsif
29
32
  [:at, :compact, :compact!, :delete_at, :delete_at, :flatten, :insert,
30
33
  :reverse, :reverse!, :rotate, :rotate, :rotate!, :shift, :shuffle,
31
34
  :shuffle!, :slice, :slice!, :transpose, :union, :uniq, :uniq!]
32
- .include? node.children[1]
35
+ .include? method
33
36
  then
34
37
  # map selected array functions
35
- s(:send, s(:lvar, :_a), node.children[1].to_s.sub("!", '_bang'),
38
+ s(:send, s(:lvar, :_a), method.to_s.sub("!", '_bang'),
36
39
  *process_all([node.children[0], *node.children[2..-1]]))
37
40
 
38
41
 
39
- elsif [:strftime].include? node.children[1]
42
+ elsif [:strftime].include? method
40
43
  # map selected time functions
41
- s(:send, s(:lvar, :_t), node.children[1],
44
+ s(:send, s(:lvar, :_t), method,
42
45
  *process_all([node.children[0], *node.children[2..-1]]))
43
46
 
44
- elsif node.children[1] == :<=>
47
+ elsif method == :<=>
45
48
  s(:send, s(:attr, s(:const, nil, :R), :Comparable), :cmp,
46
49
  node.children[0], *node.children[2..-1])
47
50
 
48
- elsif node.children[1] == :between?
51
+ elsif method == :between?
49
52
  s(:send, s(:send, nil, :R, node.children[0]), :between,
50
53
  *node.children[2..-1])
51
54
 
@@ -56,31 +59,32 @@ module Ruby2JS
56
59
 
57
60
  def on_block(node)
58
61
  call, args, *block = node.children
62
+ method = call.children[1]
63
+ return super if excluded?(method)
59
64
 
60
65
  if
61
66
  [:collect_concat, :count, :cycle, :delete_if, :drop_while,
62
67
  :each_index, :each_slice, :each_with_index, :each_with_object,
63
68
  :find, :find_all, :flat_map, :inject, :grep, :group_by, :keep_if,
64
69
  :map, :max_by, :min_by, :one?, :partition, :reject, :reverse_each,
65
- :select!, :sort_by, :take_while].include? call.children[1]
70
+ :select!, :sort_by, :take_while].include? method
66
71
  then
67
72
  if
68
73
  [:collect_concat, :count, :delete_if, :drop_while, :find,
69
74
  :find_all, :flat_map, :grep, :group_by, :keep_if, :map, :max_by,
70
75
  :min_by, :one?, :partition, :reject, :select!, :sort_by,
71
- :take_while].include? call.children[1]
76
+ :take_while].include? method
72
77
  then
73
78
  block = [ s(:autoreturn, *block) ]
74
79
  end
75
80
 
76
- lvar = [:each_index, :keep_if, :select!].
77
- include?(call.children[1]) ? :_a : :_e
81
+ lvar = [:each_index, :keep_if, :select!].include?(method) ? :_a : :_e
78
82
 
79
- if call.children[1] == :find and call.children.length == 2
83
+ if method == :find and call.children.length == 2
80
84
  call = s(:send, *call.children, s(:nil))
81
- elsif call.children[1] == :inject and call.children.length == 3
85
+ elsif method == :inject and call.children.length == 3
82
86
  call = s(:send, *call.children, s(:nil))
83
- elsif call.children[1] == :select!
87
+ elsif method == :select!
84
88
  call = s(:send, call.children.first, :select_bang,
85
89
  *call.children[2..-1])
86
90
  end
@@ -8,43 +8,46 @@ module Ruby2JS
8
8
  def on_send(node)
9
9
  return super if node.children.first and node.children.first.children.last == :_
10
10
 
11
+ method = node.children[1]
12
+ return super if excluded?(method)
13
+
11
14
  if [:clone, :shuffle, :size, :compact, :flatten, :invert, :values,
12
- :uniq].include? node.children[1]
15
+ :uniq].include? method
13
16
  if node.is_method? and node.children.length == 2
14
- process S(:send, s(:lvar, :_), node.children[1], node.children[0])
17
+ process S(:send, s(:lvar, :_), method, node.children[0])
15
18
  else
16
19
  super
17
20
  end
18
- elsif node.children[1] == :sample and node.children.length <= 3
21
+ elsif method == :sample and node.children.length <= 3
19
22
  process S(:send, s(:lvar, :_), :sample, node.children[0],
20
23
  *node.children[2..-1])
21
- elsif node.children[1] == :has_key? and node.children.length == 3
24
+ elsif method == :has_key? and node.children.length == 3
22
25
  process S(:send, s(:lvar, :_), :has, node.children[0],
23
26
  node.children[2])
24
- elsif node.children[1] == :sort and node.children.length == 2
27
+ elsif method == :sort and node.children.length == 2
25
28
  if node.is_method?
26
29
  process S(:send, s(:lvar, :_), :sortBy, node.children[0],
27
30
  s(:attr, s(:lvar, :_), :identity))
28
31
  else
29
32
  super
30
33
  end
31
- elsif node.children[1] == :map
34
+ elsif method == :map
32
35
  if node.children.length == 3 and node.children[2].type == :block_pass
33
36
  process S(:send, s(:lvar, :_), :pluck, node.children[0],
34
37
  node.children[2].children.first)
35
38
  else
36
39
  super
37
40
  end
38
- elsif node.children[1] == :merge and node.children.length >= 3
41
+ elsif method == :merge and node.children.length >= 3
39
42
  process S(:send, s(:lvar, :_), :extend, s(:hash), node.children[0],
40
43
  *node.children[2..-1])
41
- elsif node.children[1] == :merge! and node.children.length >= 3
44
+ elsif method == :merge! and node.children.length >= 3
42
45
  process S(:send, s(:lvar, :_), :extend, node.children[0],
43
46
  *node.children[2..-1])
44
- elsif node.children[1] == :zip and node.children.length >= 3
47
+ elsif method == :zip and node.children.length >= 3
45
48
  process S(:send, s(:lvar, :_), :zip, node.children[0],
46
49
  *node.children[2..-1])
47
- elsif node.children[1] == :invoke
50
+ elsif method == :invoke
48
51
  if node.children.length >= 3 and node.children.last.type==:block_pass
49
52
  process S(:send, s(:lvar, :_), :invoke, node.children[0],
50
53
  node.children.last.children.first,
@@ -52,11 +55,11 @@ module Ruby2JS
52
55
  else
53
56
  super
54
57
  end
55
- elsif [:where, :find_by].include? node.children[1]
56
- method = node.children[1] == :where ? :where : :findWhere
58
+ elsif [:where, :find_by].include? method
59
+ method = (method == :where ? :where : :findWhere)
57
60
  process S(:send, s(:lvar, :_), method, node.children[0],
58
61
  *node.children[2..-1])
59
- elsif node.children[1] == :reduce
62
+ elsif method == :reduce
60
63
  if node.children.length == 3 and node.children[2].type == :sym
61
64
  # input: a.reduce(:+)
62
65
  # output: _.reduce(_.rest(a),
@@ -86,13 +89,13 @@ module Ruby2JS
86
89
  end
87
90
 
88
91
  elsif [:compact!, :flatten!, :shuffle!, :uniq!].
89
- include? node.children[1] and node.is_method?
92
+ include? method and node.is_method?
90
93
  # input: a.compact!
91
94
  # output: a.splice(0, a.length, *a.compact)
92
95
  target = node.children.first
93
96
  process S(:send, target, :splice, s(:int, 0),
94
97
  s(:attr, target, :length), s(:splat, s(:send, target,
95
- :"#{node.children[1].to_s[0..-2]}", *node.children[2..-1])))
98
+ :"#{method.to_s[0..-2]}", *node.children[2..-1])))
96
99
  else
97
100
  super
98
101
  end
@@ -100,31 +103,34 @@ module Ruby2JS
100
103
 
101
104
  def on_block(node)
102
105
  call = node.children.first
103
- if [:sort_by, :group_by, :index_by, :count_by].include? call.children[1]
106
+ method = call.children[1]
107
+ return super if excluded?(method)
108
+
109
+ if [:sort_by, :group_by, :index_by, :count_by].include? method
104
110
  # input: a.sort_by {}
105
111
  # output: _.sortBy {return expression}
106
- method = call.children[1].to_s.sub(/\_by$/,'By').to_sym
112
+ method = method.to_s.sub(/\_by$/,'By').to_sym
107
113
  process S(:block, s(:send, s(:lvar, :_), method,
108
114
  call.children.first), node.children[1],
109
115
  s(:autoreturn, node.children[2]))
110
- elsif [:find, :reject].include? call.children[1]
116
+ elsif [:find, :reject].include? method
111
117
  if call.children.length == 2
112
118
  # input: a.find {|item| item > 0}
113
119
  # output: _.find(a) {|item| return item > 0}
114
- process S(:block, s(:send, s(:lvar, :_), call.children[1],
120
+ process S(:block, s(:send, s(:lvar, :_), method,
115
121
  call.children.first), node.children[1],
116
122
  s(:autoreturn, node.children[2]))
117
123
  else
118
124
  super
119
125
  end
120
126
 
121
- elsif call.children[1] == :times and call.children.length == 2
127
+ elsif method == :times and call.children.length == 2
122
128
  # input: 5.times {|i| console.log i}
123
129
  # output: _.find(5) {|i| console.log(i)}
124
- process S(:block, s(:send, s(:lvar, :_), call.children[1],
130
+ process S(:block, s(:send, s(:lvar, :_), method,
125
131
  call.children.first), node.children[1], node.children[2])
126
132
 
127
- elsif call.children[1] == :reduce
133
+ elsif method == :reduce
128
134
  if call.children.length == 2
129
135
  # input: a.reduce {|memo, item| memo+item}
130
136
  # output: _.reduce(_.rest(a),
@@ -144,10 +150,10 @@ module Ruby2JS
144
150
  call.children[2])
145
151
  end
146
152
 
147
- elsif [:map!, :reject!, :select!, :sort_by!].include? call.children[1]
153
+ elsif [:map!, :reject!, :select!, :sort_by!].include? method
148
154
  # input: a.map! {expression}
149
155
  # output: a.splice(0, a.length, *a.map {expression})
150
- method = :"#{call.children[1].to_s[0..-2]}"
156
+ method = :"#{method.to_s[0..-2]}"
151
157
  target = call.children.first
152
158
  process S(:call, target, :splice, s(:splat, s(:send, s(:array,
153
159
  s(:int, 0), s(:attr, target, :length)), :concat,
@@ -623,7 +623,11 @@ module Ruby2JS
623
623
  hash['nativeOn']['input'] ||
624
624
  hash['nativeOn']['change']
625
625
  attr = 'value'
626
- event = 'input'
626
+ if hash[:attrs]['type'] == s(:str, 'file')
627
+ event = 'change'
628
+ else
629
+ event = 'input'
630
+ end
627
631
  end
628
632
 
629
633
  # search for the presence of a 'onClick' attribute
@@ -2,7 +2,7 @@ module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
4
  MINOR = 0
5
- TINY = 11
5
+ TINY = 12
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2js
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.11
4
+ version: 3.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-08 00:00:00.000000000 Z
11
+ date: 2018-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -93,6 +93,7 @@ files:
93
93
  - lib/ruby2js/es2017.rb
94
94
  - lib/ruby2js/es2017/strict.rb
95
95
  - lib/ruby2js/execjs.rb
96
+ - lib/ruby2js/filter.rb
96
97
  - lib/ruby2js/filter/camelCase.rb
97
98
  - lib/ruby2js/filter/functions.rb
98
99
  - lib/ruby2js/filter/jquery.rb
@@ -130,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  version: '0'
131
132
  requirements: []
132
133
  rubyforge_project:
133
- rubygems_version: 2.7.4
134
+ rubygems_version: 2.7.6
134
135
  signing_key:
135
136
  specification_version: 4
136
137
  summary: Minimal yet extensible Ruby to JavaScript conversion.