ruby2js 5.0.1 → 5.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 856cba477f40164ac46c0289d53f7a650125a39ef83f8fd00404c61fd35e81f1
4
- data.tar.gz: 4a8799549c286b30ea340fd0181e615d1b7c1c725f7450310b02888692210083
3
+ metadata.gz: b6436ac11f25e564ac1a5c8efdc17cd8c2f20e6d024899abdaa9badcb35c7c2b
4
+ data.tar.gz: c9f18781880390e3fd6db9a699a9de8c92c0ca34d853d416ff0a1daf3fa5b64f
5
5
  SHA512:
6
- metadata.gz: 839bbb243e185cdf61b4c7a67e85595dcad52acdaadb4f053f08e1c41f60dfcc8bc9c3320670538976413299bf199c77ffcd5d4b70df232600673e4b8bc580c5
7
- data.tar.gz: cbac1f4f24ae5292c65555a15185204e7128e22a54a257ef05132f9b7d4954a49b65db8dae6d8089b6b5e1a5682625ba6fd5778b0c68ba5748abf3c03c330901
6
+ metadata.gz: c6531b7bdae9313e665ed2801e78db70aa8fc22fd667aaed1d2ec9ce17824f2a812efa123c0341e518a40303c264c929d084f2160e98f9c6132a47684a3cf4c7
7
+ data.tar.gz: f0a805108a89a46132625ab5541a490e8e08c1fdd62483009a415aa2bff71d11c4427f35d8f9bc6b6e24f4512ab8bc50108ba18d3abb3370e76216f2c51bbcca
data/README.md CHANGED
@@ -4,15 +4,13 @@ Ruby2JS
4
4
  Minimal yet extensible Ruby to JavaScript conversion.
5
5
 
