ruby2js 4.2.0 → 5.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c55c9f73006080091da67df98c60fb8789615cf162604405223b5104f5d04f0d
4
- data.tar.gz: 50010eb8552bdf41fdcf3eba7a8f0e20bed0c1bd33916f951806426674c35147
3
+ metadata.gz: 40cd0d8969ad8042e83eaad5d0c870a05e949907a8d8429ca295f12ccdce5b45
4
+ data.tar.gz: f03789a32881ceca5fb7310e9e88c96882a76a9782c8940bdfe01bf00354fcef
5
5
  SHA512:
6
- metadata.gz: ba0d06c13ff572deb0d001ce342fb5fb92d3573356976f898693896c73960cf58f467daf3f619a1b2a04a47afb3fabc3461608d4645aaf04272031aaf9d625fb
7
- data.tar.gz: 6274e5f520764b8ccaeb711eb6910a06cdb67d62791ef73e5a6b36e369d1ea05acf0db653148fb3ad05df320002aaab92b71c6d083b3cbed5ba83f768dc840bd
6
+ metadata.gz: 719e4a55b5eb62ffd31e53eeeebfb80140d0692d3cf7675a33eb10188d1845267b4a35fe3590ed589af49c9f576430c261e568184a8a13675036510fdc01493a
7
+ data.tar.gz: d4a0962d3534066225b8500b20af25124784de5ac7d5b62d25d363b8d781d09d4a359ef2fbd945ccd2aa7b48f34b8d3564e52b18bb15c09df7e0da0b01115e42
data/README.md CHANGED
@@ -7,7 +7,7 @@ Minimal yet extensible Ruby to JavaScript conversion.
7
7
  [![Gitter](https://badges.gitter.im/ruby2js/community.svg)](https://gitter.im/ruby2js/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
8
8
 
9
9
 
10
- Documentation
10
+ ## Documentation
11
11
  ---
12
12
 
13
13
  * Visit **[ruby2js.com](https://www.ruby2js.com)** for detailed setup instructions and API reference.
@@ -15,8 +15,8 @@ Documentation
15
15
  * [Try Ruby2JS online](https://ruby2js.com/demo)
16
16
 
17
17
 
18
- Synopsis
19
- ---
18
+ ## Synopsis
19
+
20
20
 
21
21
  Basic:
22
22
 
@@ -44,9 +44,17 @@ Enable ES2015 support:
44
44
  puts Ruby2JS.convert('"#{a}"', eslevel: 2015)
45
45
  ```
46
46
 
47
+ ## Testing
47
48
 
48
- License
49
- ---
49
+ 1. Run `bundle install`
50
+ 2. Run `bundle exec rake test_all`
51
+
52
+ ## Release Process for Maintainers
53
+
54
+ 1. Update the version in both `packages/ruby2js/package.json` and `lib/ruby2js/version`, ensuring they match.
55
+ 2. Run `bundle exec rake release_core`
56
+
57
+ ## License
50
58
 
51
59
  (The MIT License)
52
60
 
data/demo/ruby2js.rb CHANGED
@@ -199,6 +199,8 @@ if (not defined? Wunderbar or not env['SERVER_PORT']) and not @live
199
199
  end
200
200
 
201
201
  else
202
+ require 'wunderbar'
203
+
202
204
  def walk(ast, indent='', tail='', last=true)
203
205
  return unless ast
204
206
  _div class: (ast.loc ? 'loc' : 'unloc') do
@@ -69,5 +69,19 @@ module Ruby2JS
69
69
  parse s(@ast.children[0].type, *call.children, function), @state
70
70
  end
71
71
  end
72
+
73
+ # (numblock
74
+ # (send nil :x)
75
+ # 1
76
+ # (lvar :_1))
77
+
78
+ handle :numblock do |call, count, block|
79
+ parse s(:block,
80
+ call,
81
+ s(:args, *((1..count).map {|i| s(:arg, "_#{i}")})),
82
+ block
83
+ )
84
+ end
85
+
72
86
  end
73
87
  end
@@ -1,7 +1,28 @@
1
1
  module Ruby2JS
2
2
  class Converter
3
3
  handle :match_pattern do |value, name|
4
- parse @ast.updated(:lvasgn, [name.children.first, value]), @state
4
+ if name.type == :match_var
5
+ parse @ast.updated(:lvasgn, [name.children.first, value]), @state
6
+ elsif name.type == :hash_pattern and name.children.all? {|child| child.type == :match_var}
7
+ if es2015
8
+ put 'let { '
9
+ put name.children.map {|child| child.children[0].to_s}.join(', ')
10
+ put ' } = '
11
+ parse value
12
+ else
13
+ name.children.each_with_index do |child, index|
14
+ put @sep unless index == 0
15
+ put 'var '
16
+ put child.children[0].to_s
17
+ put ' = '
18
+ parse value
19
+ put '.'
20
+ put child.children[0].to_s
21
+ end
22
+ end
23
+ else
24
+ raise Error.new("complex match patterns are not supported", @ast)
25
+ end
5
26
  end
6
27
  end
7
28
  end
@@ -69,7 +69,7 @@ module Ruby2JS
69
69
  def handle_generic_node(node, node_type)
70
70
  return node if node.type != node_type
71
71
 
72
- if node.children[0] =~ /_.*[?!\w]$/ and !ALLOWLIST.include?(node.children[0].to_s)
72
+ if node.children[0].to_s =~ /_.*[?!\w]$/ and !ALLOWLIST.include?(node.children[0].to_s)
73
73
  S(node_type , camelCase(node.children[0]), *node.children[1..-1])
74
74
  else
75
75
  node
@@ -162,6 +162,9 @@ module Ruby2JS
162
162
  elsif method == :to_f
163
163
  process node.updated :send, [nil, :parseFloat, target, *args]
164
164
 
165
+ elsif method == :to_json
166
+ process node.updated :send, [s(:const, nil, :JSON), :stringify, target, *args]
167
+
165
168
  elsif method == :sub and args.length == 2
166
169
  if args[1].type == :str
167
170
  args[1] = s(:str, args[1].children.first.gsub(/\\(\d)/, "$\\1"))
@@ -439,8 +442,12 @@ module Ruby2JS
439
442
  S(:send, s(:const, nil, :JSON), :stringify, process(target))
440
443
 
441
444
  elsif method == :* and target.type == :str
442
- process S(:send, s(:send, s(:const, nil, :Array), :new,
443
- s(:send, args.first, :+, s(:int, 1))), :join, target)
445
+ if es2015
446
+ process S(:send, target, :repeat, args.first)
447
+ else
448
+ process S(:send, s(:send, s(:const, nil, :Array), :new,
449
+ s(:send, args.first, :+, s(:int, 1))), :join, target)
450
+ end
444
451
 
445
452
  elsif [:is_a?, :kind_of?].include? method and args.length == 1
446
453
  if args[0].type == :const
@@ -503,6 +510,12 @@ module Ruby2JS
503
510
  s(:regexp, s(:str, '\A\s+') , s(:regopt)), s(:str, '')])
504
511
  end
505
512
 
513
+ elsif method == :index and node.is_method?
514
+ process node.updated(nil, [target, :indexOf, *args])
515
+
516
+ elsif method == :rindex and node.is_method?
517
+ process node.updated(nil, [target, :lastIndexOf, *args])
518
+
506
519
  elsif method == :class and args.length==0 and not node.is_method?
507
520
  process node.updated(:attr, [target, :constructor])
508
521
 
@@ -515,6 +528,9 @@ module Ruby2JS
515
528
  elsif method == :abs and args.length == 0
516
529
  process S(:send, s(:const, nil, :Math), :abs, target)
517
530
 
531
+ elsif method == :round and args.length == 0
532
+ process S(:send, s(:const, nil, :Math), :round, target)
533
+
518
534
  elsif method == :ceil and args.length == 0
519
535
  process S(:send, s(:const, nil, :Math), :ceil, target)
520
536
 
@@ -570,6 +586,20 @@ module Ruby2JS
570
586
  process S(:send, s(:attr, target, :prototype), :[]=, args[0],
571
587
  s(:attr, s(:attr, target, :prototype), args[1].children[0]))
572
588
 
589
+ elsif method == :new and args.length == 2 and target == s(:const, nil, :Array)
590
+ if es2015
591
+ s(:send, S(:send, target, :new, args.first), :fill, args.last)
592
+ else
593
+ super
594
+ end
595
+
596
+ elsif method == :chars and args.length == 0
597
+ if es2015
598
+ S(:send, s(:const, nil, :Array), :from, target)
599
+ else
600
+ super
601
+ end
602
+
573
603
  else
574
604
  super
575
605
  end
@@ -616,6 +646,11 @@ module Ruby2JS
616
646
  node.updated nil, [process(call), process(node.children[1]),
617
647
  s(:autoreturn, *process_all(node.children[2..-1]))]
618
648
 
649
+ elsif method == :index and call.children.length == 2
650
+ call = call.updated nil, [call.children.first, :findIndex]
651
+ node.updated nil, [process(call), process(node.children[1]),
652
+ s(:autoreturn, *process_all(node.children[2..-1]))]
653
+
619
654
  elsif method == :map and call.children.length == 2
620
655
  node.updated nil, [process(call), process(node.children[1]),
621
656
  s(:autoreturn, *process_all(node.children[2..-1]))]
@@ -38,6 +38,6 @@ Tilt.register 'rb', Ruby2JSTemplate
38
38
  helpers do
39
39
  def ruby2js(*args)
40
40
  content_type 'application/javascript'
41
- render('rb', *args)
41
+ render('rb', *args).to_s
42
42
  end
43
43
  end
@@ -1,7 +1,7 @@
1
1
  module Ruby2JS
2
2
  module VERSION #:nodoc:
3
- MAJOR = 4
4
- MINOR = 2
3
+ MAJOR = 5
4
+ MINOR = 0
5
5
  TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
data/lib/ruby2js.rb CHANGED
@@ -185,6 +185,17 @@ module Ruby2JS
185
185
  # provide a method so filters can call 'super'
186
186
  def on_sym(node); node; end
187
187
 
188
+ # convert numbered parameters block to a normal block
189
+ def on_numblock(node)
190
+ call, count, block = node.children
191
+
192
+ process s(:block,
193
+ call,
194
+ s(:args, *((1..count).map {|i| s(:arg, "_#{i}")})),
195
+ block
196
+ )
197
+ end
198
+
188
199
  # convert map(&:symbol) to a block
189
200
  def on_send(node)
190
201
  if node.children.length > 2 and node.children.last.type == :block_pass
data/ruby2js.gemspec CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.files = %w(ruby2js.gemspec README.md bin/ruby2js demo/ruby2js.rb) + Dir.glob("{lib}/**/*")
16
16
  s.homepage = "http://github.com/rubys/ruby2js".freeze
17
17
  s.licenses = ["MIT".freeze]
18
- s.required_ruby_version = Gem::Requirement.new(">= 2.3".freeze)
18
+ s.required_ruby_version = Gem::Requirement.new(">= 2.7".freeze)
19
19
  s.summary = "Minimal yet extensible Ruby to JavaScript conversion.".freeze
20
20
 
21
21
  s.executables << 'ruby2js'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2js
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-10-11 00:00:00.000000000 Z
12
+ date: 2022-05-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parser
@@ -187,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
187
187
  requirements:
188
188
  - - ">="
189
189
  - !ruby/object:Gem::Version
190
- version: '2.3'
190
+ version: '2.7'
191
191
  required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  requirements:
193
193
  - - ">="