class Ruby2JS::Filter::Processor

Constants

BINARY_OPERATORS

Attributes

disable_autoimports[RW]
namespace[RW]
prepend_list[RW]

Public Class Methods

new(comments) click to toggle source
# File lib/ruby2js.rb, line 72
def initialize(comments)
  @comments = comments

  # check if magic comment is present:
  first_comment = @comments.values.first&.map(&:text)&.first
  @disable_autoimports = first_comment&.include?(" autoimports: false")
  @disable_autoexports = first_comment&.include?(" autoexports: false")

  @ast = nil
  @exclude_methods = []
  @prepend_list = Set.new
end

Public Instance Methods

es2015() click to toggle source
# File lib/ruby2js.rb, line 108
def es2015
  @options[:eslevel] >= 2015
end
es2016() click to toggle source
# File lib/ruby2js.rb, line 112
def es2016
  @options[:eslevel] >= 2016
end
es2017() click to toggle source
# File lib/ruby2js.rb, line 116
def es2017
  @options[:eslevel] >= 2017
end
es2018() click to toggle source
# File lib/ruby2js.rb, line 120
def es2018
  @options[:eslevel] >= 2018
end
es2019() click to toggle source
# File lib/ruby2js.rb, line 124
def es2019
  @options[:eslevel] >= 2019
end
es2020() click to toggle source
# File lib/ruby2js.rb, line 128
def es2020
  @options[:eslevel] >= 2020
end
es2021() click to toggle source
# File lib/ruby2js.rb, line 132
def es2021
  @options[:eslevel] >= 2021
end
es2022() click to toggle source
# File lib/ruby2js.rb, line 136
def es2022
  @options[:eslevel] >= 2022
end
modules_enabled?() click to toggle source
# File lib/ruby2js.rb, line 104
def modules_enabled?
  @modules_enabled
end
on_assign(node) click to toggle source

handle all of the 'invented/synthetic' ast types

# File lib/ruby2js.rb, line 154
def on_assign(node); end
on_async(node) click to toggle source
# File lib/ruby2js.rb, line 155
def on_async(node); on_def(node); end
on_asyncs(node) click to toggle source
# File lib/ruby2js.rb, line 156
def on_asyncs(node); on_defs(node); end
on_attr(node) click to toggle source
# File lib/ruby2js.rb, line 157
def on_attr(node); on_send(node); end
on_autoreturn(node) click to toggle source
# File lib/ruby2js.rb, line 158
def on_autoreturn(node); on_return(node); end
on_await(node) click to toggle source
# File lib/ruby2js.rb, line 159
def on_await(node); on_send(node); end
on_call(node) click to toggle source
# File lib/ruby2js.rb, line 160
def on_call(node); on_send(node); end
on_class_extend(node) click to toggle source
# File lib/ruby2js.rb, line 161
def on_class_extend(node); on_send(node); end
on_class_hash(node) click to toggle source
# File lib/ruby2js.rb, line 162
def on_class_hash(node); on_class(node); end
on_class_module(node) click to toggle source
# File lib/ruby2js.rb, line 163
def on_class_module(node); on_send(node); end
on_constructor(node) click to toggle source
# File lib/ruby2js.rb, line 164
def on_constructor(node); on_def(node); end
on_deff(node) click to toggle source
# File lib/ruby2js.rb, line 165
def on_deff(node); on_def(node); end
on_defineProps(node) click to toggle source
# File lib/ruby2js.rb, line 177
def on_defineProps(node); end
on_defm(node) click to toggle source
# File lib/ruby2js.rb, line 166
def on_defm(node); on_defs(node); end
on_defp(node) click to toggle source
# File lib/ruby2js.rb, line 167
def on_defp(node); on_defs(node); end
on_export(node) click to toggle source
# File lib/ruby2js.rb, line 181
def on_export(node); end
on_for_of(node) click to toggle source
# File lib/ruby2js.rb, line 168
def on_for_of(node); on_for(node); end
on_hide(node) click to toggle source
# File lib/ruby2js.rb, line 178
def on_hide(node); on_begin(node); end
on_import(node) click to toggle source
# File lib/ruby2js.rb, line 182
def on_import(node); end
on_in?(node) click to toggle source
# File lib/ruby2js.rb, line 169
def on_in?(node); on_send(node); end
on_method(node) click to toggle source
# File lib/ruby2js.rb, line 170
def on_method(node); on_send(node); end
on_module_hash(node) click to toggle source
# File lib/ruby2js.rb, line 171
def on_module_hash(node); on_module(node); end
on_nil(node) click to toggle source
# File lib/ruby2js.rb, line 179
def on_nil(node); end
on_prop(node) click to toggle source
# File lib/ruby2js.rb, line 172
def on_prop(node); on_array(node); end
on_prototype(node) click to toggle source
# File lib/ruby2js.rb, line 173
def on_prototype(node); on_begin(node); end
on_send(node) click to toggle source

convert map(&:symbol) to a block

Calls superclass method
# File lib/ruby2js.rb, line 189
def on_send(node)
  if node.children.length > 2 and node.children.last.type == :block_pass
    method = node.children.last.children.first.children.last
    if BINARY_OPERATORS.include? method
      return on_block s(:block, s(:send, *node.children[0..-2]),
        s(:args, s(:arg, :a), s(:arg, :b)), s(:return,
        process(s(:send, s(:lvar, :a), method, s(:lvar, :b)))))
    elsif node.children.last.children.first.type == :sym
      return on_block s(:block, s(:send, *node.children[0..-2]),
        s(:args, s(:arg, :item)), s(:return,
        process(s(:attr, s(:lvar, :item), method))))
    else
      super
    end
  end
  super
end
on_send!(node) click to toggle source
# File lib/ruby2js.rb, line 174
def on_send!(node); on_send(node); end
on_sendw(node) click to toggle source
# File lib/ruby2js.rb, line 175
def on_sendw(node); on_send(node); end
on_sym(node) click to toggle source

provide a method so filters can call 'super'

# File lib/ruby2js.rb, line 186
def on_sym(node); node; end
on_taglit(node) click to toggle source
# File lib/ruby2js.rb, line 183
def on_taglit(node); on_pair(node); end
on_undefined?(node) click to toggle source
# File lib/ruby2js.rb, line 176
def on_undefined?(node); on_defined?(node); end
on_xnode(node) click to toggle source
# File lib/ruby2js.rb, line 180
def on_xnode(node); end
options=(options) click to toggle source
# File lib/ruby2js.rb, line 85
def options=(options)
  @options = options

  @included = Filter.included_methods
  @excluded = Filter.excluded_methods

  include_all if options[:include_all]
  include_only(options[:include_only]) if options[:include_only]
  include(options[:include]) if options[:include]
  exclude(options[:exclude]) if options[:exclude]

  filters = options[:filters] || DEFAULTS
  @modules_enabled =
    (defined? Ruby2JS::Filter::ESM and
    filters.include? Ruby2JS::Filter::ESM) or
    (defined? Ruby2JS::Filter::CJS and
    filters.include? Ruby2JS::Filter::CJS)
end
process(node) click to toggle source
Calls superclass method
# File lib/ruby2js.rb, line 140
def process(node)
  ast, @ast = @ast, node
  replacement = super

  if replacement != node and @comments[node]
    @comments[replacement] = @comments[node]
  end

  replacement
ensure
  @ast = ast
end