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/filter/rubyjs.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/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/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/minitest-jasmine.rb

Overview

})

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

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
# File 'lib/ruby2js.rb', line 160

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

  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 = Parser::Source::Comment.associate(ast, comments) if ast
  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)
  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
  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
end

.eslevel_defaultObject



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

def self.eslevel_default
  @@eslevel_default
end

.eslevel_default=(level) ⇒ Object



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

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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/ruby2js.rb', line 240

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

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



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ruby2js.rb', line 224

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



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

def self.strict_default
  @@strict_default
end

.strict_default=(level) ⇒ Object



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

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