ruby2js 5.0.0 → 5.1.0

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: 40cd0d8969ad8042e83eaad5d0c870a05e949907a8d8429ca295f12ccdce5b45
4
- data.tar.gz: f03789a32881ceca5fb7310e9e88c96882a76a9782c8940bdfe01bf00354fcef
3
+ metadata.gz: 709b38712553ae1a4f14aad4f1a994ba45e54f81ebb1d0a11ec5ae54b55eedcf
4
+ data.tar.gz: eba029d424058e50057a226fb65b81d9eebc8956d645947e4dccf212e2b7a5b3
5
5
  SHA512:
6
- metadata.gz: 719e4a55b5eb62ffd31e53eeeebfb80140d0692d3cf7675a33eb10188d1845267b4a35fe3590ed589af49c9f576430c261e568184a8a13675036510fdc01493a
7
- data.tar.gz: d4a0962d3534066225b8500b20af25124784de5ac7d5b62d25d363b8d781d09d4a359ef2fbd945ccd2aa7b48f34b8d3564e52b18bb15c09df7e0da0b01115e42
6
+ metadata.gz: 246f1a73f71cfac86e0021a509ef5342ccba199a695e9716b61247ccd2cd54e3d5579dda27289dbd01a2bfc2f04a52060357238ce1ccf3cbf64eda5a5174b599
7
+ data.tar.gz: ce88cde76f203294a84eaefcf0d61136e30c981fc25682516ed617d2ea884610d21db6b51c7a1fd2fc3552413b4a3222491c8abef6b1dea76f82a53a8ec61646
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,12 @@ 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
+ opts.on('-C', '--config [FILE]', "configuration file to use (default is config/ruby2js.rb)") {|filename|
61
+ options[:config_file] = filename
62
+ }
63
+
62
64
  opts.on('--autoexports [default]', "add export statements for top level constants") {|option|
63
65
  options[:autoexports] = option ? option.to_sym : true
64
66
  }
@@ -92,6 +94,10 @@ def parse_request(env=ENV)
92
94
  selected.push(*names)
93
95
  end
94
96
 
97
+ opts.on('--filepath [PATH]', "supply a path if stdin is related to a source file") do |filepath|
98
+ options[:file] = filepath
99
+ end
100
+
95
101
  opts.on('--identity', "triple equal comparison operators") {options[:comparison] = :identity}
96
102
 
97
103
  opts.on('--import_from_skypack', "use Skypack for internal functions import statements") do
@@ -132,6 +138,10 @@ def parse_request(env=ENV)
132
138
  options[:underscored_private] = true
133
139
  end
134
140
 
141
+ opts.on("--sourcemap", "Provide a JSON object with the code and sourcemap") do
142
+ @provide_sourcemap = true
143
+ end
144
+
135
145
  # shameless hack. Instead of repeating the available options, extract them
136
146
  # from the OptionParser. Exclude default options and es20xx options.
137
147
  options_available = opts.instance_variable_get(:@stack).last.list.
@@ -159,30 +169,7 @@ def parse_request(env=ENV)
159
169
  require 'wunderbar' unless wunderbar_options.empty?
160
170
 
161
171
  # 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
172
+ options[:filters] = Ruby2JS::Filter.require_filters(selected)
186
173
 
187
174
  return options, selected, options_available
188
175
  end
@@ -193,9 +180,29 @@ if (not defined? Wunderbar or not env['SERVER_PORT']) and not @live
193
180
  # command line support
194
181
  if ARGV.length > 0
195
182
  options[:file] = ARGV.first
196
- puts Ruby2JS.convert(File.read(ARGV.first), options).to_s
183
+ conv = Ruby2JS.convert(File.read(ARGV.first), options)
184
+ if @provide_sourcemap
185
+ puts(
186
+ {
187
+ code: conv.to_s,
188
+ sourcemap: conv.sourcemap,
189
+ }.to_json
190
+ )
191
+ else
192
+ puts conv.to_s
193
+ end
197
194
  else