6
6
  [![Gem Version](https://badge.fury.io/rb/ruby2js.svg)](https://badge.fury.io/rb/ruby2js)
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
-
9
7
 
10
8
  ## Documentation
11
9
  ---
12
10
 
13
11
  * Visit **[ruby2js.com](https://www.ruby2js.com)** for detailed setup instructions and API reference.
14
12
 
15
- * [Try Ruby2JS online](https://ruby2js.com/demo)
13
+ * [Try Ruby2JS online](https://ruby2js.com/demo?preset=true)
16
14
 
17
15
 
18
16
  ## Synopsis
@@ -22,26 +20,7 @@ Basic:
22
20
 
23
21
  ```ruby
24
22
  require 'ruby2js'
25
- puts Ruby2JS.convert("a={age:3}\na.age+=1")
26
- ```
27
-
28
- With filter:
29
-
30
- ```ruby
31
- require 'ruby2js/filter/functions'
32
- puts Ruby2JS.convert('"2A".to_i(16)')
33
- ```
34
-
35
- Host variable substitution:
36
-
37
- ```ruby
38
- puts Ruby2JS.convert("@name", ivars: {:@name => "Joe"})
39
- ```
40
-
41
- Enable ES2015 support:
42
-
43
- ```ruby
44
- puts Ruby2JS.convert('"#{a}"', eslevel: 2015)
23
+ puts Ruby2JS.convert("a={age:3}\na.age+=1", preset: true)
45
24
  ```
46
25
 
47
26
  ## Testing
data/demo/ruby2js.rb CHANGED
@@ -23,15 +23,11 @@ $:.unshift File.absolute_path('../../lib', __FILE__)
23
23
  require 'ruby2js/demo'
24
24
  require 'cgi'
25
25
  require 'pathname'
26
+ require 'json'
26
27
 
27
28
  def parse_request(env=ENV)
28
-
29
29
  # autoregister filters
30
- filters = {}
31
- Dir["#{$:.first}/ruby2js/filter/*.rb"].sort.each do |file|
32
- filter = File.basename(file, '.rb')
33
- filters[filter] = file
34
- end
30
+ filters = Ruby2JS::Filter.autoregister($:.first)
35
31
 
36
32
  # web/CGI query string support
37
33
  selected = env['PATH_INFO'].to_s.split('/')
@@ -59,6 +55,14 @@ def parse_request(env=ENV)
59
55
  opts = OptionParser.new
60
56
  opts.banner = "Usage: #$0 [options] [file]"
61
57
 
58
+ opts.on('--preset', "use sane defaults (modern eslevel & common filters)") {options[:preset] = true}
59
+
60
+ unless env['QUERY_STRING']
61
+ opts.on('-C', '--config [FILE]', "configuration file to use (default is config/ruby2js.rb)") {|filename|
62
+ options[:config_file] = filename
63
+ }
64
+ end
65
+
62
66
  opts.on('--autoexports [default]', "add export statements for top level constants") {|option|
63
67
  options[:autoexports] = option ? option.to_sym : true
64
68
  }
@@ -92,6 +96,10 @@ def parse_request(env=ENV)
92
96
  selected.push(*names)
93
97
  end
94
98
 
99
+ opts.on('--filepath [PATH]', "supply a path if stdin is related to a source file") do |filepath|
100
+ options[:file] = filepath
101
+ end
102
+
95
103
  opts.on('--identity', "triple equal comparison operators") {options[:comparison] = :identity}
96
104
 
97
105
  opts.on('--import_from_skypack', "use Skypack for internal functions import statements") do
@@ -132,6 +140,10 @@ def parse_request(env=ENV)
132
140
  options[:underscored_private] = true
133
141
  end
134
142
 
143
+ opts.on("--sourcemap", "Provide a JSON object with the code and sourcemap") do
144
+ @provide_sourcemap = true
145
+ end
146
+
135
147
  # shameless hack. Instead of repeating the available options, extract them
136
148
  # from the OptionParser. Exclude default options and es20xx options.
137
149
  options_available = opts.instance_variable_get(:@stack).last.list.
@@ -159,30 +171,7 @@ def parse_request(env=ENV)
159
171
  require 'wunderbar' unless wunderbar_options.empty?
160
172
 
161
173
  # load selected filters
162
- options[:filters] = []
163
-
164
- selected.each do |name|
165
- begin
166
- if filters.include? name
167
- require filters[name]
168
-
169
- # find the module and add it to the list of filters.
170
- # Note: explicit filter option is used instead of
171
- # relying on Ruby2JS::Filter::DEFAULTS as the demo
172
- # may be run as a server and as such DEFAULTS may
173
- # contain filters from previous requests.
174
- Ruby2JS::Filter::DEFAULTS.each do |mod|
175
- method = mod.instance_method(mod.instance_methods.first)
176
- if filters[name] == method.source_location.first
177
- options[:filters] << mod
178
- end
179
- end
180
- elsif not name.empty? and name =~ /^[-\w+]$/
181
- $load_error = "UNKNOWN filter: #{name}"
182
- end
183
- rescue Exception => $load_error
184
- end
185
- end
174
+ options[:filters] = Ruby2JS::Filter.require_filters(selected)
186
175
 
187
176
  return options, selected, options_available
188
177
  end
@@ -193,9 +182,29 @@ if (not defined? Wunderbar or not env['SERVER_PORT']) and not @live
193
182
  # command line support
194
183
  if ARGV.length > 0
195
184
  options[:file] = ARGV.first
196
- puts Ruby2JS.convert(File.read(ARGV.first), options).to_s
185
+ conv = Ruby2JS.convert(File.read(ARGV.first), options)
186
+ if @provide_sourcemap
187
+ puts(
188
+ {
189
+ code: conv.to_s,
190
+ sourcemap: conv.sourcemap,
191
+ }.to_json
192
+ )
193
+ else
194
+ puts conv.to_s
195
+ end
197
196
  else
198
- puts Ruby2JS.convert($stdin.read, options).to_s
197
+ conv = Ruby2JS.convert($stdin.read, options)
198
+ if @provide_sourcemap
199
+ puts(
200
+ {
201
+ code: conv.to_s,
202
+ sourcemap: conv.sourcemap,
203
+ }.to_json
204
+ )
205
+ else
206
+ puts conv.to_s
207
+ end
199
208
  end
200
209
 
201
210
  else
@@ -332,11 +341,11 @@ else
332
341
  def _sl_menu_item(name, args)
333
342
  if args.include? :checked
334
343
  _div do
335
- _input type: 'checkbox', **args
344
+ _input type: 'checkbox', **args.reject { |k| k == :type }
336
345
  _span name
337
346
  end
338
347
  else
339
- _option name, args
348
+ _option name, args.reject { |k| k == :type }
340
349
  end
341
350
  end
342
351
 
@@ -348,7 +357,7 @@ else
348
357
 
349
358
  _form method: 'post' do
350
359
  _div data_controller: @live && 'ruby' do
351
- _textarea.ruby.form_control @ruby, name: 'ruby', rows: 8,
360
+ _textarea.ruby.form_control @ruby || 'puts "Hello world!"', name: 'ruby', rows: 8,
352
361
  placeholder: 'Ruby source'
353
362
  end
354
363
 
@@ -356,15 +365,17 @@ else
356
365
  _input.btn.btn_primary type: 'submit', value: 'Convert',
357
366
  style: "display: #{@live ? 'none' : 'inline'}"
358
367
 
368
+ _sl_checkbox 'Use Preset', id: 'preset', name: 'preset', checked: options[:preset] ? !!options[:preset] : true
369
+
359
370
  _label 'ESLevel:', for: 'eslevel'
360
371
  if @live
361
372
  _sl_dropdown.eslevel! name: 'eslevel' do
362
373
  _sl_button @eslevel || 'default', slot: 'trigger', caret: true
363
374
  _sl_menu do
364
- _sl_menu_item 'default', checked: !@eslevel || @eslevel == 'default'
375
+ _sl_menu_item 'default', type: "checkbox", checked: !@eslevel || @eslevel == 'default'
365
376
  Dir["#{$:.first}/ruby2js/es20*.rb"].sort.each do |file|
366
377
  eslevel = File.basename(file, '.rb').sub('es', '')
367
- _sl_menu_item eslevel, value: eslevel, checked: @eslevel == eslevel
378
+ _sl_menu_item eslevel, type: "checkbox", value: eslevel, checked: @eslevel == eslevel
368
379
  end
369
380
  end
370
381
  end
@@ -386,7 +397,7 @@ else
386
397
  Dir["#{$:.first}/ruby2js/filter/*.rb"].sort.each do |file|
387
398
  filter = File.basename(file, '.rb')
388
399
  next if filter == 'require'
389
- _sl_menu_item filter, name: filter,
400
+ _sl_menu_item filter, type: "checkbox", name: filter,
390
401
  checked: selected.include?(filter)
391
402
  end
392
403
  end
@@ -400,9 +411,10 @@ else
400
411
  checked[:nullish] = options[:or] == :nullish
401
412
 
402
413
  options_available.each do |option, args|
414
+ next if option == 'preset'
403
415
  next if option == 'filter'
404
416
  next if option.start_with? 'require_'
405
- _sl_menu_item option, name: option,
417
+ _sl_menu_item option, type: "checkbox", name: option,
406
418
  checked: checked[option.to_sym],
407
419
  data_args: options_available[option]
408
420
  end
data/lib/ruby2js/cgi.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+ #
1
3
  # Example usage:
2
4
  #
3
5
  # hello.cgi:
@@ -0,0 +1,95 @@
1
+ module Ruby2JS
2
+ class ConfigurationDSL
3
+ def self.load_from_file(config_file, options = {})
4
+ new(options).tap { _1.instance_eval(File.read(config_file), config_file, 1) }
5
+ end
6
+
7
+ def initialize(options = {})
8
+ @options = options
9
+ end
10
+
11
+ def preset(bool = true)
12
+ @options[:preset] = bool
13
+ end
14
+
15
+ def filter(name)
16
+ @options[:filters] ||= []
17
+ @options[:filters] << name
18
+ end
19
+
20
+ def remove_filter(name)
21
+ @options[:disable_filters] ||= []
22
+ @options[:disable_filters] << name
23
+ end
24
+
25
+ def eslevel(level)
26
+ @options[:eslevel] = level
27
+ end
28
+
29
+ def equality_comparison
30
+ @options[:comparison] = :equality
31
+ end
32
+
33
+ def identity_comparison
34
+ @options[:comparison] = :identity
35
+ end
36
+
37
+ def esm_modules
38
+ @options[:module] = :esm
39
+ end
40
+
41
+ def cjs_modules
42
+ @options[:module] = :cjs
43
+ end
44
+
45
+ def underscored_ivars
46
+ @options[:underscored_private] = true
47
+ end
48
+
49
+ # Only applies for ES2022+
50
+ def private_field_ivars
51
+ @options[:underscored_private] = false
52
+ end
53
+
54
+ def logical_or
55
+ @options[:or] = :logical
56
+ end
57
+
58
+ def nullish_or
59
+ @options[:or] = :nullish
60
+ end
61
+
62
+ def autoimport(identifier = nil, file = nil, &block)
63
+ if block
64
+ @options[:autoimports] = block
65
+ return
66
+ elsif @options[:autoimports].is_a?(Proc)
67
+ @options[:autoimports] = {}
68
+ end
69
+
70
+ @options[:autoimports] ||= {}
71
+ @options[:autoimports][identifier] = file
72
+ end
73
+
74
+ def autoimport_defs(value)
75
+ @options[:defs] = value
76
+ end
77
+
78
+ def autoexports(value)
79
+ @options[:autoexports] = value
80
+ end
81
+
82
+ def include_method(method_name)
83
+ @options[:include] ||= []
84
+ @options[:include] << method_name unless @options[:include].include?(method_name)
85
+ end
86
+
87
+ def template_literal_tags(tags)
88
+ @options[:template_literal_tags] = tags
89
+ end
90
+
91
+ def to_h
92
+ @options
93
+ end
94
+ end
95
+ end
@@ -15,7 +15,7 @@ module Ruby2JS
15
15
  EXPRESSIONS = [ :array, :float, :hash, :int, :lvar, :nil, :send, :attr,
16
16
  :str, :sym, :dstr, :dsym, :cvar, :ivar, :zsuper, :super, :or, :and,
17
17
  :block, :const, :true, :false, :xnode, :taglit, :self,
18
- :op_asgn, :and_asgn, :or_asgn, :taglit, :gvar, :csend ]
18
+ :op_asgn, :and_asgn, :or_asgn, :taglit, :gvar, :csend, :call ]
19
19
 
20
20
  handle :autoreturn do |*statements|
21
21
  return if statements == [nil]
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  require 'ruby2js'
2
4
  require 'execjs'
3
5
 
@@ -132,6 +132,10 @@ module Ruby2JS
132
132
  handle_generic_node(super, :sym)
133
133
  end
134
134
 
135
+ def on_assign(node)
136
+ S(:assign , node.children[0], *node.children[1..-1].map{ process _1 })
137
+ end
138
+
135
139
  def on_defs(node)
136
140
  node = super
137
141
  return node if node.type != :defs
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  require 'ruby2js'
2
4
  #
3
5
  # Jquery functions are either invoked using jQuery() or, more commonly, $().
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  require 'ruby2js'
2
4
 
3
5
  # Convert Wunderbar syntax to JSX
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  require 'ruby2js'
2
4
 
3
5
  module Ruby2JS
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  require 'ruby2js'
2
4
 
3
5
  module Ruby2JS
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  # A filter to support usage of React. Overview of translations provided:
2
4
  # * classes that inherit from React are converted to React.createClass
3
5
  # calls.
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  require 'ruby2js'
2
4
  require 'pathname'
3
5
 
@@ -91,6 +93,18 @@ module Ruby2JS
91
93
  target << child.children[1]
92
94
  elsif child.type == :def
93
95
  target << child.children[0]
96
+ elsif child.type == :send && child.children[1] == :async
97
+ target << child.children[2].children[0]
98
+ elsif child.type == :const
99
+ target << child.children[1]
100
+ elsif child.type == :array
101
+ child.children.each do |export_statement|
102
+ if export_statement.type == :const
103
+ target << export_statement.children[1]
104
+ elsif export_statement.type == :hash
105
+ default_exports << export_statement.children[0].children[1].children[1]
106
+ end
107
+ end
94
108
  end
95
109
  end
96
110
 
@@ -99,6 +113,12 @@ module Ruby2JS
99
113
  else
100
114
  named_exports += auto_exports
101
115
  end
116
+ default_exports.map! { _1.to_s.sub(/[?!]/, '').then do |name|
117
+ respond_to?(:camelCase) ? camelCase(name) : name.to_sym
118
+ end }
119
+ named_exports.map! { _1.to_s.sub(/[?!]/, '').then do |name|
120
+ respond_to?(:camelCase) ? camelCase(name) : name.to_sym
121
+ end }
102
122
 
103
123
  imports = @require_seen[realpath]
104
124
  imports << s(:const, nil, default_exports.first) unless default_exports.empty?
@@ -1,3 +1,5 @@
1
+ # TODO: this feature is deprecated.
2
+
1
3
  require 'ruby2js'
2
4
 
3
5
  module Ruby2JS
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  require 'ruby2js'
2
4
 
3
5
  module Ruby2JS
@@ -5,6 +5,56 @@
5
5
 
6
6
  module Ruby2JS
7
7
  module Filter
8
+ PRESET_FILTERS = [:esm, :functions, :return]
9
+
10
+ def self.registered_filters
11
+ @@registered_filters ||= {}
12
+ end
13
+
14
+ def self.autoregister(lib_dir = File.expand_path("..", __dir__))
15
+ Dir["#{lib_dir}/ruby2js/filter/*.rb"].sort.each do |file|
16
+ filter = File.basename(file, '.rb')
17
+ registered_filters[filter] = file
18
+ end
19
+
20
+ registered_filters
21
+ end
22
+
23
+ # TODO: better document this code path
24
+ def self.require_filters(filters)
25
+ mods = []
26
+ filters.each do |name|
27
+ if name.is_a?(Module)
28
+ mods << name
29
+ next
30
+ end
31
+
32
+ name = name.to_s
33
+
34
+ if registered_filters[name].is_a?(Module)
35
+ mods << registered_filters[name]
36
+ next
37
+ end
38
+
39
+ begin
40
+ if registered_filters.include? name
41
+ require registered_filters[name]
42
+
43
+ Ruby2JS::Filter::DEFAULTS.each do |mod|
44
+ method = mod.instance_method(mod.instance_methods.first)
45
+ if registered_filters[name] == method.source_location.first
46
+ mods << mod
47
+ end
48
+ end
49
+ elsif not name.empty? and name =~ /^[-\w+]$/
50
+ $load_error = "UNKNOWN filter: #{name}"
51
+ end
52
+ rescue Exception => $load_error
53
+ end
54
+ end
55
+
56
+ mods
57
+ end
8
58
 
9
59
  #
10
60
  # module level defaults
data/lib/ruby2js/haml.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+ #
1
3
  # Example usage:
2
4
  #
3
5
  # Add:
@@ -15,13 +17,42 @@
15
17
  # (Note missing brackets: ruby syntax, js sematics)
16
18
  #
17
19
  require "haml"
20
+ require "haml/filters"
21
+ require "haml/filters/base"
22
+
23
+ module Haml
24
+ class Filters
25
+ class Ruby2JS < Base
26
+ def compile(node)
27
+ temple = [:multi]
28
+ temple << [:static, "<script type='text/javascript'>\n"]
29
+ compile_ruby!( temple , node )
30
+ temple << [:static, "\n</script>"]
31
+ temple
32
+ end
33
+
34
+ #Copird from text base, added ruby2js convert
35
+ def compile_ruby!(temple, node)
36
+ text = node.value[:text]
37
+ if ::Haml::Util.contains_interpolation?(node.value[:text])
38
+ # original: Haml::Filters#compile
39
+ text = ::Haml::Util.unescape_interpolation(text).gsub(/(\\+)n/) do |s|
40
+ escapes = $1.size
41
+ next s if escapes % 2 == 0
42
+ "#{'\\' * (escapes - 1)}\n"
43
+ end
44
+ text.prepend("\n")
45
+
46
+ temple << [:dynamic, "::Ruby2JS.convert(#{text} ).to_s"]
47
+ else
48
+ temple << [:static, ::Ruby2JS.convert(text).to_s]
49
+ end
50
+ end
51
+
18
52
 
19
- module Ruby2JS
20
- module Haml::Ruby2JS
21
- include Haml::Filters::Base
22
- def render(text)
23
- converted = Ruby2JS.convert(text).to_s
24
- "<script type='text/javascript'>\n#{converted}\n</script>"
25
53
  end
26
54
  end
27
55
  end
56
+
57
+
58
+ Haml::Filters.registered[:ruby2js] ||= Haml::Filters::Ruby2JS
data/lib/ruby2js/jsx.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+ #
1
3
  # convert a JSX expression into wunderbar statements
2
4
  #
3
5
  # Once the syntax is converted to pure Ruby statements,
data/lib/ruby2js/rails.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  require 'rails'
2
4
  require_relative './sprockets'
3
5
 
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+ #
1
3
  # Example usage:
2
4
  #
3
5
  # sinatra.rb:
@@ -1,4 +1,6 @@
1
1
  # frozen_string_literal: true
2
+ # TODO: This feature is deprecated.
3
+
2
4
  require 'sprockets/source_map_utils'
3
5
  require 'ruby2js'
4
6
  require 'ruby2js/version'
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  require 'ruby2js'
2
4
 
3
5
  Ruby2JS.strict_default = true
@@ -1,7 +1,7 @@
1
1
  module Ruby2JS
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 5
4
- MINOR = 0
4
+ MINOR = 1
5
5
  TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
data/lib/ruby2js.rb CHANGED
@@ -8,6 +8,7 @@ ensure
8
8
  $VERBOSE = old_verbose
9
9
  end
10
10
 
11
+ require 'ruby2js/configuration_dsl' unless RUBY_ENGINE == 'opal'
11
12
  require 'ruby2js/converter'
12
13
  require 'ruby2js/filter'
13
14
  require 'ruby2js/namespace'
@@ -22,6 +23,7 @@ module Ruby2JS
22
23
  end
23
24
 
24
25
  @@eslevel_default = 2009 # ecmascript 5
26
+ @@eslevel_preset_default = 2021
25
27
  @@strict_default = false
26
28
  @@module_default = nil
27
29
 
@@ -67,16 +69,11 @@ module Ruby2JS
67
69
  include Ruby2JS::Filter
68
70
  BINARY_OPERATORS = Converter::OPERATORS[2..-1].flatten
69
71
 
70
- attr_accessor :prepend_list, :disable_autoimports, :namespace
72
+ attr_accessor :prepend_list, :disable_autoimports, :disable_autoexports, :namespace
71
73
 
72
74
  def initialize(comments)
73
75
  @comments = comments
74
76
 
75
- # check if magic comment is present:
76
- first_comment = @comments.values.first&.map(&:text)&.first
77
- @disable_autoimports = first_comment&.include?(" autoimports: false")
78
- @disable_autoexports = first_comment&.include?(" autoexports: false")
79
-
80
77
  @ast = nil
81
78
  @exclude_methods = []
82
79
  @prepend_list = Set.new
@@ -217,11 +214,10 @@ module Ruby2JS
217
214
  end
218
215
  end
219
216
 
217
+ # TODO: this method has gotten long and unwieldy!
220
218
  def self.convert(source, options={})
219
+ Filter.autoregister unless RUBY_ENGINE == 'opal'
221
220
  options = options.dup
222
- options[:eslevel] ||= @@eslevel_default
223
- options[:strict] = @@strict_default if options[:strict] == nil
224
- options[:module] ||= @@module_default || :esm
225
221
 
226
222
  if Proc === source
227
223
  file,line = source.source_location
@@ -238,11 +234,54 @@ module Ruby2JS
238
234
  comments = ast ? Parser::Source::Comment.associate(ast, comments) : {}
239
235
  end
240
236
 
237
+ # check if magic comment is present
238
+ first_comment = comments.values.first&.map(&:text)&.first
239
+ if first_comment
240
+ if first_comment.include?(" ruby2js: preset")
241
+ options[:preset] = true
242
+ if first_comment.include?("filters: ")
243
+ options[:filters] = first_comment.match(%r(filters:\s*?([^\s]+)\s?.*$))[1].split(",").map(&:to_sym)
244
+ end
245
+ if first_comment.include?("eslevel: ")
246
+ options[:eslevel] = first_comment.match(%r(eslevel:\s*?([^\s]+)\s?.*$))[1].to_i
247
+ end
248
+ if first_comment.include?("disable_filters: ")
249
+ options[:disable_filters] = first_comment.match(%r(disable_filters:\s*?([^\s]+)\s?.*$))[1].split(",").map(&:to_sym)
250
+ end
251
+ end
252
+ disable_autoimports = first_comment.include?(" autoimports: false")
253
+ disable_autoexports = first_comment.include?(" autoexports: false")
254
+ end
255
+
256
+ unless RUBY_ENGINE == 'opal'
257
+ unless options.key?(:config_file) || !File.exist?("config/ruby2js.rb")
258
+ options[:config_file] ||= "config/ruby2js.rb"
259
+ end
260
+
261
+ if options[:config_file]
262
+ options = ConfigurationDSL.load_from_file(options[:config_file], options).to_h
263
+ end
264
+ end
265
+
266
+ if options[:preset]
267
+ options[:eslevel] ||= @@eslevel_preset_default
268
+ options[:filters] = Filter::PRESET_FILTERS + Array(options[:filters]).uniq
269
+ if options[:disable_filters]
270
+ options[:filters] -= options[:disable_filters]
271
+ end
272
+ options[:comparison] ||= :identity
273
+ options[:underscored_private] = true unless options[:underscored_private] == false
274
+ end
275
+ options[:eslevel] ||= @@eslevel_default
276
+ options[:strict] = @@strict_default if options[:strict] == nil
277
+ options[:module] ||= @@module_default || :esm
278
+
241
279
  namespace = Namespace.new
242
280
 
243
- filters = (options[:filters] || Filter::DEFAULTS)
281
+ filters = Filter.require_filters(options[:filters] || Filter::DEFAULTS)
244
282
 
245
283
  unless filters.empty?
284
+ filter_options = options.merge({ filters: filters })
246
285
  filters.dup.each do |filter|
247
286
  filters = filter.reorder(filters) if filter.respond_to? :reorder
248
287
  end
@@ -253,7 +292,9 @@ module Ruby2JS
253
292
  end
254
293
  filter = filter.new(comments)
255
294
 
256
- filter.options = options
295
+ filter.disable_autoimports = disable_autoimports
296
+ filter.disable_autoexports = disable_autoexports
297
+ filter.options = filter_options
257
298
  filter.namespace = namespace
258
299
  ast = filter.process(ast)
259
300
 
data/lib/tasks/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ # TODO: This feature is deprecated.
2
+
1
3
  This directory contains rails install tasks and a test program which will run
2
4
  the rails examples from the docs/src/_examples/rails directory.
3
5
 
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: 5.0.1
4
+ version: 5.1.1
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: 2022-05-14 00:00:00.000000000 Z
12
+ date: 2024-01-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parser
@@ -54,6 +54,7 @@ files:
54
54
  - demo/ruby2js.rb
55
55
  - lib/ruby2js.rb
56
56
  - lib/ruby2js/cgi.rb
57
+ - lib/ruby2js/configuration_dsl.rb
57
58
  - lib/ruby2js/converter.rb
58
59
  - lib/ruby2js/converter/arg.rb
59
60
  - lib/ruby2js/converter/args.rb