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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ruby2JS::Filter

exclude, #exclude, #excluded?, excluded_methods, #include, include, include_all, #include_all, include_only, #include_only, included_methods

Constructor Details

#initialize(comments) ⇒ Processor

Returns a new instance of Processor.



72
73
74
75
76
77
78
79
80
81
82
83
# 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

Instance Attribute Details

#disable_autoimportsObject

Returns the value of attribute disable_autoimports.



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

def disable_autoimports
  @disable_autoimports
end

#namespaceObject

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#prepend_listObject

Returns the value of attribute prepend_list.



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

def prepend_list
  @prepend_list
end

Instance Method Details

#es2015Object



97
98
99
# File 'lib/ruby2js.rb', line 97

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

#es2016Object



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

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

#es2017Object



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

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

#es2018Object



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

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

#es2019Object



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

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

#es2020Object



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

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

#es2021Object



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

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

#es2022Object



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

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

#on_assign(node) ⇒ Object

handle all of the ‘invented/synthetic’ ast types



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

def on_assign(node); end

#on_async(node) ⇒ Object



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

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

#on_asyncs(node) ⇒ Object



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

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

#on_attr(node) ⇒ Object



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

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

#on_autoreturn(node) ⇒ Object



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

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

#on_await(node) ⇒ Object



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

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

#on_call(node) ⇒ Object



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

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

#on_class_extend(node) ⇒ Object



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

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

#on_class_hash(node) ⇒ Object



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

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

#on_class_module(node) ⇒ Object



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

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

#on_constructor(node) ⇒ Object



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

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

#on_deff(node) ⇒ Object



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

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

#on_defineProps(node) ⇒ Object



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

def on_defineProps(node); end

#on_defm(node) ⇒ Object



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

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

#on_defp(node) ⇒ Object



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

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

#on_export(node) ⇒ Object



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

def on_export(node); end

#on_for_of(node) ⇒ Object



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

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

#on_hide(node) ⇒ Object



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

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

#on_import(node) ⇒ Object



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

def on_import(node); end

#on_in?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#on_method(node) ⇒ Object



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

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

#on_module_hash(node) ⇒ Object



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

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

#on_nil(node) ⇒ Object



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

def on_nil(node); end

#on_prop(node) ⇒ Object



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

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

#on_prototype(node) ⇒ Object



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

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

#on_send(node) ⇒ Object

convert map(&:symbol) to a block



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ruby2js.rb', line 178

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)))))
    else
      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))))
    end
  end
  super
end

#on_send!(node) ⇒ Object



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

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

#on_sendw(node) ⇒ Object



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

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

#on_sym(node) ⇒ Object

provide a method so filters can call ‘super’



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

def on_sym(node); node; end

#on_taglit(node) ⇒ Object



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

def on_taglit(node); end

#on_undefined?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#on_xnode(node) ⇒ Object



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

def on_xnode(node); end

#options=(options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# 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]
end

#process(node) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ruby2js.rb', line 129

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