198
- puts Ruby2JS.convert($stdin.read, options).to_s
195
+ conv = Ruby2JS.convert($stdin.read, options)
196
+ if @provide_sourcemap
197
+ puts(
198
+ {
199
+ code: conv.to_s,
200
+ sourcemap: conv.sourcemap,
201
+ }.to_json
202
+ )
203
+ else
204
+ puts conv.to_s
205
+ end
199
206
  end
200
207
 
201
208
  else
@@ -332,11 +339,11 @@ else
332
339
  def _sl_menu_item(name, args)
333
340
  if args.include? :checked
334
341
  _div do
335
- _input type: 'checkbox', **args
342
+ _input type: 'checkbox', **args.reject { |k| k == :type }
336
343
  _span name
337
344
  end
338
345
  else
339
- _option name, args
346
+ _option name, args.reject { |k| k == :type }
340
347
  end
341
348
  end
342
349
 
@@ -348,7 +355,7 @@ else
348
355
 
349
356
  _form method: 'post' do
350
357
  _div data_controller: @live && 'ruby' do
351
- _textarea.ruby.form_control @ruby, name: 'ruby', rows: 8,
358
+ _textarea.ruby.form_control @ruby || 'puts "Hello world!"', name: 'ruby', rows: 8,
352
359
  placeholder: 'Ruby source'
353
360
  end
354
361
 
@@ -356,15 +363,17 @@ else
356
363
  _input.btn.btn_primary type: 'submit', value: 'Convert',
357
364
  style: "display: #{@live ? 'none' : 'inline'}"
358
365
 
366
+ _sl_checkbox 'Use Preset', id: 'preset', name: 'preset', checked: options[:preset] ? !!options[:preset] : true
367
+
359
368
  _label 'ESLevel:', for: 'eslevel'
360
369
  if @live
361
370
  _sl_dropdown.eslevel! name: 'eslevel' do
362
371
  _sl_button @eslevel || 'default', slot: 'trigger', caret: true
363
372
  _sl_menu do
364
- _sl_menu_item 'default', checked: !@eslevel || @eslevel == 'default'
373
+ _sl_menu_item 'default', type: "checkbox", checked: !@eslevel || @eslevel == 'default'
365
374
  Dir["#{$:.first}/ruby2js/es20*.rb"].sort.each do |file|
366
375
  eslevel = File.basename(file, '.rb').sub('es', '')
367
- _sl_menu_item eslevel, value: eslevel, checked: @eslevel == eslevel
376
+ _sl_menu_item eslevel, type: "checkbox", value: eslevel, checked: @eslevel == eslevel
368
377
  end
369
378
  end
370
379
  end
@@ -386,7 +395,7 @@ else
386
395
  Dir["#{$:.first}/ruby2js/filter/*.rb"].sort.each do |file|
387
396
  filter = File.basename(file, '.rb')
388
397
  next if filter == 'require'
389
- _sl_menu_item filter, name: filter,
398
+ _sl_menu_item filter, type: "checkbox", name: filter,
390
399
  checked: selected.include?(filter)
391
400
  end
392
401
  end
@@ -400,9 +409,10 @@ else
400
409
  checked[:nullish] = options[:or] == :nullish
401
410
 
402
411
  options_available.each do |option, args|
412
+ next if option == 'preset'
403
413
  next if option == 'filter'
404
414
  next if option.start_with? 'require_'
405
- _sl_menu_item option, name: option,
415
+ _sl_menu_item option, type: "checkbox", name: option,
406
416
  checked: checked[option.to_sym],
407
417
  data_args: options_available[option]
408
418
  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
@@ -125,7 +125,7 @@ module Ruby2JS
125
125
  *process_all(pair.children))
126
126
  end
127
127
  elsif arg.type == :str
128
- init << s(:send, s(:gvar, :$_), :content=, process(arg))
128
+ init << s(:send, s(:gvar, :$_), :textContent=, process(arg))
129
129
  else
130
130
  return super
131
131
  end
@@ -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 = 0
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.0
4
+ version: 5.1.0
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: 2023-02-20 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