ruby2js 4.1.3 → 4.1.7

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: 447f78e50320a18d86c8b6812ab6e45c67c4b25972e1c0e36466a364440d0738
4
- data.tar.gz: f7b98307a552c859cea2e74724b5029796171a7ac843a953a0dc227340caf8f7
3
+ metadata.gz: 0e5086f1cd3eb09ea176eb28e0ad73eea87f47f111c24f5029b547883bda7fbd
4
+ data.tar.gz: d758cd6a5719aaba435ac7944bfbefc426e8d6ff98b41f7794c9dd6394847e6c
5
5
  SHA512:
6
- metadata.gz: e0ac7541cfe5adc0b2a0d434cac81a1b37d216f3b94a870380e47452133b33d2b19c38eb6b90d70ae1b9336b89cde7db91251572d3c40990fddac65a34cd9e00
7
- data.tar.gz: ad597409c14874aa0f42511f5f7795b49746385c739d27ff11ab0e392ad692963adac799c07583b0c953db303674bdf2dabdefd4c0dd776b43cbdc8c47ea3330
6
+ metadata.gz: 405b1b6c7eb8d62d767a3d756378bf1226dfe204d89eb24743eb3af6727e5a2950a2ae16c0d9dc8b0e352fd2a2390964cfb4ef074edc7f4820676ece42ce819c
7
+ data.tar.gz: 16557065963d7ad981a07e2695fd756956866beb45eada9f3dcfbbe7544b84791e366ccc2e65f2beb4cad6de484c04dbc7465b222853792a1219dfc9997eb466
@@ -169,7 +169,9 @@ module Ruby2JS
169
169
  put 'function'
170
170
  end
171
171
 
172
- put '('; parse args; put ") {#{nl}"
172
+ put '('
173
+ parse s(:args, *args.children.select {|arg| arg.type != :shadowarg})
174
+ put ") {#{nl}"
173
175
 
174
176
  next_token, @next_token = @next_token, :return
175
177
  @block_depth += 1 if @block_depth
@@ -69,16 +69,16 @@ module Ruby2JS
69
69
  left = rewrite(*left.children)
70
70
  end
71
71
 
72
- if right.type != :send
72
+ if right.type != :send or OPERATORS.flatten.include? right.children[1]
73
73
  s(:and, left, right)
74
74
  elsif conditionally_equals(left, right.children.first)
75
75
  # a && a.b => a&.b
76
- right.updated(:csend, [left, right.children.last])
76
+ right.updated(:csend, [left, *right.children[1..-1]])
77
77
  elsif conditionally_equals(left.children.last, right.children.first)
78
78
  # a && b && b.c => a && b&.c
79
79
  left.updated(:and, [left.children.first,
80
80
  left.children.last.updated(:csend,
81
- [left.children.last, right.children.last])])
81
+ [left.children.last, *right.children[1..-1]])])
82
82
  else
83
83
  s(:and, left, right)
84
84
  end
@@ -17,8 +17,8 @@ module Ruby2JS
17
17
 
18
18
  LOGICAL = :and, :not, :or
19
19
  OPERATORS = [:[], :[]=], [:not, :!], [:**], [:*, :/, :%], [:+, :-],
20
- [:>>, :<<], [:&], [:^, :|], [:<=, :<, :>, :>=], [:==, :!=, :===, :"!=="],
21
- [:and, :or]
20
+ [:>>, :<<], [:&], [:^, :|], [:<=, :<, :>, :>=],
21
+ [:==, :!=, :===, :"!==", :=~, :!~], [:and, :or]
22
22
 
