ruby2js 3.0.15 → 3.3.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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +92 -7
  3. data/lib/ruby2js.rb +30 -3
  4. data/lib/ruby2js/converter.rb +23 -3
  5. data/lib/ruby2js/converter/args.rb +31 -1
  6. data/lib/ruby2js/converter/block.rb +2 -2
  7. data/lib/ruby2js/converter/casgn.rb +3 -2
  8. data/lib/ruby2js/converter/class.rb +2 -2
  9. data/lib/ruby2js/converter/class2.rb +117 -13
  10. data/lib/ruby2js/converter/cvar.rb +5 -3
  11. data/lib/ruby2js/converter/cvasgn.rb +5 -3
  12. data/lib/ruby2js/converter/def.rb +2 -2
  13. data/lib/ruby2js/converter/for.rb +8 -1
  14. data/lib/ruby2js/converter/hash.rb +22 -6
  15. data/lib/ruby2js/converter/if.rb +13 -2
  16. data/lib/ruby2js/converter/import.rb +38 -0
  17. data/lib/ruby2js/converter/ivar.rb +2 -0
  18. data/lib/ruby2js/converter/ivasgn.rb +1 -1
  19. data/lib/ruby2js/converter/kwbegin.rb +16 -2
  20. data/lib/ruby2js/converter/logical.rb +46 -1
  21. data/lib/ruby2js/converter/opasgn.rb +5 -2
  22. data/lib/ruby2js/converter/regexp.rb +27 -2
  23. data/lib/ruby2js/converter/return.rb +1 -1
  24. data/lib/ruby2js/converter/send.rb +160 -69
  25. data/lib/ruby2js/converter/super.rb +15 -9
  26. data/lib/ruby2js/converter/xnode.rb +89 -0
  27. data/lib/ruby2js/es2018.rb +5 -0
  28. data/lib/ruby2js/es2018/strict.rb +3 -0
  29. data/lib/ruby2js/es2019.rb +5 -0
  30. data/lib/ruby2js/es2019/strict.rb +3 -0
  31. data/lib/ruby2js/es2020.rb +5 -0
  32. data/lib/ruby2js/es2020/strict.rb +3 -0
  33. data/lib/ruby2js/es2021.rb +5 -0
  34. data/lib/ruby2js/es2021/strict.rb +3 -0
  35. data/lib/ruby2js/filter.rb +1 -0
  36. data/lib/ruby2js/filter/cjs.rb +2 -2
  37. data/lib/ruby2js/filter/esm.rb +72 -0
  38. data/lib/ruby2js/filter/functions.rb +218 -34
  39. data/lib/ruby2js/filter/matchAll.rb +49 -0
  40. data/lib/ruby2js/filter/node.rb +18 -10
  41. data/lib/ruby2js/filter/nokogiri.rb +13 -13
  42. data/lib/ruby2js/filter/react.rb +190 -30
  43. data/lib/ruby2js/filter/require.rb +1 -4
  44. data/lib/ruby2js/filter/rubyjs.rb +4 -4
  45. data/lib/ruby2js/filter/vue.rb +45 -17
  46. data/lib/ruby2js/filter/wunderbar.rb +63 -0
  47. data/lib/ruby2js/serializer.rb +25 -11
  48. data/lib/ruby2js/version.rb +2 -2
  49. metadata +16 -4
@@ -17,7 +17,7 @@ module Ruby2JS
17
17
  end
18
18
 
19
19
  def on_send(node)
20
- if
20
+ if \
21
21
  not @require_expr and # only statements
22
22
  node.children.length == 3 and
23
23
  node.children[0] == nil and
@@ -39,9 +39,6 @@ module Ruby2JS
39
39
  filename = File.join(dirname, basename)
40
40
 
41
41
  segments = basename.split(/[\/\\]/)
42
- if segments.all? {|path| path =~ @@valid_path and path != '..'}
43
- filename.untaint
44
- end
45
42
 
46
43
  if not File.file? filename and File.file? filename+".rb"
47
44
  filename += '.rb'
@@ -20,7 +20,7 @@ module Ruby2JS
20
20
  target = target.children.first
21
21
  end
22
22
 
23
- if
23
+ if \
24
24
  [:capitalize, :center, :chomp, :ljust, :lstrip, :rindex, :rjust,
25
25
  :rstrip, :scan, :swapcase, :tr].include? method
26
26
  then
