Module: Ruby2JS::Filter

Included in:
Processor
Defined in:
lib/ruby2js.rb,
lib/ruby2js/filter.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/filter/node.rb,
lib/ruby2js/filter/react.rb,
lib/ruby2js/filter/jquery.rb,
lib/ruby2js/filter/return.rb,
lib/ruby2js/filter/require.rb,
lib/ruby2js/filter/matchAll.rb,
lib/ruby2js/filter/nokogiri.rb,
lib/ruby2js/filter/stimulus.rb,
lib/ruby2js/filter/camelCase.rb,
lib/ruby2js/filter/functions.rb,
lib/ruby2js/filter/underscore.rb,
lib/ruby2js/filter/securerandom.rb,
lib/ruby2js/filter/active_functions.rb,
lib/ruby2js/filter/minitest-jasmine.rb,
lib/ruby2js/filter/tagged_templates.rb

Defined Under Namespace

Modules: ActiveFunctions, CJS, CamelCase, ESM, Functions, JQuery, JSX, Lit, MatchAll, MiniTestJasmine, Node, Nokogiri, React, Require, Return, SEXP, SecureRandom, Stimulus, TaggedTemplates, Underscore, Vue Classes: Processor

Constant Summary collapse

DEFAULTS =
[]
PRESET_FILTERS =
[:esm, :functions, :return]
@@included =

module level defaults

nil
@@excluded =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.autoregister(lib_dir = File.expand_path("..", __dir__)) ⇒ Object



14
15
16
17
18
19
20
21
# 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) ⇒ Object

indicate that the specified methods are not to be processed



75
76
77
78
79
80
81
# File 'lib/ruby2js/filter.rb', line 75

def self.exclude(*methods)
  if @@included
    @@included -= methods.flatten
  else
    @@excluded += methods.flatten
  end
end

.excluded_methodsObject



70
71
72
# File 'lib/ruby2js/filter.rb', line 70

def self.excluded_methods
  @@excluded&.dup
end

.include(*methods) ⇒ Object

indicate that the specified methods are to be processed



95
96
97
98
99
100
101
# File 'lib/ruby2js/filter.rb', line 95

def self.include(*methods)
  if @@included
    @@included += methods.flatten
  else
    @@excluded -= methods.flatten
  end
end

.include_allObject

indicate that all methods are to be processed



84
85
86
87
# File 'lib/ruby2js/filter.rb', line 84

def self.include_all
  @@included = nil
  @@excluded = []
end

.include_only(*methods) ⇒ Object

indicate that only the specified methods are to be processed



90
91
92
# File 'lib/ruby2js/filter.rb', line 90

def self.include_only(*methods)
  @@included = methods.flatten
end

.included_methodsObject



66
67
68
# File 'lib/ruby2js/filter.rb', line 66

def self.included_methods
  @@included&.dup
end

.registered_filtersObject



10
11
12
# File 'lib/ruby2js/filter.rb', line 10

def self.registered_filters
  @@registered_filters ||= {}
end

.require_filters(filters) ⇒ Object

TODO: better document this code path



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

Instance Method Details

#exclude(*methods) ⇒ Object

indicate that the specified methods are not to be processed



138
139
140
141
142
143
144
# File 'lib/ruby2js/filter.rb', line 138

def exclude(*methods)
  if @included
    @included -= methods.flatten
  else
    @excluded += methods.flatten
  end
end

#excluded?(method) ⇒ Boolean

determine if a method is NOT to be processed

Returns:



108
109
110
111
112
113
114
115
# 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) ⇒ Object

indicate that the specified methods are to be processed



129
130
131
132
133
134
135
# File 'lib/ruby2js/filter.rb', line 129

def include(*methods)
  if @included
    @included += methods.flatten
  else
    @excluded -= methods.flatten
  end
end

#include_allObject

indicate that all methods are to be processed



118
119
120
121
# File 'lib/ruby2js/filter.rb', line 118

def include_all
  @included = nil
  @excluded = []
end

#include_only(*methods) ⇒ Object

indicate that only the specified methods are to be processed



124
125
126
# File 'lib/ruby2js/filter.rb', line 124

def include_only(*methods)
  @included = methods.flatten
end