23
23
  INVERT_OP = {
24
24
  :< => :>=,
@@ -7,7 +7,6 @@ module Ruby2JS
7
7
 
8
8
  def on_send(node)
9
9
  target, method, *args = node.children
10
- return super unless args.empty?
11
10
 
12
11
  if es2015 and method == :blank?
13
12
  create_or_update_import("blank$")
@@ -18,6 +17,15 @@ module Ruby2JS
18
17
  elsif es2015 and method == :presence
19
18
  create_or_update_import("presence$")
20
19
  process node.updated :send, [nil, "presence$", target]
20
+ elsif es2015 and method == :chomp
21
+ create_or_update_import("chomp$")
22
+ process node.updated :send, [nil, "chomp$", target, *args]
23
+ elsif es2015 and method == :delete_prefix
24
+ create_or_update_import("deletePrefix$")
25
+ process node.updated :send, [nil, "deletePrefix$", target, *args]
26
+ elsif es2015 and method == :delete_suffix
27
+ create_or_update_import("deleteSuffix$")
28
+ process node.updated :send, [nil, "deleteSuffix$", target, *args]
21
29
  else
22
30
  super
23
31
  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] =~ /_.*[?!\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
@@ -104,6 +104,10 @@ module Ruby2JS
104
104
  handle_generic_node(super, :arg)
105
105
  end
106
106
 
107
+ def on_kwarg(node)
108
+ handle_generic_node(super, :kwarg)
109
+ end
110
+
107
111
  def on_lvasgn(node)
108
112
  handle_generic_node(super, :lvasgn)
109
113
  end
@@ -132,7 +136,7 @@ module Ruby2JS
132
136
  node = super
133
137
  return node if node.type != :defs
134
138
 
135
- if node.children[1] =~ /_.*\w$/
139
+ if node.children[1] =~ /_.*[?!\w]$/
136
140
  S(:defs , node.children[0],
137
141
  camelCase(node.children[1]), *node.children[2..-1])
138
142
  else
@@ -341,7 +341,11 @@ module Ruby2JS
341
341
 
342
342
  elsif method == :last
343
343
  if node.children.length == 2
344
- process on_send S(:send, target, :[], s(:int, -1))
344
+ if es2022
345
+ process S(:send, target, :at, s(:int, -1))
346
+ else
347
+ process on_send S(:send, target, :[], s(:int, -1))
348
+ end
345
349
  elsif node.children.length == 3
346
350
  process S(:send, target, :slice,
347
351
  s(:send, s(:attr, target, :length), :-, node.children[2]),
@@ -358,8 +362,12 @@ module Ruby2JS
358
362
  # resolve negative literal indexes
359
363
  i = proc do |index|
360
364
  if index.type == :int and index.children.first < 0
361
- process S(:send, S(:attr, target, :length), :-,
362
- s(:int, -index.children.first))
365
+ if es2022
366
+ return process S(:send, target, :at, index)
367
+ else
368
+ process S(:send, S(:attr, target, :length), :-,
369
+ s(:int, -index.children.first))
370
+ end
363
371
  else
364
372
  index
365
373
  end
@@ -36,7 +36,7 @@ module Ruby2JS
36
36
  end
37
37
 
38
38
  def on_class(node)
39
- cname, inheritance, *body = node.children
39
+ _, inheritance, *body = node.children
40
40
  return super unless inheritance == s(:const, nil, :LitElement)
41
41
 
42
42
  @le_props = {}
@@ -1492,7 +1492,13 @@ module Ruby2JS
1492
1492
  source = Ruby2JS.jsx2_rb(source)
1493
1493
  ast = Ruby2JS.parse(source).first
1494
1494
  ast = s(:block, s(:send, nil, :_), s(:args), ast) if ast.type == :begin
1495
- process ast
1495
+
1496
+ begin
1497
+ react, @react = @react, @react || :react
1498
+ process ast
1499
+ ensure
1500
+ @react = react
1501
+ end
1496
1502
  end
1497
1503
  end
1498
1504
 
@@ -9,12 +9,12 @@ module Ruby2JS
9
9
 
10
10
  STIMULUS_IMPORT = s(:import,
11
11
  [s(:pair, s(:sym, :as), s(:const, nil, :Stimulus)),
12
- s(:pair, s(:sym, :from), s(:str, "stimulus"))],
12
+ s(:pair, s(:sym, :from), s(:str, "@hotwired/stimulus"))],
13
13
  s(:str, '*'))
14
14
 
15
15
  STIMULUS_IMPORT_SKYPACK = s(:import,
16
16
  [s(:pair, s(:sym, :as), s(:const, nil, :Stimulus)),
17
- s(:pair, s(:sym, :from), s(:str, "https://cdn.skypack.dev/stimulus"))],
17
+ s(:pair, s(:sym, :from), s(:str, "https://cdn.skypack.dev/@hotwired/stimulus"))],
18
18
  s(:str, '*'))
19
19
 
20
20
  def initialize(*args)
@@ -18,6 +18,12 @@ module Ruby2JS
18
18
  else
19
19
  super
20
20
  end
