Module: Ruby2JS

Defined in:
lib/ruby2js/filter.rb,
lib/ruby2js.rb,
lib/ruby2js/haml.rb,
lib/ruby2js/rails.rb,
lib/ruby2js/execjs.rb,
lib/ruby2js/version.rb,
lib/ruby2js/converter.rb,
lib/ruby2js/filter/cjs.rb,
lib/ruby2js/filter/esm.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/ivar.rb,
lib/ruby2js/converter/next.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/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/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/filter/wunderbar.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: Filter, Rails, VERSION Classes: Converter, Error, Line, Serializer, SyntaxError, Token

Constant Summary collapse

@@eslevel_default =

ecmascript 5

2009
@@strict_default =
false
@@module_default =
nil

Class Method Summary collapse

Class Method Details

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



5
6
7
# File 'lib/ruby2js/execjs.rb', line 5

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

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



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
# File 'lib/ruby2js.rb', line 180

def self.convert(source, options={})
  options[:eslevel] ||= @@eslevel_default
  options[:strict] = @@strict_default if options[:strict] == nil
  options[:module] ||= @@module_default || :esm

  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

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

  unless filters.empty?
    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.options = options
    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] < 2020) || options[:underscored_private]
  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



22
23
24
# File 'lib/ruby2js.rb', line 22

def self.eslevel_default
  @@eslevel_default
end

.eslevel_default=(level) ⇒ Object



26
27
28
# File 'lib/ruby2js.rb', line 26

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

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



9
10
11
# File 'lib/ruby2js/execjs.rb', line 9

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

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



13
14
15
# File 'lib/ruby2js/execjs.rb', line 13

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

.find_block(ast, line) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/ruby2js.rb', line 271

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

.module_defaultObject



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

def self.module_default
  @@module_default
end

.module_default=(module_type) ⇒ Object



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

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

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



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/ruby2js.rb', line 255

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.builder.emit_file_line_as_literals=false
  ast, comments = parser.parse_with_comments(buffer)
  Parser::CurrentRuby.parse(source.encode('utf-8')) unless ast
  [ast, comments]
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)
end

.strict_defaultObject



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

def self.strict_default
  @@strict_default
end

.strict_default=(level) ⇒ Object



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

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