Class: Ruby2JS::Filter::Processor

Inherits:
Parser::AST::Processor
  • Object
show all
Includes:
Ruby2JS::Filter
Defined in:
lib/ruby2js.rb

Constant Summary collapse

BINARY_OPERATORS =
Converter::OPERATORS[2..-1].flatten

Constants included from Ruby2JS::Filter

DEFAULTS, PRESET_FILTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ruby2JS::Filter

autoregister, exclude, #exclude, #excluded?, excluded_methods, include, #include, include_all, #include_all, include_only, #include_only, included_methods, registered_filters, require_filters

Constructor Details

#initialize(comments) ⇒ Processor

Returns a new instance of Processor.



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

def initialize(comments)
  @comments = comments

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

Instance Attribute Details

#disable_autoexportsObject

Returns the value of attribute disable_autoexports.



73
74
75
# File 'lib/ruby2js.rb', line 73

def disable_autoexports
  @disable_autoexports
end

#disable_autoimportsObject

Returns the value of attribute disable_autoimports.



73
74
75
# File 'lib/ruby2js.rb', line 73

def disable_autoimports
  @disable_autoimports
end

#namespaceObject

Returns the value of attribute namespace.



73
74
75
# File 'lib/ruby2js.rb', line 73

def namespace
  @namespace
end

#prepend_listObject

Returns the value of attribute prepend_list.



73
74
75
# File 'lib/ruby2js.rb', line 73

def prepend_list
  @prepend_list
end

Instance Method Details

#es2015Object



106
107
108
# File 'lib/ruby2js.rb', line 106

def es2015
  @options[:eslevel] >= 2015
end

#es2016Object



110
111
112
# File 'lib/ruby2js.rb', line 110

def es2016
  @options[:eslevel] >= 2016
end

#es2017Object



114
115
116
# File 'lib/ruby2js.rb', line 114

def es2017
  @options[:eslevel] >= 2017
end

#es2018Object



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

def es2018
  @options[:eslevel] >= 2018
end

#es2019Object



122
123
124
# File 'lib/ruby2js.rb', line 122

def es2019
  @options[:eslevel] >= 2019
end

#es2020Object



126
127
128
# File 'lib/ruby2js.rb', line 126

def es2020
  @options[:eslevel] >= 2020
end

#es2021Object



130
131
132
# File 'lib/ruby2js.rb', line 130

def es2021
  @options[:eslevel] >= 2021
end

#es2022Object



134
135
136
# File 'lib/ruby2js.rb', line 134

def es2022
  @options[:eslevel] >= 2022
end

#modules_enabled?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/ruby2js.rb', line 102

def modules_enabled?
  @modules_enabled
end

#on_assign(node) ⇒ Object

handle all of the ‘invented/synthetic’ ast types



152
# File 'lib/ruby2js.rb', line 152

def on_assign(node); end

#on_async(node) ⇒ Object



153
# File 'lib/ruby2js.rb', line 153

def on_async(node); on_def(node); end

#on_asyncs(node) ⇒ Object



154
# File 'lib/ruby2js.rb', line 154

def on_asyncs(node); on_defs(node); end

#on_attr(node) ⇒ Object



155
# File 'lib/ruby2js.rb', line 155

def on_attr(node); on_send(node); end

#on_autoreturn(node) ⇒ Object



156
# File 'lib/ruby2js.rb', line 156

def on_autoreturn(node); on_return(node); end

#on_await(node) ⇒ Object



157
# File 'lib/ruby2js.rb', line 157

def on_await(node); on_send(node); end

#on_call(node) ⇒ Object



158
# File 'lib/ruby2js.rb', line 158

def on_call(node); on_send(node); end

#on_class_extend(node) ⇒ Object



159
# File 'lib/ruby2js.rb', line 159

def on_class_extend(node); on_send(node); end

#on_class_hash(node) ⇒ Object



160
# File 'lib/ruby2js.rb', line 160

def on_class_hash(node); on_class(node); end

#on_class_module(node) ⇒ Object



161
# File 'lib/ruby2js.rb', line 161

def on_class_module(node); on_send(node); end

#on_constructor(node) ⇒ Object



162
# File 'lib/ruby2js.rb', line 162

def on_constructor(node); on_def(node); end

#on_deff(node) ⇒ Object



163
# File 'lib/ruby2js.rb', line 163

def on_deff(node); on_def(node); end

#on_defineProps(node) ⇒ Object



175
# File 'lib/ruby2js.rb', line 175

def on_defineProps(node); end

#on_defm(node) ⇒ Object



164
# File 'lib/ruby2js.rb', line 164

def on_defm(node); on_defs(node); end

#on_defp(node) ⇒ Object



165
# File 'lib/ruby2js.rb', line 165

def on_defp(node); on_defs(node); end

#on_export(node) ⇒ Object



179
# File 'lib/ruby2js.rb', line 179

def on_export(node); end

#on_for_of(node) ⇒ Object



166
# File 'lib/ruby2js.rb', line 166

def on_for_of(node); on_for(node); end

#on_hide(node) ⇒ Object



176
# File 'lib/ruby2js.rb', line 176

def on_hide(node); on_begin(node); end

#on_import(node) ⇒ Object



180
# File 'lib/ruby2js.rb', line 180

def on_import(node); end

#on_in?(node) ⇒ Boolean

Returns:

  • (Boolean)


167
# File 'lib/ruby2js.rb', line 167

def on_in?(node); on_send(node); end

#on_method(node) ⇒ Object



168
# File 'lib/ruby2js.rb', line 168

def on_method(node); on_send(node); end

#on_module_hash(node) ⇒ Object



169
# File 'lib/ruby2js.rb', line 169

def on_module_hash(node); on_module(node); end

#on_nil(node) ⇒ Object



177
# File 'lib/ruby2js.rb', line 177

def on_nil(node); end

#on_numblock(node) ⇒ Object

convert numbered parameters block to a normal block



187
188
189
190
191
192
193
194
195
# File 'lib/ruby2js.rb', line 187

def on_numblock(node)
  call, count, block = node.children

  process s(:block,
    call,
    s(:args, *((1..count).map {|i| s(:arg, "_#{i}")})),
    block
  )
end

#on_prop(node) ⇒ Object



170
# File 'lib/ruby2js.rb', line 170

def on_prop(node); on_array(node); end

#on_prototype(node) ⇒ Object



171
# File 'lib/ruby2js.rb', line 171

def on_prototype(node); on_begin(node); end

#on_send(node) ⇒ Object

convert map(&:symbol) to a block



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/ruby2js.rb', line 198

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) ⇒ Object



172
# File 'lib/ruby2js.rb', line 172

def on_send!(node); on_send(node); end

#on_sendw(node) ⇒ Object



173
# File 'lib/ruby2js.rb', line 173

def on_sendw(node); on_send(node); end

#on_sym(node) ⇒ Object

provide a method so filters can call ‘super’



184
# File 'lib/ruby2js.rb', line 184

def on_sym(node); node; end

#on_taglit(node) ⇒ Object



181
# File 'lib/ruby2js.rb', line 181

def on_taglit(node); on_pair(node); end

#on_undefined?(node) ⇒ Boolean

Returns:

  • (Boolean)


174
# File 'lib/ruby2js.rb', line 174

def on_undefined?(node); on_defined?(node); end

#on_xnode(node) ⇒ Object



178
# File 'lib/ruby2js.rb', line 178

def on_xnode(node); end

#options=(options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby2js.rb', line 83

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) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ruby2js.rb', line 138

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