ruby2js 3.3.3 → 3.5.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.
@@ -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 = 3
4
+ MINOR = 5
5
+ TINY = 0
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.3
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-07 00:00:00.000000000 Z
11
+ date: 2020-10-31 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,7 @@ 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
125
128
  - lib/ruby2js/filter/functions.rb
126
129
  - lib/ruby2js/filter/jquery.rb
127
130
  - lib/ruby2js/filter/matchAll.rb
@@ -131,7 +134,7 @@ files:
131
134
  - lib/ruby2js/filter/react.rb
132
135
  - lib/ruby2js/filter/require.rb
133
136
  - lib/ruby2js/filter/return.rb
134
- - lib/ruby2js/filter/rubyjs.rb
137
+ - lib/ruby2js/filter/tagged_templates.rb
135
138
  - lib/ruby2js/filter/underscore.rb
136
139
  - lib/ruby2js/filter/vue.rb
137
140
  - lib/ruby2js/filter/wunderbar.rb
@@ -146,7 +149,7 @@ homepage: http://github.com/rubys/ruby2js
146
149
  licenses:
147
150
  - MIT
148
151
  metadata: {}
149
- post_install_message:
152
+ post_install_message:
150
153
  rdoc_options: []
151
154
  require_paths:
152
155
  - lib
@@ -154,15 +157,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
154
157
  requirements:
155
158
  - - ">="
156
159
  - !ruby/object:Gem::Version
157
- version: '2.0'
160
+ version: '2.3'
158
161
  required_rubygems_version: !ruby/object:Gem::Requirement
159
162
  requirements:
160
163
  - - ">="
161
164
  - !ruby/object:Gem::Version
162
165
  version: '0'
163
166
  requirements: []
164
- rubygems_version: 3.1.2
165
- signing_key:
167
+ rubygems_version: 3.2.0.rc.1
168
+ signing_key:
166
169
  specification_version: 4
167
170
  summary: Minimal yet extensible Ruby to JavaScript conversion.
168
171
  test_files: []
@@ -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