ruby2js 3.3.4 → 3.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -528,6 +528,9 @@ module Ruby2JS
528
528
  elsif method == :new and target == s(:const, nil, :Exception)
529
529
  process S(:send, s(:const, nil, :Error), :new, *args)
530
530
 
531
+ elsif method == :block_given? and target == nil and args.length == 0
532
+ process process s(:lvar, "_implicitBlockYield")
533
+
531
534
  else
532
535
  super
533
536
  end
@@ -538,7 +541,7 @@ module Ruby2JS
538
541
  method = call.children[1]
539
542
  return super if excluded?(method)
540
543
 
541
- if [:setInterval, :setTimeout].include? method
544
+ if [:setInterval, :setTimeout, :set_interval, :set_timeout].include? method
542
545
  return super unless call.children.first == nil
543
546
  block = process s(:block, s(:send, nil, :proc), *node.children[1..-1])
544
547
  on_send call.updated nil, [*call.children[0..1], block,
@@ -703,6 +706,17 @@ module Ruby2JS
703
706
  process call.updated(nil, [*call.children, s(:block,
704
707
  s(:send, nil, :proc), *node.children[1..-1])])
705
708
 
709
+ elsif method == :yield_self and call.children.length == 2
710
+ process node.updated(:send, [s(:block, s(:send, nil, :proc),
711
+ node.children[1], s(:autoreturn, node.children[2])),
712
+ :[], call.children[0]])
713
+
714
+ elsif method == :tap and call.children.length == 2
715
+ process node.updated(:send, [s(:block, s(:send, nil, :proc),
716
+ node.children[1], s(:begin, node.children[2],
717
+ s(:return, s(:lvar, node.children[1].children[0].children[0])))),
718
+ :[], call.children[0]])
719
+
706
720
  else
707
721
  super
708
722
  end
@@ -7,7 +7,7 @@ module Ruby2JS
7
7
  include SEXP
8
8
  extend SEXP
9
9
 
10
- NODE_SETUP = {
10
+ CJS_SETUP = {
11
11
  child_process: s(:casgn, nil, :child_process,
12
12
  s(:send, nil, :require, s(:str, "child_process"))),
13
13
  fs: s(:casgn, nil, :fs, s(:send, nil, :require, s(:str, "fs"))),
@@ -15,6 +15,13 @@ module Ruby2JS
15
15
  s(:attr, nil, :process), :argv), :slice, s(:int, 2)))
16
16
  }
17
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
+
18
25
  def initialize(*args)
19
26
  @node_setup = nil
20
27
  super
@@ -28,8 +35,8 @@ module Ruby2JS
28
35
  if @node_setup.empty?
29
36
  result
30
37
  else
31
- s(:begin, *@node_setup.to_a.map {|token| NODE_SETUP[token]},
32
- result)
38
+ setup = @esm ? ESM_SETUP : CJS_SETUP;
39
+ s(:begin, *@node_setup.to_a.map {|token| setup[token]}, result)
33
40
  end
34
41
  end
35
42
 
@@ -7,11 +7,15 @@ module Ruby2JS
7
7
  include SEXP
8
8
  extend SEXP
9
9
 
10
- NOKOGIRI_SETUP = {
10
+ CJS_SETUP = {
11
11
  jsdom: s(:casgn, nil, :JSDOM,
12
12
  s(:attr, s(:send, nil, :require, s(:str, "jsdom")), :JSDOM))
13
13
  }
14
14
 
15
+ ESM_SETUP = {
16
+ jsdom: s(:import, ["jsdom"], [s(:attr, nil, :JSDOM)])
17
+ }
18
+
15
19
  def initialize(*args)
16
20
  @nokogiri_setup = nil
17
21
  super
@@ -25,9 +29,9 @@ module Ruby2JS
25
29
  if @nokogiri_setup.empty?
26
30
  result
27
31
  else
32
+ setup = @esm ? ESM_SETUP : CJS_SETUP;
28
33
  s(:begin,
29
- *@nokogiri_setup.to_a.map {|token| NOKOGIRI_SETUP[token]},
30
- result)
34
+ *@nokogiri_setup.to_a.map {|token| setup[token]}, result)
31
35
  end
32
36
  end
33
37
 
@@ -38,8 +38,6 @@ module Ruby2JS
38
38
 
39
39
  filename = File.join(dirname, basename)
40
40
 
41
- segments = basename.split(/[\/\\]/)
42
-
43
41
  if not File.file? filename and File.file? filename+".rb"
44
42
  filename += '.rb'
45
43
  elsif not File.file? filename and File.file? filename+".js.rb"
@@ -8,16 +8,20 @@ module Ruby2JS
8
8
  EXPRESSIONS = [ :array, :float, :hash, :if, :int, :lvar, :nil, :send ]
9
9
 