@@ -28,7 +28,7 @@ module Ruby2JS
28
28
  s(:send, s(:lvar, :_s), method,
29
29
  *process_all([node.children[0], *node.children[2..-1]]))
30
30
 
31
- elsif
31
+ elsif \
32
32
  [:at, :compact, :compact!, :delete_at, :delete_at, :flatten, :insert,
33
33
  :reverse, :reverse!, :rotate, :rotate, :rotate!, :shift, :shuffle,
34
34
  :shuffle!, :slice, :slice!, :transpose, :union, :uniq, :uniq!]
@@ -62,14 +62,14 @@ module Ruby2JS
62
62
  method = call.children[1]
63
63
  return super if excluded?(method)
64
64
 
65
- if
65
+ if \
66
66
  [:collect_concat, :count, :cycle, :delete_if, :drop_while,
67
67
  :each_index, :each_slice, :each_with_index, :each_with_object,
68
68
  :find, :find_all, :flat_map, :inject, :grep, :group_by, :keep_if,
69
69
  :map, :max_by, :min_by, :one?, :partition, :reject, :reverse_each,
70
70
  :select!, :sort_by, :take_while].include? method
71
71
  then
72
- if
72
+ if \
73
73
  [:collect_concat, :count, :delete_if, :drop_while, :find,
74
74
  :find_all, :flat_map, :grep, :group_by, :keep_if, :map, :max_by,
75
75
  :min_by, :one?, :partition, :reject, :select!, :sort_by,
@@ -30,7 +30,12 @@ module Ruby2JS
30
30
  @vue_props = []
31
31
  @vue_reactive = []
32
32
  @vue_filter_functions = false
33
+
34
+ @vue_setup = false
35
+
33
36
  super
37
+
38
+ @exclude_methods << @vue_methods
34
39
  end
35
40
 
36
41
  def options=(options)
@@ -38,7 +43,7 @@ module Ruby2JS
38
43
  @vue_h ||= options[:vue_h]
39
44
  filters = options[:filters] || Filter::DEFAULTS
40
45
 
41
- if
46
+ if \
42
47
  defined? Ruby2JS::Filter::Functions and
43
48
  filters.include? Ruby2JS::Filter::Functions
44
49
  then
@@ -46,6 +51,29 @@ module Ruby2JS
46
51
  end
47
52
  end
48
53
 
54
+ # if options[:vue_h] is set, return an array of nodes
55
+ def process(node)
56
+ return super if @vue_setup
57
+ @vue_setup = true
58
+ if @vue_h
59
+ if node.type == :begin
60
+ begin
61
+ @vue_apply = true
62
+ process s(:send, s(:block, s(:send, nil, :proc),
63
+ s(:args, s(:shadowarg, :$_)),
64
+ s(:begin, s(:lvasgn, :$_, s(:array)), process(node),
65
+ s(:return, s(:gvar, :$_)))), :[])
66
+ ensure
67
+ @vue_apply = nil
68
+ end
69
+ else
70
+ process s(:array, node)
71
+ end
72
+ else
73
+ process node
74
+ end
75
+ end
76
+
49
77
  # Example conversion
50
78
  # before:
51
79
  # (class (const nil :Foo) (const nil :Vue) nil)
@@ -92,7 +120,7 @@ module Ruby2JS
92
120
  end
93
121
 
94
122
  @vue_inventory = vue_walk(node)
95
- @vue_methods = []
123
+ @vue_methods.clear
96
124
  @vue_props = []
97
125
  @vue_reactive = []
98
126
 
@@ -112,7 +140,7 @@ module Ruby2JS
112
140
  end
113
141
  end
114
142
 
115
- elsif
143
+ elsif \
116
144
  statement.type == :send and statement.children[0] == cname and
117
145
  statement.children[1].to_s.end_with? '='
118
146
  then
@@ -133,13 +161,13 @@ module Ruby2JS
133
161
  hash << s(:pair, s(:sym, statement.children[1]),
134
162
  statement.children[2])
135
163
 
136
- elsif
164
+ elsif \
137
165
  statement.children[1] == :options and
138
166
  statement.children[2].type == :hash
139
167
  then
140
168
  options += statement.children[2].children
141
169
 
142
- elsif
170
+ elsif \
143
171
  statement.children[1] == :el and
144
172
  statement.children[2].type == :str
145
173
  then
@@ -162,7 +190,7 @@ module Ruby2JS
162
190
 
163
191
  block = s(:begin, block) unless block and block.type == :begin
164
192
 
165
- if
193
+ if \
166
194
  (block.children.length != 1 and
167
195
  not vue_wunderbar_free(block.children[0..-2])) or
168
196
 
@@ -280,7 +308,7 @@ module Ruby2JS
280
308
  hash << s(:pair, s(:sym, :methods), s(:hash, *methods))
281
309
  end
282
310
 
283
- @vue_methods = []
311
+ @vue_methods.clear
284
312
 
285
313
  # append setters to computed list
286
314
  setters.each do |setter|
@@ -375,7 +403,7 @@ module Ruby2JS
375
403
  end
376
404
  end
377
405
 
378
- elsif
406
+ elsif \
379
407
  class_methods.any? do |other_method|
380
408
  other_method.children[1].to_s == "#{method.children[1]}="
381
409
  end
@@ -517,7 +545,7 @@ module Ruby2JS
517
545
  *values.join(' ').split(' ').
518
546
  map {|str| s(:pair, s(:str, str), s(:true))})
