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
- .compile(source, options = {}) ⇒ Object
-
.convert(source, options = {}) ⇒ Object
TODO: this method has gotten long and unwieldy!.
- .eslevel_default ⇒ Object
- .eslevel_default=(level) ⇒ Object
- .eval(source, options = {}) ⇒ Object
- .exec(source, options = {}) ⇒ Object
- .find_block(ast, line) ⇒ Object
- .jsx2_rb(string) ⇒ Object
- .module_default ⇒ Object
- .module_default=(module_type) ⇒ Object
- .parse(source, file = nil, line = 1) ⇒ Object
- .strict_default ⇒ Object
- .strict_default=(level) ⇒ Object
Class Method Details
.compile(source, options = {}) ⇒ Object
7 8 9 |
# File 'lib/ruby2js/execjs.rb', line 7 def self.compile(source, ={}) ExecJS.compile(convert(source, ).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, ={}) Filter.autoregister unless RUBY_ENGINE == 'opal' = .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 ) [:file] ||= file elsif Parser::AST::Node === source ast, comments = source, {} source = ast.loc.expression.source_buffer.source else ast, comments = parse( source, [: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") [:preset] = true if first_comment.include?("filters: ") [:filters] = first_comment.match(%r(filters:\s*?([^\s]+)\s?.*$))[1].split(",").map(&:to_sym) end if first_comment.include?("eslevel: ") [:eslevel] = first_comment.match(%r(eslevel:\s*?([^\s]+)\s?.*$))[1].to_i end if first_comment.include?("disable_filters: ") [: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 .key?(:config_file) || !File.exist?("config/ruby2js.rb") [:config_file] ||= "config/ruby2js.rb" end if [:config_file] = ConfigurationDSL.load_from_file([:config_file], ).to_h end end if [:preset] [:eslevel] ||= @@eslevel_preset_default [:filters] = Filter::PRESET_FILTERS + Array([:filters]).uniq if [:disable_filters] [:filters] -= [:disable_filters] end [:comparison] ||= :identity [:underscored_private] = true unless [:underscored_private] == false end [:eslevel] ||= @@eslevel_default [:strict] = @@strict_default if [:strict] == nil [:module] ||= @@module_default || :esm namespace = Namespace.new filters = Filter.require_filters([:filters] || Filter::DEFAULTS) unless filters.empty? = .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. = 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 = [:binding] ruby2js.ivars = [:ivars] ruby2js.eslevel = [:eslevel] ruby2js.strict = [:strict] ruby2js.comparison = [:comparison] || :equality ruby2js.or = [:or] || :logical ruby2js.module_type = [:module] || :esm ruby2js.underscored_private = ([:eslevel] < 2022) || [: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 [:scope] and not ruby2js.ivars scope = .delete(:scope) ruby2js.ivars = Hash[scope.instance_variables.map {|var| [var, scope.instance_variable_get(var)]}] end ruby2js.width = [:width] if [:width] ruby2js.enable_vertical_whitespace if source.include? "\n" ruby2js.convert ruby2js. [:file] ruby2js.file_name = [:file] || ast&.loc&.expression&.source_buffer&.name || '' ruby2js end |
.eslevel_default ⇒ Object
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, ={}) ExecJS.eval(convert(source, .merge(strict: false)).to_s) end |
.exec(source, options = {}) ⇒ Object
15 16 17 |
# File 'lib/ruby2js/execjs.rb', line 15 def self.exec(source, ={}) ExecJS.exec(convert(source, ).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_default ⇒ Object
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 = "line #{line}, column #{col}: #{e.diagnostic.message}" += "\n in file #{file}" if file raise Ruby2JS::SyntaxError.new(, e.diagnostic) end |
.strict_default ⇒ Object
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 |