Module: Ruby2JS
- Defined in:
- lib/ruby2js/jsx.rb,
lib/ruby2js.rb,
lib/ruby2js/demo.rb,
lib/ruby2js/haml.rb,
lib/ruby2js/rails.rb,
lib/ruby2js/execjs.rb,
lib/ruby2js/filter.rb,
lib/ruby2js/version.rb,
lib/ruby2js/converter.rb,
lib/ruby2js/namespace.rb,
lib/ruby2js/filter/cjs.rb,
lib/ruby2js/filter/esm.rb,
lib/ruby2js/filter/jsx.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/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/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/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, Rails, VERSION
Classes: Converter, Error, JsxParser, Line, Namespace, Serializer, SyntaxError, Token
Constant Summary
collapse
- @@eslevel_default =
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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
# File 'lib/ruby2js.rb', line 196
def self.convert(source, options={})
options = options.dup
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, = parse(source)
= Parser::Source::Comment.associate(ast, ) if ast
ast = find_block( ast, line )
options[:file] ||= file
elsif Parser::AST::Node === source
ast, = source, {}
source = ast.loc.expression.source_buffer.source
else
ast, = parse( source, options[:file] )
= ast ? Parser::Source::Comment.associate(ast, ) : {}
end
namespace = Namespace.new
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()
filter.options = 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, )
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_default ⇒ Object
28
29
30
|
# File 'lib/ruby2js.rb', line 28
def self.eslevel_default
@@eslevel_default
end
|
.eslevel_default=(level) ⇒ Object
32
33
34
|
# File 'lib/ruby2js.rb', line 32
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/ruby2js.rb', line 294
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
8
9
10
|
# File 'lib/ruby2js/jsx.rb', line 8
def self.jsx2_rb(string)
JsxParser.new(string.chars.each).parse.join("\n")
end
|
.module_default ⇒ Object
44
45
46
|
# File 'lib/ruby2js.rb', line 44
def self.module_default
@@module_default
end
|
.module_default=(module_type) ⇒ Object
48
49
50
|
# File 'lib/ruby2js.rb', line 48
def self.module_default=(module_type)
@@module_default = module_type
end
|
.parse(source, file = nil, line = 1) ⇒ Object
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/ruby2js.rb', line 278
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.(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_default ⇒ Object
36
37
38
|
# File 'lib/ruby2js.rb', line 36
def self.strict_default
@@strict_default
end
|
.strict_default=(level) ⇒ Object
40
41
42
|
# File 'lib/ruby2js.rb', line 40
def self.strict_default=(level)
@@strict_default = level
end
|