21
+ elsif [:take, :drop].include? method and node.is_method?
22
+ process S(:send, s(:lvar, :_), method, node.children[0], node.children[2])
23
+ elsif method == :each_slice and node.is_method?
24
+ process S(:send, s(:lvar, :_), :chunk, node.children[0], node.children[2])
25
+ elsif [:min, :max].include? method and node.children.length == 2
26
+ process S(:send, s(:lvar, :_), method, node.children[0])
21
27
  elsif method == :sample and node.children.length <= 3
22
28
  process S(:send, s(:lvar, :_), :sample, node.children[0],
23
29
  *node.children[2..-1])
@@ -62,14 +68,14 @@ module Ruby2JS
62
68
  elsif method == :reduce
63
69
  if node.children.length == 3 and node.children[2].type == :sym
64
70
  # input: a.reduce(:+)
65
- # output: _.reduce(_.rest(a),
71
+ # output: _.reduce(_.rest(a),
66
72
  # proc {|memo, item| return memo+item},
67
73
  # a[0])
68
- process S(:send, s(:lvar, :_), :reduce,
74
+ process S(:send, s(:lvar, :_), :reduce,
69
75
  s(:send, s(:lvar, :_), :rest, node.children.first),
70
- s(:block, s(:send, nil, :proc),
76
+ s(:block, s(:send, nil, :proc),
71
77
  s(:args, s(:arg, :memo), s(:arg, :item)),
72
- s(:autoreturn, s(:send, s(:lvar, :memo),
78
+ s(:autoreturn, s(:send, s(:lvar, :memo),
73
79
  node.children[2].children.first, s(:lvar, :item)))),
74
80
  s(:send, node.children.first, :[], s(:int, 0)))
75
81
  elsif node.children.last.type == :block_pass
@@ -79,9 +85,9 @@ module Ruby2JS
79
85
  # input: a.reduce(n, :+)
80
86
  # output: _.reduce(a, proc {|memo, item| return memo+item}, n)
81
87
  process S(:send, s(:lvar, :_), :reduce, node.children.first,
82
- s(:block, s(:send, nil, :proc),
88
+ s(:block, s(:send, nil, :proc),
83
89
  s(:args, s(:arg, :memo), s(:arg, :item)),
84
- s(:autoreturn, s(:send, s(:lvar, :memo),
90
+ s(:autoreturn, s(:send, s(:lvar, :memo),
85
91
  node.children[3].children.first, s(:lvar, :item)))),
86
92
  node.children[2])
87
93
  else
@@ -93,8 +99,8 @@ module Ruby2JS
93
99
  # input: a.compact!
94
100
  # output: a.splice(0, a.length, *a.compact)
95
101
  target = node.children.first
96
- process S(:send, target, :splice, s(:int, 0),
97
- s(:attr, target, :length), s(:splat, s(:send, target,
102
+ process S(:send, target, :splice, s(:int, 0),
103
+ s(:attr, target, :length), s(:splat, s(:send, target,
98
104
  :"#{method.to_s[0..-2]}", *node.children[2..-1])))
99
105
  else
100
106
  super
@@ -111,14 +117,14 @@ module Ruby2JS
111
117
  # output: _.sortBy {return expression}
112
118
  method = method.to_s.sub(/\_by$/,'By').to_sym
113
119
  process S(:block, s(:send, s(:lvar, :_), method,
114
- call.children.first), node.children[1],
120
+ call.children.first), node.children[1],
115
121
  s(:autoreturn, node.children[2]))
116
122
  elsif [:find, :reject].include? method
117
123
  if call.children.length == 2
118
124
  # input: a.find {|item| item > 0}
119
125
  # output: _.find(a) {|item| return item > 0}
120
- process S(:block, s(:send, s(:lvar, :_), method,
121
- call.children.first), node.children[1],
126
+ process S(:block, s(:send, s(:lvar, :_), method,
127
+ call.children.first), node.children[1],
122
128
  s(:autoreturn, node.children[2]))
123
129
  else
124
130
  super
@@ -127,25 +133,25 @@ module Ruby2JS
127
133
  elsif method == :times and call.children.length == 2
128
134
  # input: 5.times {|i| console.log i}
129
135
  # output: _.find(5) {|i| console.log(i)}
130
- process S(:block, s(:send, s(:lvar, :_), method,
136
+ process S(:block, s(:send, s(:lvar, :_), method,
131
137
  call.children.first), node.children[1], node.children[2])
132
138
 
133
139
  elsif method == :reduce
134
140
  if call.children.length == 2
135
141
  # input: a.reduce {|memo, item| memo+item}
136
- # output: _.reduce(_.rest(a),
142
+ # output: _.reduce(_.rest(a),
137
143
  # proc {|memo, item| return memo+item},
138
144
  # a[0])
139
- process S(:call, s(:lvar, :_), :reduce,
145
+ process S(:call, s(:lvar, :_), :reduce,
140
146
  s(:send, s(:lvar, :_), :rest, call.children.first),
141
- s(:block, s(:send, nil, :proc),
147
+ s(:block, s(:send, nil, :proc),
142
148
  node.children[1], s(:autoreturn, node.children[2])),
143
149
  s(:send, call.children.first, :[], s(:int, 0)))
144
150
  elsif call.children.length == 3
145
151
  # input: a.reduce(n) {|memo, item| memo+item}
146
152
  # output: _.reduce(a, proc {|memo, item| return memo+item}, n)
147
153
  process S(:call, s(:lvar, :_), :reduce, call.children.first,
148
- s(:block, s(:send, nil, :proc),
154
+ s(:block, s(:send, nil, :proc),
149
155
  node.children[1], s(:autoreturn, node.children[2])),
150
156
  call.children[2])
151
157
  end
@@ -155,7 +161,7 @@ module Ruby2JS
155
161
  # output: a.splice(0, a.length, *a.map {expression})
156
162
  method = :"#{method.to_s[0..-2]}"
157
163
  target = call.children.first
158
- process S(:call, target, :splice, s(:splat, s(:send, s(:array,
164
+ process S(:call, target, :splice, s(:splat, s(:send, s(:array,
159
165
  s(:int, 0), s(:attr, target, :length)), :concat,
160
166
  s(:block, s(:send, target, method, *call.children[2..-1]),
161
167
  *node.children[1..-1]))))
@@ -2,7 +2,7 @@ module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 4
4
4
  MINOR = 1
5
- TINY = 3
5
+ TINY = 7
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -0,0 +1,43 @@
1
+ # install rollup plugin
2
+ run "yarn add @ruby2js/rollup-plugin"
3
+
4
+ # configure rollup for ruby2js with stimulus filters
5
+ insert_into_file Rails.root.join("rollup.config.js").to_s,
6
+ "import ruby2js from '@ruby2js/rollup-plugin';\n",
7
+ after: /import resolve from .*\n/
8
+
9
+ insert_into_file Rails.root.join("rollup.config.js").to_s,
10
+ <<-CONFIG, after: "resolve()\n"
11
+ ,ruby2js({
12
+ eslevel: 2020,
13
+ autoexports: 'default',
14
+ filters: ['stimulus', 'esm', 'functions']
15
+ })
16
+ CONFIG
17
+
18
+ # monkey patch stimulus:manifest:update to find .rb.js controllers too
19
+ append_to_file Rails.root.join('config/application.rb').to_s,
20
+ "\n" + <<~'CONFIG'
21
+ require 'stimulus/manifest'
22
+
23
+ module Stimulus::Manifest
24
+ def import_and_register_controller(controllers_path, controller_path)
25
+ controller_path = controller_path.relative_path_from(controllers_path).to_s
26
+ module_path = controller_path.split('.').first
27
+ controller_class_name = module_path.camelize.gsub(/::/, "__")
28
+ tag_name = module_path.remove(/_controller/).gsub(/_/, "-").gsub(/\//, "--")
29
+
30
+ <<~JS
31
+
32
+ import #{controller_class_name} from "./#{controller_path}"
33
+ application.register("#{tag_name}", #{controller_class_name})
34
+ JS
35
+ end
36
+
37
+ def extract_controllers_from(directory)
38
+ (directory.children.select { |e| e.to_s =~ /_controller\.js(\.\w+)?$/ } +
39
+ directory.children.select(&:directory?).collect { |d| extract_controllers_from(d) }
40
+ ).flatten.sort
41
+ end
42
+ end
43
+ CONFIG
@@ -1,32 +1,25 @@
1
1
  create_file Rails.root.join('config/initializers/ruby2js.rb').to_s,
2
2
  <<~CONFIG
3
- require 'ruby2js/filter/esm'
4
- require 'ruby2js/filter/functions'
5
- require 'ruby2js/filter/stimulus'
3
+ require 'stimulus/manifest'
6
4
 
7
- Ruby2JS::SprocketsTransformer.options = {
8
- autoexports: :default,
9
- eslevel: 2020
10
- }
5
+ module Stimulus::Manifest
6
+ def import_and_register_controller(controllers_path, controller_path)
7
+ controller_path = controller_path.relative_path_from(controllers_path).to_s
8
+ module_path = controller_path.split('.').first
9
+ controller_class_name = module_path.camelize.gsub(/::/, "__")
10
+ tag_name = module_path.remove(/_controller/).gsub(/_/, "-").gsub(/\//, "--")
11
11
 
12
- require 'stimulus/importmap_helper'
12
+ <<~JS
13
13
 
14
- module Stimulus::ImportmapHelper
15
- def find_javascript_files_in_tree(path)
16
- exts = {'.js' => '.js', '.jsm' => '.jsm'}.merge(
17
- Sprockets.mime_exts.map {|key, value|
18
- next unless Sprockets.transformers[value]["application/javascript"]
19
- [key, '.js']
20
- }.compact.to_h)
21
-
22
- Dir[path.join('**/*')].map {|file|
23
- file_ext, web_ext = Sprockets::PathUtils.match_path_extname(file, exts)
24
- next unless file_ext
25
-
26
- next unless File.file? file
14
+ import #{controller_class_name} from "./#{controller_path}"
15
+ application.register("#{tag_name}", #{controller_class_name})
16
+ JS
17
+ end
27
18
 
28
- Pathname.new(file.chomp(file_ext) + web_ext)
29
- }.compact
19
+ def extract_controllers_from(directory)
20
+ (directory.children.select { |e| e.to_s =~ /_controller\.js(\.\w+)?$/ } +
21
+ directory.children.select(&:directory?).collect { |d| extract_controllers_from(d) }
22
+ ).flatten.sort
30
23
  end
31
24
  end
32
25
  CONFIG
@@ -3,3 +3,6 @@ eval IO.read "#{__dir__}/webpacker.rb"
3
3
 
4
4
  insert_into_file Rails.root.join("app/javascript/controllers/index.js").to_s,
5
5
  '(\\.rb)?', after: '_controller\\.js'
6
+
7
+ insert_into_file Rails.root.join("app/javascript/packs/application.js").to_s,
8
+ "import \"../controllers\"\n", after: "import \"channels\"\n"
@@ -1,4 +1,6 @@
1
- Thor::Actions::WARNINGS[:unchanged_no_flag] = 'unchanged'
1
+ if defined? Thor
2
+ Thor::Actions::WARNINGS[:unchanged_no_flag] = 'unchanged'
3
+ end
2
4
 
3
5
  def template(location)
4
6
  system "#{RbConfig.ruby} #{Rails.root.join("bin")}/rails app:template " +
@@ -24,9 +26,9 @@ namespace :ruby2js do
24
26
  end
25
27
 
26
28
  namespace :stimulus do
27
- desc "Install Ruby2JS with Stimulus Sprockets support"
28
- task :sprockets => :"stimulus:install:asset_pipeline" do
29
- template 'install/stimulus-sprockets.rb'
29
+ desc "Install Ruby2JS with Stimulus Rollup support"
30
+ task :rollup do
31
+ template 'install/stimulus-rollup.rb'
30
32
  end
31
33
 
32
34
  desc "Install Ruby2JS with Stimulus Webpacker support"
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2js
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.3
4
+ version: 4.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  - Jared White
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-04-12 00:00:00.000000000 Z
12
+ date: 2021-09-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parser
@@ -164,6 +164,7 @@ files:
164
164
  - lib/tasks/install/litelement.rb
165
165
  - lib/tasks/install/preact.rb
166
166
  - lib/tasks/install/react.rb
167
+ - lib/tasks/install/stimulus-rollup.rb
167
168
  - lib/tasks/install/stimulus-sprockets.rb
168
169
  - lib/tasks/install/stimulus-webpacker.rb
169
170
  - lib/tasks/install/webpacker.rb
@@ -174,7 +175,7 @@ homepage: http://github.com/rubys/ruby2js
174
175
  licenses:
175
176
  - MIT
176
177
  metadata: {}
177
- post_install_message:
178
+ post_install_message:
178
179
  rdoc_options: []
179
180
  require_paths:
180
181
  - lib
@@ -189,8 +190,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
190
  - !ruby/object:Gem::Version
190
191
  version: '0'
191
192
  requirements: []
192
- rubygems_version: 3.2.15
193
- signing_key:
193
+ rubygems_version: 3.1.2
194
+ signing_key:
194
195
  specification_version: 4
195
196
  summary: Minimal yet extensible Ruby to JavaScript conversion.
196
197
  test_files: []