Module: Ruby2JS

Defined in:
lib/ruby2js/jsx.rb,
lib/ruby2js.rb,
lib/ruby2js/demo.rb,
lib/ruby2js/execjs.rb,
lib/ruby2js/filter.rb,
lib/ruby2js/version.rb,
lib/ruby2js/converter.rb,
lib/ruby2js/namespace.rb,
lib/ruby2js/sprockets.rb,
lib/ruby2js/filter/cjs.rb,
lib/ruby2js/filter/esm.rb,
lib/ruby2js/filter/jsx.rb,
lib/ruby2js/filter/lit.rb,
lib/ruby2js/filter/vue.rb,
lib/ruby2js/serializer.rb,
lib/ruby2js/filter/node.rb,
lib/ruby2js/converter/if.rb,
lib/ruby2js/converter/in.rb,
lib/ruby2js/filter/react.rb,
lib/ruby2js/converter/arg.rb,
lib/ruby2js/converter/def.rb,
lib/ruby2js/converter/for.rb,
lib/ruby2js/converter/nil.rb,
lib/ruby2js/converter/sym.rb,
lib/ruby2js/converter/var.rb,
lib/ruby2js/filter/jquery.rb,
lib/ruby2js/filter/return.rb,
lib/ruby2js/converter/args.rb,
lib/ruby2js/converter/case.rb,
lib/ruby2js/converter/cvar.rb,
lib/ruby2js/converter/defs.rb,
lib/ruby2js/converter/dstr.rb,
lib/ruby2js/converter/hash.rb,
lib/ruby2js/converter/hide.rb,
lib/ruby2js/converter/ivar.rb,
lib/ruby2js/converter/next.rb,
lib/ruby2js/converter/redo.rb,
lib/ruby2js/converter/self.rb,
lib/ruby2js/converter/send.rb,
lib/ruby2js/converter/xstr.rb,
lib/ruby2js/filter/require.rb,
lib/ruby2js/converter/array.rb,
lib/ruby2js/converter/begin.rb,
lib/ruby2js/converter/block.rb,
lib/ruby2js/converter/break.rb,
lib/ruby2js/converter/casgn.rb,
lib/ruby2js/converter/class.rb,
lib/ruby2js/converter/const.rb,
lib/ruby2js/converter/masgn.rb,
lib/ruby2js/converter/match.rb,
lib/ruby2js/converter/super.rb,
lib/ruby2js/converter/undef.rb,
lib/ruby2js/converter/until.rb,
lib/ruby2js/converter/vasgn.rb,
lib/ruby2js/converter/while.rb,
lib/ruby2js/converter/xnode.rb,
lib/ruby2js/converter/yield.rb,
lib/ruby2js/filter/matchAll.rb,
lib/ruby2js/filter/nokogiri.rb,
lib/ruby2js/filter/stimulus.rb,
lib/ruby2js/converter/assign.rb,
lib/ruby2js/converter/class2.rb,
lib/ruby2js/converter/cvasgn.rb,
lib/ruby2js/converter/import.rb,
lib/ruby2js/converter/ivasgn.rb,
lib/ruby2js/converter/module.rb,
lib/ruby2js/converter/nthref.rb,
lib/ruby2js/converter/opasgn.rb,
lib/ruby2js/converter/regexp.rb,
lib/ruby2js/converter/return.rb,
lib/ruby2js/converter/taglit.rb,
lib/ruby2js/filter/camelCase.rb,
lib/ruby2js/filter/functions.rb,
lib/ruby2js/configuration_dsl.rb,
lib/ruby2js/converter/boolean.rb,
lib/ruby2js/converter/defined.rb,
lib/ruby2js/converter/kwbegin.rb,
lib/ruby2js/converter/literal.rb,
lib/ruby2js/converter/logical.rb,
lib/ruby2js/filter/underscore.rb,
lib/ruby2js/converter/fileline.rb,
lib/ruby2js/converter/blockpass.rb,
lib/ruby2js/converter/prototype.rb,
lib/ruby2js/converter/untilpost.rb,
lib/ruby2js/converter/whilepost.rb,
lib/ruby2js/filter/securerandom.rb,
lib/ruby2js/filter/active_functions.rb,
lib/ruby2js/filter/minitest-jasmine.rb,
lib/ruby2js/filter/tagged_templates.rb