519
547
  else
520
- if
548
+ if \
521
549
  expr.type == :if and expr.children[1] and
522
550
  expr.children[1].type == :str
523
551
  then
@@ -819,7 +847,7 @@ module Ruby2JS
819
847
  s(:splat, s(:array, *process_all(node.children[2..-1])))
820
848
  end
821
849
 
822
- elsif
850
+ elsif \
823
851
  node.children[1] == :createElement and
824
852
  node.children[0] == s(:const, nil, :Vue)
825
853
  then
@@ -839,7 +867,7 @@ module Ruby2JS
839
867
  element
840
868
  end
841
869
 
842
- elsif
870
+ elsif \
843
871
  @vue_self and VUE_METHODS.include? node.children[1] and
844
872
  node.children[0] == s(:const, nil, :Vue)
845
873
  then
@@ -901,7 +929,7 @@ module Ruby2JS
901
929
  prev = nil
902
930
  (node.children.length-1).downto(0) do |i|
903
931
  child = node.children[i]
904
- if
932
+ if \
905
933
  child.type == :send and child.children[0] == s(:gvar, :$_) and
906
934
  child.children[1] == :push
907
935
  then
@@ -940,7 +968,7 @@ module Ruby2JS
940
968
  child = node.children.first
941
969
 
942
970
  # map Vue.render(el, &block) to Vue.new(el: el, render: block)
943
- if
971
+ if \
944
972
  child.children[1] == :render and
945
973
  child.children[0] == s(:const, nil, :Vue)
946
974
  then
@@ -951,7 +979,7 @@ module Ruby2JS
951
979
  block = node.children[2]
952
980
  block = s(:begin, block) unless block and block.type == :begin
953
981
 
954
- if
982
+ if \
955
983
  block.children.length != 1 or not block.children.last or
956
984
  not [:send, :block].include? block.children.first.type
957
985
  then
@@ -971,7 +999,7 @@ module Ruby2JS
971
999
 
972
1000
  return super unless @vue_h
973
1001
 
974
- if
1002
+ if \
975
1003
  child.children[1] == :createElement and
976
1004
  child.children[0] == s(:const, nil, :Vue)
977
1005
  then
@@ -1031,7 +1059,7 @@ module Ruby2JS
1031
1059
  *node.children[1..-1]))
1032
1060
  end
1033
1061
 
1034
- elsif
1062
+ elsif \
1035
1063
  node.children.first.children.last == :forEach and
1036
1064
  vue_element?(node.children.last)
1037
1065
  then
@@ -1116,7 +1144,7 @@ module Ruby2JS
1116
1144
  node.children[0].children[0].to_s[1..-1]),
1117
1145
  node.children[1], process(node.children[2])]
1118
1146
 
1119
- elsif
1147
+ elsif \
1120
1148
  node.children.first.type == :lvasgn and
1121
1149
  @vue_props.include? node.children[0].children[0]
1122
1150
  then