10
10
  def on_block(node)
11
- children = process_all(node.children)
11
+ node = super
12
+ return node unless node.type == :block
13
+ children = node.children.dup
12
14
 
13
15
  children[-1] = s(:nil) if children.last == nil
14
16
 
15
- node.updated nil, [*node.children[0..1],
17
+ node.updated nil, [*children[0..1],
16
18
  s(:autoreturn, *children[2..-1])]
17
19
  end
18
20
 
19
21
  def on_def(node)
20
- children = process_all(node.children[1..-1])
22
+ node = super
23
+ return node unless node.type == :def
24
+ children = node.children[1..-1]
21
25
 
22
26
  children[-1] = s(:nil) if children.last == nil
23
27
 
@@ -0,0 +1,40 @@
1
+ require 'ruby2js'
2
+
3
+ module Ruby2JS
4
+ module Filter
5
+ module TaggedTemplates
6
+ include SEXP
7
+
8
+ def initialize(*args)
9
+ super
10
+ end
11
+
12
+ def on_send(node)
13
+ target, method, *args = node.children
14
+ return super unless target.nil? and es2015
15
+
16
+ tagged_methods = @options[:template_literal_tags] || [:html, :css]
17
+
18
+ if tagged_methods.include?(method) && !args.empty?
19
+ strnode = process args.first
20
+ if strnode.type == :str
21
+ # convert regular strings to literal strings
22
+ strnode = strnode.updated(:dstr, [s(:str, strnode.children.first.chomp("\n"))])
23
+ else
24
+ # for literal strings, chomp a newline off the end
25
+ if strnode.children.last.type == :str && strnode.children.last.children[0].end_with?("\n")
26
+ children = [*strnode.children.take(strnode.children.length - 1), s(:str, strnode.children.last.children[0].chomp)]
27
+ strnode = s(:dstr, *children)
28
+ end
29
+ end
30
+
31
+ S(:taglit, s(:sym, method), strnode)
32
+ else
33
+ super
34
+ end
35
+ end
36
+ end
37
+
38
+ DEFAULTS.push TaggedTemplates
39
+ end
40
+ end
@@ -355,7 +355,7 @@ module Ruby2JS
355
355
  end
356
356
 
357
357
  split = buffer.source[0...pos].split("\n")
358
- vlq row, col, source_index, split.length-1, split.last.to_s.length
358
+ vlq row, col, source_index, [split.length - 1, 0].max, split.last.to_s.length
359
359
  end
360
360
  col += token.length
361
361
  end
@@ -1,8 +1,8 @@
1
1
  module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
- MINOR = 3
5
- TINY = 4
4
+ MINOR = 5
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.files = %w(ruby2js.gemspec README.md) + 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.0".freeze)
18
+ s.required_ruby_version = Gem::Requirement.new(">= 2.3".freeze)
19
19
  s.summary = "Minimal yet extensible Ruby to JavaScript conversion.".freeze
20
20
 
21
21
  s.add_dependency('parser')
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.3.4
4
+ version: 3.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-07 00:00:00.000000000 Z
11
+ date: 2020-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -94,6 +94,7 @@ files:
94
94
  - lib/ruby2js/converter/send.rb
95
95
  - lib/ruby2js/converter/super.rb
96
96
  - lib/ruby2js/converter/sym.rb
97
+ - lib/ruby2js/converter/taglit.rb
97
98
  - lib/ruby2js/converter/undef.rb
98
99
  - lib/ruby2js/converter/until.rb
99
100
  - lib/ruby2js/converter/untilpost.rb
@@ -103,6 +104,7 @@ files:
103
104
  - lib/ruby2js/converter/whilepost.rb
104
105
  - lib/ruby2js/converter/xnode.rb
105
106
  - lib/ruby2js/converter/xstr.rb
107
+ - lib/ruby2js/converter/yield.rb
106
108
  - lib/ruby2js/es2015.rb
107
109
  - lib/ruby2js/es2015/strict.rb
108
110
  - lib/ruby2js/es2016.rb
@@ -122,6 +124,8 @@ files:
122
124
  - lib/ruby2js/filter/camelCase.rb
123
125
  - lib/ruby2js/filter/cjs.rb
124
126
  - lib/ruby2js/filter/esm.rb
127
+ - lib/ruby2js/filter/esm_migration.rb
128
+ - lib/ruby2js/filter/fast-deep-equal.rb
125
129
  - lib/ruby2js/filter/functions.rb
126
130
  - lib/ruby2js/filter/jquery.rb
127
131
  - lib/ruby2js/filter/matchAll.rb
@@ -131,7 +135,7 @@ files:
131
135
  - lib/ruby2js/filter/react.rb
132
136
  - lib/ruby2js/filter/require.rb
133
137
  - lib/ruby2js/filter/return.rb
134
- - lib/ruby2js/filter/rubyjs.rb
138
+ - lib/ruby2js/filter/tagged_templates.rb
135
139
  - lib/ruby2js/filter/underscore.rb
136
140
  - lib/ruby2js/filter/vue.rb
137
141
  - lib/ruby2js/filter/wunderbar.rb
@@ -154,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
154
158
  requirements:
155
159
  - - ">="
156
160
  - !ruby/object:Gem::Version
157
- version: '2.0'
161
+ version: '2.3'
158
162
  required_rubygems_version: !ruby/object:Gem::Requirement
159
163
  requirements:
160
164
  - - ">="
@@ -1,112 +0,0 @@
1
- require 'ruby2js'
2
-
3
- module Ruby2JS
4
- module Filter
5
- module RubyJS
6
- include SEXP
7
-
8
- def on_send(node)
9
- # leave functional style calls alone
10
- target = node.children.first
11
- return super if target and [:_s, :_a, :_h, :_n, :_i, :_t].
12
- include? target.children[1]
13
-
14
- method = node.children[1]
15
- return super if excluded?(method)
16
-
17
- # leave classic ("OO") style call chains alone
18
- while target and target.type == :send
19
- return super if target.children[1] == :R
20
- target = target.children.first
21
- end
22
-
23
- if \
24
- [:capitalize, :center, :chomp, :ljust, :lstrip, :rindex, :rjust,
25
- :rstrip, :scan, :swapcase, :tr].include? method
26
- then
27
- # map selected string functions
28
- s(:send, s(:lvar, :_s), method,
29
- *process_all([node.children[0], *node.children[2..-1]]))
30
-
31
- elsif \
32
- [:at, :compact, :compact!, :delete_at, :delete_at, :flatten, :insert,
33
- :reverse, :reverse!, :rotate, :rotate, :rotate!, :shift, :shuffle,
34
- :shuffle!, :slice, :slice!, :transpose, :union, :uniq, :uniq!]
35
- .include? method
36
- then
37
- # map selected array functions
38
- s(:send, s(:lvar, :_a), method.to_s.sub("!", '_bang'),
39
- *process_all([node.children[0], *node.children[2..-1]]))
40
-
41
-
42
- elsif [:strftime].include? method
43
- # map selected time functions
44
- s(:send, s(:lvar, :_t), method,
45
- *process_all([node.children[0], *node.children[2..-1]]))
46
-
47
- elsif method == :<=>
48
- s(:send, s(:attr, s(:const, nil, :R), :Comparable), :cmp,
49
- node.children[0], *node.children[2..-1])
50
-
51
- elsif method == :between?
52
- s(:send, s(:send, nil, :R, node.children[0]), :between,
53
- *node.children[2..-1])
54
-
55
- else
56
- super
57
- end
58
- end
59
-
60
- def on_block(node)
61
- call, args, *block = node.children
62
- method = call.children[1]
63
- return super if excluded?(method)
64
-
65
- if \
66
- [:collect_concat, :count, :cycle, :delete_if, :drop_while,
67
- :each_index, :each_slice, :each_with_index, :each_with_object,
68
- :find, :find_all, :flat_map, :inject, :grep, :group_by, :keep_if,
69
- :map, :max_by, :min_by, :one?, :partition, :reject, :reverse_each,
70
- :select!, :sort_by, :take_while].include? method
71
- then
72
- if \
73
- [:collect_concat, :count, :delete_if, :drop_while, :find,
74
- :find_all, :flat_map, :grep, :group_by, :keep_if, :map, :max_by,
75
- :min_by, :one?, :partition, :reject, :select!, :sort_by,
76
- :take_while].include? method
77
- then
78
- block = [ s(:autoreturn, *block) ]
79
- end
80
-
81
- lvar = [:each_index, :keep_if, :select!].include?(method) ? :_a : :_e
82
-
83
- if method == :find and call.children.length == 2
84
- call = s(:send, *call.children, s(:nil))
85
- elsif method == :inject and call.children.length == 3
86
- call = s(:send, *call.children, s(:nil))
87
- elsif method == :select!
88
- call = s(:send, call.children.first, :select_bang,
89
- *call.children[2..-1])
90
- end
91
-
92
- s(:block, s(:send, s(:lvar, lvar), call.children[1],
93
- *process_all([call.children[0], *call.children[2..-1]])),
94
- args, *block)
95
- else
96
- super
97
- end
98
- end
99
-
100
- def on_irange(node)
101
- s(:send, s(:attr, s(:const, nil, :R), :Range), :new, *node.children)
102
- end
103
-
104
- def on_erange(node)
105
- s(:send, s(:attr, s(:const, nil, :R), :Range), :new, *node.children,
106
- s(:true))
107
- end
108
- end
109
-
110
- DEFAULTS.push RubyJS
111
- end
112
- end