Overview

Experimental secure random support

Defined Under Namespace

Modules: Demo, Filter, SprocketsTransformer, VERSION Classes: ConfigurationDSL, Converter, Error, JsxParser, Line, Namespace, OpalEnumerator, Serializer, SyntaxError, Token

Constant Summary collapse

@@eslevel_default =

ecmascript 5

2009
@@eslevel_preset_default =
2021
@@strict_default =
false
@@module_default =
nil

Class Method Summary collapse

Class Method Details

.compile(source, options = {}) ⇒ Object



7
8
9
# File 'lib/ruby2js/execjs.rb', line 7

def self.compile(source, options={})
  ExecJS.compile(convert(source, options).to_s)
end

.convert(source, options = {}) ⇒ Object

TODO: this method has gotten long and unwieldy!



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/ruby2js.rb', line 219

def self.convert(source, options={})
  Filter.autoregister unless RUBY_ENGINE == 'opal'
  options = options.dup

  if Proc === source
    file,line = source.source_location
    source = IO.read(file)
    ast, comments = parse(source)
    comments = Parser::Source::Comment.associate(ast, comments) if ast
    ast = find_block( ast, line )
    options[:file] ||= file
  elsif Parser::AST::Node === source
    ast, comments = source, {}
    source = ast.loc.expression.source_buffer.source
  else
    ast, comments = parse( source, options[:file] )
    comments = ast ? Parser::Source::Comment.associate(ast, comments) : {}
  end

  # check if magic comment is present
  first_comment = comments.values.first&.map(&:text)&.first
  if first_comment
    if first_comment.include?(" ruby2js: preset")
      options[:preset] = true
      if first_comment.include?("filters: ")
        options[:filters] = first_comment.match(%r(filters:\s*?([^\s]+)\s?.*$))[1].split(",").map(&:to_sym)
      end
      if first_comment.include?("eslevel: ")
        options[:eslevel] = first_comment.match(%r(eslevel:\s*?([^\s]+)\s?.*$))[1].to_i
      end
      if first_comment.include?("disable_filters: ")
        options[:disable_filters] = first_comment.match(%r(disable_filters:\s*?([^\s]+)\s?.*$))[1].split(",").map(&:to_sym)
      end
    end
    disable_autoimports = first_comment.include?(" autoimports: false")
    disable_autoexports = first_comment.include?(" autoexports: false")
  end

  unless RUBY_ENGINE == 'opal'
    unless options.key?(:config_file) || !File.exist?("config/ruby2js.rb")
      options[:config_file] ||= "config/ruby2js.rb"
    end

    if options[:config_file]
      options = ConfigurationDSL.load_from_file(options[:config_file], options).to_h
    end
  end

  if options[:preset]
    options[:eslevel] ||= @@eslevel_preset_default
    options[:filters] = Filter::PRESET_FILTERS + Array(options[:filters]).uniq
    if options[:disable_filters]
      options[:filters] -= options[:disable_filters]
    end
    options[:comparison] ||= :identity
    options[:underscored_private] = true unless options[:underscored_private] == false
  end
  options[:eslevel] ||= @@eslevel_default
  options[:strict] = @@strict_default if options[:strict] == nil
  options[:module] ||= @@module_default || :esm

  namespace = Namespace.new

  filters = Filter.require_filters(options[:filters] || Filter::DEFAULTS)

  unless filters.empty?
    filter_options = options.merge({ filters: filters })
    filters.dup.each do |filter|
      filters = filter.reorder(filters) if filter.respond_to? :reorder
    end

    filter = Filter::Processor
    filters.reverse.each do |mod|
      filter = Class.new(filter) {include mod} 
    end
    filter = filter.new(comments)

    filter.disable_autoimports = disable_autoimports
    filter.disable_autoexports = disable_autoexports
    filter.options = filter_options
    filter.namespace = namespace
    ast = filter.process(ast)

    unless filter.prepend_list.empty?
      prepend = filter.prepend_list.sort_by {|ast| ast.type == :import ? 0 : 1}
      prepend.reject! {|ast| ast.type == :import} if filter.disable_autoimports
      ast = Parser::AST::Node.new(:begin, [*prepend, ast])
    end
  end

  ruby2js = Ruby2JS::Converter.new(ast, comments)

  ruby2js.binding = options[:binding]
  ruby2js.ivars = options[:ivars]
  ruby2js.eslevel = options[:eslevel]
  ruby2js.strict = options[:strict]
  ruby2js.comparison = options[:comparison] || :equality
  ruby2js.or = options[:or] || :logical
  ruby2js.module_type = options[:module] || :esm
  ruby2js.underscored_private = (options[:eslevel] < 2022) || options[:underscored_private]

  ruby2js.namespace = namespace

  if ruby2js.binding and not ruby2js.ivars
    ruby2js.ivars = ruby2js.binding.eval \
      'Hash[instance_variables.map {|var| [var, instance_variable_get(var)]}]'
  elsif options[:scope] and not ruby2js.ivars
    scope = options.delete(:scope)
    ruby2js.ivars = Hash[scope.instance_variables.map {|var|
      [var, scope.instance_variable_get(var)]}]
  end

  ruby2js.width = options[:width] if options[:width]

  ruby2js.enable_vertical_whitespace if source.include? "\n"

  ruby2js.convert

  ruby2js.timestamp options[:file]

  ruby2js.file_name = options[:file] || ast&.loc&.expression&.source_buffer&.name || ''

  ruby2js