@@ -0,0 +1,63 @@
1
+ require 'ruby2js'
2
+
3
+ module Ruby2JS
4
+ module Filter
5
+ module Wunderbar
6
+ include SEXP
7
+
8
+ def on_send(node)
9
+ target, method, *attrs = node.children
10
+
11
+ if target == s(:const, nil, :Wunderbar)
12
+ if [:debug, :info, :warn, :error, :fatal].include? method
13
+ method = :error if method == :fatal
14
+ return node.updated(nil, [s(:const, nil, :console), method, *attrs])
15
+ end
16
+ end
17
+
18
+ stack = []
19
+ while target!=nil and target.type==:send and target.children.length==2
20
+ name = method.to_s
21
+ if name.end_with? '!'
22
+ stack << s(:hash, s(:pair, s(:sym, :id), s(:str, name[0..-2])))
23
+ else
24
+ stack << s(:hash, s(:pair, s(:sym, :class), s(:str, name)))
25
+ end
26
+ target, method = target.children
27
+ end
28
+
29
+ if target == nil and method.to_s.start_with? "_"
30
+ S(:xnode, *method.to_s[1..-1], *stack, *process_all(attrs))
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ def on_block(node)
37
+ send, args, *block = node.children
38
+ target, method, *_ = send.children
39
+ while target!=nil and target.type==:send and target.children.length==2
40
+ target, method = target.children
41
+ end
42
+
43
+ if target == nil and method.to_s.start_with? "_"
44
+ if args.children.empty?
45
+ # append block as a standalone proc
46
+ process send.updated(nil, [*send.children, *process_all(block)])
47
+ else
48
+ # iterate over Enumerable arguments if there are args present
49
+ send = send.children
50
+ return super if send.length < 3
51
+ process s(:block, s(:send, *send[0..1], *send[3..-1]),
52
+ s(:args), s(:block, s(:send, send[2], :map),
53
+ *node.children[1..-1]))
54
+ end
55
+ else
56
+ super
57
+ end
58
+ end
59
+ end
60
+
61
+ DEFAULTS.push Wunderbar
62
+ end
63
+ end
@@ -32,8 +32,10 @@ module Ruby2JS
32
32
  ''
33
33
  elsif ['case ', 'default:'].include? self[0]
34
34
  ' ' * ([0,indent-2].max) + join
35
- else
35
+ elsif indent > 0
36
36
  ' ' * indent + join
37
+ else
38
+ join
37
39
  end
38
40
  end
39
41
  end
@@ -58,7 +60,6 @@ module Ruby2JS
58
60
 
59
61
  def timestamp(file)
60
62
  if file
61
- file = file.dup.untaint
62
63
  @timestamps[file] = File.mtime(file) if File.exist?(file)
63
64
  end
64
65
  end
@@ -87,9 +88,21 @@ module Ruby2JS
87
88
  first = line.find {|token| !token.empty?}
88
89
  if first
89
90
  last = line[line.rindex {|token| !token.empty?}]
90
- indent -= @indent if ')}]'.include? first[0] and indent >= @indent
91
- line.indent = indent
92
- indent += @indent if '({['.include? last[-1]
91
+ if (first.start_with? '<' and line.include? '>') or
92
+ (last.end_with? '>' and line.include? '<')
93
+ then
94
+ node = line.join[/.*?(<.*)/, 1]
95
+ indent -= @indent if node.start_with? '</'
96
+
97
+ line.indent = indent
98
+
99
+ node = line.join[/.*(<.*)/, 1]
100
+ indent += @indent unless node.include? '</' or node.include? '/>'
101
+ else
102
+ indent -= @indent if ')}]'.include? first[0] and indent >= @indent
103
+ line.indent = indent
104
+ indent += @indent if '({['.include? last[-1]
105
+ end
93
106
  else
94
107
  line.indent = indent
95
108
  end
@@ -102,24 +115,24 @@ module Ruby2JS
102
115
  reindent @lines
103
116
 
104
117
  (@lines.length-3).downto(0) do |i|
105
- if
118
+ if \
106
119
  @lines[i].length == 0
107
120
  then
108
121
  @lines.delete i
109
- elsif
122
+ elsif \
110
123
  @lines[i+1].comment? and not @lines[i].comment? and
111
124
  @lines[i].indent == @lines[i+1].indent
112
125
  then
113
126
  # before a comment
114
127
  @lines.insert i+1, Line.new
115
- elsif
128
+ elsif \
116
129
  @lines[i].indent == @lines[i+1].indent and
117
130
  @lines[i+1].indent < @lines[i+2].indent and
118
131
  not @lines[i].comment?
119
132
  then
120
133
  # start of indented block
121
134
  @lines.insert i+1, Line.new
