module Ruby2JS::Filter

Constants

DEFAULTS
PRESET_FILTERS

Public Class Methods

autoregister(lib_dir = File.expand_path("..", __dir__)) click to toggle source
# File lib/ruby2js/filter.rb, line 14
def self.autoregister(lib_dir = File.expand_path("..", __dir__))
  Dir["#{lib_dir}/ruby2js/filter/*.rb"].sort.each do |file|
    filter = File.basename(file, '.rb')
    registered_filters[filter] = file
  end

  registered_filters
end
exclude(*methods) click to toggle source

indicate that the specified methods are not to be processed

# File lib/ruby2js/filter.rb, line 75
def self.exclude(*methods)
  if @@included
    @@included -= methods.flatten
  else
    @@excluded += methods.flatten
  end
end
excluded_methods() click to toggle source
# File lib/ruby2js/filter.rb, line 70
def self.excluded_methods
  @@excluded&.dup
end
include(*methods) click to toggle source

indicate that the specified methods are to be processed

# File lib/ruby2js/filter.rb, line 95
def self.include(*methods)
  if @@included
    @@included += methods.flatten
  else
    @@excluded -= methods.flatten
  end
end
include_all() click to toggle source

indicate that all methods are to be processed

# File lib/ruby2js/filter.rb, line 84
def self.include_all
  @@included = nil
  @@excluded = []
end
include_only(*methods) click to toggle source

indicate that only the specified methods are to be processed

# File lib/ruby2js/filter.rb, line 90
def self.include_only(*methods)
  @@included = methods.flatten
end
included_methods() click to toggle source
# File lib/ruby2js/filter.rb, line 66
def self.included_methods
  @@included&.dup
end
registered_filters() click to toggle source
# File lib/ruby2js/filter.rb, line 10
def self.registered_filters
  @@registered_filters ||= {}
end
require_filters(filters) click to toggle source

TODO: better document this code path

# File lib/ruby2js/filter.rb, line 24
def self.require_filters(filters)
  mods = []
  filters.each do |name|
    if name.is_a?(Module)
      mods << name
      next
    end

    name = name.to_s

    if registered_filters[name].is_a?(Module)
      mods << registered_filters[name]
      next
    end

    begin
      if registered_filters.include? name
        require registered_filters[name]

        Ruby2JS::Filter::DEFAULTS.each do |mod|
          method = mod.instance_method(mod.instance_methods.first)
          if registered_filters[name] == method.source_location.first
            mods << mod
          end
        end
      elsif not name.empty? and name =~ /^[-\w+]$/
        $load_error = "UNKNOWN filter: #{name}"
      end
    rescue Exception => $load_error
    end
  end

  mods
end

Public Instance Methods

exclude(*methods) click to toggle source

indicate that the specified methods are not to be processed

# File lib/ruby2js/filter.rb, line 138
def exclude(*methods)
  if @included
    @included -= methods.flatten
  else
    @excluded += methods.flatten
  end
end
excluded?(method) click to toggle source

determine if a method is NOT to be processed

# File lib/ruby2js/filter.rb, line 108
def excluded?(method)
  if @included
    not @included.include? method
  else
    return true if @exclude_methods.flatten.include? method
    @excluded&.include? method
  end
end
include(*methods) click to toggle source

indicate that the specified methods are to be processed

# File lib/ruby2js/filter.rb, line 129
def include(*methods)
  if @included
    @included += methods.flatten
  else
    @excluded -= methods.flatten
  end
end
include_all() click to toggle source

indicate that all methods are to be processed

# File lib/ruby2js/filter.rb, line 118
def include_all
  @included = nil
  @excluded = []
end
include_only(*methods) click to toggle source

indicate that only the specified methods are to be processed

# File lib/ruby2js/filter.rb, line 124
def include_only(*methods)
  @included = methods.flatten
end

Private Instance Methods

find_autoimport(token) click to toggle source
# File lib/ruby2js/filter/esm.rb, line 192
def find_autoimport(token)
  return nil if @esm_autoimports.nil?
  return nil if @esm_explicit_tokens.include?(token)

  token = camelCase(token) if respond_to?(:camelCase)

  if @esm_autoimports[token]
    [@esm_autoimports[token], s(:const, nil, token)]
  elsif found_key = @esm_autoimports.keys.find {|key| key.is_a?(Array) && key.include?(token)}
    [@esm_autoimports[found_key], found_key.map {|key| s(:const, nil, key)}]
  end
end