end

.eslevel_defaultObject



30
31
32
# File 'lib/ruby2js.rb', line 30

def self.eslevel_default
  @@eslevel_default
end

.eslevel_default=(level) ⇒ Object



34
35
36
# File 'lib/ruby2js.rb', line 34

def self.eslevel_default=(level)
  @@eslevel_default = level
end

.eval(source, options = {}) ⇒ Object



11
12
13
# File 'lib/ruby2js/execjs.rb', line 11

def self.eval(source, options={})
  ExecJS.eval(convert(source, options.merge(strict: false)).to_s)
end

.exec(source, options = {}) ⇒ Object



15
16
17
# File 'lib/ruby2js/execjs.rb', line 15

def self.exec(source, options={})
  ExecJS.exec(convert(source, options).to_s)
end

.find_block(ast, line) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/ruby2js.rb', line 360

def self.find_block(ast, line)
  if ast.type == :block and ast.loc.expression.line == line
    return ast.children.last
  end

  ast.children.each do |child|
    if Parser::AST::Node === child
      block = find_block child, line
      return block if block
    end
  end

  nil
end

.jsx2_rb(string) ⇒ Object



10
11
12
# File 'lib/ruby2js/jsx.rb', line 10

def self.jsx2_rb(string)
  JsxParser.new(string.chars.each).parse.join("\n")
end

.module_defaultObject



46
47
48
# File 'lib/ruby2js.rb', line 46

def self.module_default
  @@module_default
end

.module_default=(module_type) ⇒ Object



50
51
52
# File 'lib/ruby2js.rb', line 50

def self.module_default=(module_type)
  @@module_default = module_type
end

.parse(source, file = nil, line = 1) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/ruby2js.rb', line 344

def self.parse(source, file=nil, line=1)
  buffer = Parser::Source::Buffer.new(file, line)
  buffer.source = source.encode('UTF-8')
  parser = Parser::CurrentRuby.new
  parser.diagnostics.all_errors_are_fatal = true
  parser.diagnostics.consumer = lambda {|diagnostic| nil}
  parser.builder.emit_file_line_as_literals = false
  parser.parse_with_comments(buffer)
rescue Parser::SyntaxError => e
  split = source[0..e.diagnostic.location.begin_pos].split("\n")
  line, col = split.length, split.last.length
  message = "line #{line}, column #{col}: #{e.diagnostic.message}"
  message += "\n in file #{file}" if file
  raise Ruby2JS::SyntaxError.new(message, e.diagnostic)
end

.strict_defaultObject



38
39
40
# File 'lib/ruby2js.rb', line 38

def self.strict_default
  @@strict_default
end

.strict_default=(level) ⇒ Object



42
43
44
# File 'lib/ruby2js.rb', line 42

def self.strict_default=(level)
  @@strict_default = level
end