122
- elsif
135
+ elsif \
123
136
  @lines[i].indent > @lines[i+1].indent and
124
137
  @lines[i+1].indent == @lines[i+2].indent and
125
138
  not @lines[i+2].empty?
@@ -136,7 +149,8 @@ module Ruby2JS
136
149
  @line << Token.new(string, @ast)
137
150
  else
138
151
  parts = string.split("\n")
139
- @line << Token.new(parts.shift, @ast)
152
+ first = parts.shift
153
+ @line << Token.new(first, @ast) if first
140
154
  @lines += parts.map {|part| Line.new(Token.new(part, @ast))}
141
155
  @lines << Line.new if string.end_with?("\n")
142
156
  @line = @lines.last
@@ -208,7 +222,7 @@ module Ruby2JS
208
222
  mark = output_location
209
223
  yield
210
224
 
211
- if
225
+ if \
212
226
  @lines.length > mark.first+1 or
213
227
  @lines[mark.first-1].join.length + @line.join.length >= @width
214
228
  then
@@ -1,8 +1,8 @@
1
1
  module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
- MINOR = 0
5
- TINY = 15
4
+ MINOR = 3
5
+ TINY = 0
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.15
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-22 00:00:00.000000000 Z
11
+ date: 2020-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -60,6 +60,7 @@ files:
60
60
  - lib/ruby2js/converter/for.rb
61
61
  - lib/ruby2js/converter/hash.rb
62
62
  - lib/ruby2js/converter/if.rb
63
+ - lib/ruby2js/converter/import.rb
63
64
  - lib/ruby2js/converter/in.rb
64
65
  - lib/ruby2js/converter/ivar.rb
65
66
  - lib/ruby2js/converter/ivasgn.rb
@@ -86,6 +87,7 @@ files:
86
87
  - lib/ruby2js/converter/vasgn.rb
87
88
  - lib/ruby2js/converter/while.rb
88
89
  - lib/ruby2js/converter/whilepost.rb
90
+ - lib/ruby2js/converter/xnode.rb
89
91
  - lib/ruby2js/converter/xstr.rb
90
92
  - lib/ruby2js/es2015.rb
91
93
  - lib/ruby2js/es2015/strict.rb
@@ -93,12 +95,22 @@ files:
93
95
  - lib/ruby2js/es2016/strict.rb
94
96
  - lib/ruby2js/es2017.rb
95
97
  - lib/ruby2js/es2017/strict.rb
98
+ - lib/ruby2js/es2018.rb
99
+ - lib/ruby2js/es2018/strict.rb
100
+ - lib/ruby2js/es2019.rb
101
+ - lib/ruby2js/es2019/strict.rb
102
+ - lib/ruby2js/es2020.rb
103
+ - lib/ruby2js/es2020/strict.rb
104
+ - lib/ruby2js/es2021.rb
105
+ - lib/ruby2js/es2021/strict.rb
96
106
  - lib/ruby2js/execjs.rb
97
107
  - lib/ruby2js/filter.rb
98
108
  - lib/ruby2js/filter/camelCase.rb
99
109
  - lib/ruby2js/filter/cjs.rb
110
+ - lib/ruby2js/filter/esm.rb
100
111
  - lib/ruby2js/filter/functions.rb
101
112
  - lib/ruby2js/filter/jquery.rb
113
+ - lib/ruby2js/filter/matchAll.rb
102
114
  - lib/ruby2js/filter/minitest-jasmine.rb
103
115
  - lib/ruby2js/filter/node.rb
104
116
  - lib/ruby2js/filter/nokogiri.rb
@@ -108,6 +120,7 @@ files:
108
120
  - lib/ruby2js/filter/rubyjs.rb
109
121
  - lib/ruby2js/filter/underscore.rb
110
122
  - lib/ruby2js/filter/vue.rb
123
+ - lib/ruby2js/filter/wunderbar.rb
111
124
  - lib/ruby2js/haml.rb
112
125
  - lib/ruby2js/rails.rb
113
126
  - lib/ruby2js/serializer.rb
@@ -134,8 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
147
  - !ruby/object:Gem::Version
135
148
  version: '0'
136
149
  requirements: []
137
- rubyforge_project:
138
- rubygems_version: 2.7.6
150
+ rubygems_version: 3.1.2
139
151
  signing_key:
140
152
  specification_version: 4
141
153
  summary: Minimal yet extensible Ruby to JavaScript conversion.