Class: Spoom::Deadcode::Indexer

Inherits:
Visitor
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spoom/deadcode/indexer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

#visit_alias_global_variable_node, #visit_alternation_pattern_node, #visit_arguments_node, #visit_array_node, #visit_array_pattern_node, #visit_assoc_node, #visit_assoc_splat_node, #visit_back_reference_read_node, #visit_begin_node, #visit_block_local_variable_node, #visit_block_node, #visit_block_parameter_node, #visit_block_parameters_node, #visit_break_node, #visit_call_target_node, #visit_capture_pattern_node, #visit_case_match_node, #visit_case_node, #visit_child_nodes, #visit_class_variable_and_write_node, #visit_class_variable_operator_write_node, #visit_class_variable_or_write_node, #visit_class_variable_read_node, #visit_class_variable_target_node, #visit_class_variable_write_node, #visit_constant_path_and_write_node, #visit_constant_path_node, #visit_constant_path_operator_write_node, #visit_constant_path_or_write_node, #visit_constant_path_target_node, #visit_constant_target_node, #visit_defined_node, #visit_else_node, #visit_embedded_statements_node, #visit_embedded_variable_node, #visit_ensure_node, #visit_false_node, #visit_find_pattern_node, #visit_flip_flop_node, #visit_float_node, #visit_for_node, #visit_forwarding_arguments_node, #visit_forwarding_parameter_node, #visit_forwarding_super_node, #visit_global_variable_and_write_node, #visit_global_variable_operator_write_node, #visit_global_variable_or_write_node, #visit_global_variable_read_node, #visit_global_variable_target_node, #visit_global_variable_write_node, #visit_hash_node, #visit_hash_pattern_node, #visit_if_node, #visit_imaginary_node, #visit_implicit_node, #visit_implicit_rest_node, #visit_in_node, #visit_index_and_write_node, #visit_index_operator_write_node, #visit_index_or_write_node, #visit_index_target_node, #visit_instance_variable_and_write_node, #visit_instance_variable_operator_write_node, #visit_instance_variable_or_write_node, #visit_instance_variable_read_node, #visit_instance_variable_target_node, #visit_instance_variable_write_node, #visit_integer_node, #visit_interpolated_match_last_line_node, #visit_interpolated_regular_expression_node, #visit_interpolated_string_node, #visit_interpolated_symbol_node, #visit_interpolated_x_string_node, #visit_keyword_hash_node, #visit_keyword_rest_parameter_node, #visit_lambda_node, #visit_local_variable_read_node, #visit_local_variable_target_node, #visit_match_last_line_node, #visit_match_predicate_node, #visit_match_required_node, #visit_match_write_node, #visit_missing_node, #visit_multi_target_node, #visit_next_node, #visit_nil_node, #visit_no_keywords_parameter_node, #visit_numbered_parameters_node, #visit_numbered_reference_read_node, #visit_optional_keyword_parameter_node, #visit_optional_parameter_node, #visit_parameters_node, #visit_parentheses_node, #visit_pinned_expression_node, #visit_pinned_variable_node, #visit_post_execution_node, #visit_pre_execution_node, #visit_program_node, #visit_range_node, #visit_rational_node, #visit_redo_node, #visit_regular_expression_node, #visit_required_keyword_parameter_node, #visit_required_parameter_node, #visit_rescue_modifier_node, #visit_rescue_node, #visit_rest_parameter_node, #visit_retry_node, #visit_return_node, #visit_self_node, #visit_singleton_class_node, #visit_source_encoding_node, #visit_source_file_node, #visit_source_line_node, #visit_splat_node, #visit_statements_node, #visit_string_node, #visit_super_node, #visit_symbol_node, #visit_true_node, #visit_undef_node, #visit_unless_node, #visit_until_node, #visit_when_node, #visit_while_node, #visit_x_string_node, #visit_yield_node

Constructor Details

#initialize(path, source, index, plugins: []) ⇒ Indexer

Returns a new instance of Indexer.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spoom/deadcode/indexer.rb', line 16

def initialize(path, source, index, plugins: [])
  super()

  @path = path
  @file_name = T.let(File.basename(path), String)
  @source = source
  @index = index
  @plugins = plugins
  @previous_node = T.let(nil, T.nilable(Prism::Node))
  @names_nesting = T.let([], T::Array[String])
  @nodes_nesting = T.let([], T::Array[Prism::Node])
  @in_const_field = T.let(false, T::Boolean)
  @in_opassign = T.let(false, T::Boolean)
  @in_symbol_literal = T.let(false, T::Boolean)
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



13
14
15
# File 'lib/spoom/deadcode/indexer.rb', line 13

def index
  @index
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/spoom/deadcode/indexer.rb', line 10

def path
  @path
end

Instance Method Details

#current_nodeObject



408
409
410
# File 'lib/spoom/deadcode/indexer.rb', line 408

def current_node
  T.must(@nodes_nesting.last)
end

#define_attr_reader(name, full_name, node) ⇒ Object



322
323
324
325
326
327
328
329
330
331
# File 'lib/spoom/deadcode/indexer.rb', line 322

def define_attr_reader(name, full_name, node)
  definition = Definition.new(
    kind: Definition::Kind::AttrReader,
    name: name,
    full_name: full_name,
    location: node_location(node),
  )
  @index.define(definition)
  @plugins.each { |plugin| plugin.internal_on_define_accessor(self, definition) }
end

#define_attr_writer(name, full_name, node) ⇒ Object



334
335
336
337
338
339
340
341
342
343
# File 'lib/spoom/deadcode/indexer.rb', line 334

def define_attr_writer(name, full_name, node)
  definition = Definition.new(
    kind: Definition::Kind::AttrWriter,
    name: name,
    full_name: full_name,
    location: node_location(node),
  )
  @index.define(definition)
  @plugins.each { |plugin| plugin.internal_on_define_accessor(self, definition) }
end

#define_class(name, full_name, node) ⇒ Object



346
347
348
349
350
351
352
353
354
355
# File 'lib/spoom/deadcode/indexer.rb', line 346

def define_class(name, full_name, node)
  definition = Definition.new(
    kind: Definition::Kind::Class,
    name: name,
    full_name: full_name,
    location: node_location(node),
  )
  @index.define(definition)
  @plugins.each { |plugin| plugin.internal_on_define_class(self, definition) }
end

#define_constant(name, full_name, node) ⇒ Object



358
359
360
361
362
363
364
365
366
367
# File 'lib/spoom/deadcode/indexer.rb', line 358

def define_constant(name, full_name, node)
  definition = Definition.new(
    kind: Definition::Kind::Constant,
    name: name,
    full_name: full_name,
    location: node_location(node),
  )
  @index.define(definition)
  @plugins.each { |plugin| plugin.internal_on_define_constant(self, definition) }
end

#define_method(name, full_name, node) ⇒ Object



370
371
372
373
374
375
376
377
378
379
# File 'lib/spoom/deadcode/indexer.rb', line 370

def define_method(name, full_name, node)
  definition = Definition.new(
    kind: Definition::Kind::Method,
    name: name,
    full_name: full_name,
    location: node_location(node),
  )
  @index.define(definition)
  @plugins.each { |plugin| plugin.internal_on_define_method(self, definition) }
end

#define_module(name, full_name, node) ⇒ Object



382
383
384
385
386
387
388
389
390
391
# File 'lib/spoom/deadcode/indexer.rb', line 382

def define_module(name, full_name, node)
  definition = Definition.new(
    kind: Definition::Kind::Module,
    name: name,
    full_name: full_name,
    location: node_location(node),
  )
  @index.define(definition)
  @plugins.each { |plugin| plugin.internal_on_define_module(self, definition) }
end

#last_sigObject



453
454
455
456
457
458
459
# File 'lib/spoom/deadcode/indexer.rb', line 453

def last_sig
  previous_call = @previous_node
  return unless previous_call.is_a?(Prism::CallNode)
  return unless previous_call.name == :sig

  previous_call.slice
end

#nesting_blockObject



427
428
429
# File 'lib/spoom/deadcode/indexer.rb', line 427

def nesting_block
  nesting_node(Prism::BlockNode)
end

#nesting_callObject



432
433
434
# File 'lib/spoom/deadcode/indexer.rb', line 432

def nesting_call
  nesting_node(Prism::CallNode)
end

#nesting_classObject



422
423
424
# File 'lib/spoom/deadcode/indexer.rb', line 422

def nesting_class
  nesting_node(Prism::ClassNode)
end

#nesting_class_nameObject



437
438
439
440
441
442
# File 'lib/spoom/deadcode/indexer.rb', line 437

def nesting_class_name
  nesting_class = self.nesting_class
  return unless nesting_class

  nesting_class.name.to_s
end

#nesting_class_superclass_nameObject



445
446
447
448
449
450
# File 'lib/spoom/deadcode/indexer.rb', line 445

def nesting_class_superclass_name
  nesting_class_superclass = nesting_class&.superclass
  return unless nesting_class_superclass

  nesting_class_superclass.slice.delete_prefix("::")
end

#nesting_node(type) ⇒ Object



413
414
415
416
417
418
419
# File 'lib/spoom/deadcode/indexer.rb', line 413

def nesting_node(type)
  @nodes_nesting.reverse_each do |node|
    return T.unsafe(node) if node.is_a?(type)
  end

  nil
end

#node_location(node) ⇒ Object



464
465
466
# File 'lib/spoom/deadcode/indexer.rb', line 464

def node_location(node)
  Location.from_prism(@path, node.location)
end

#reference_constant(name, node) ⇒ Object



396
397
398
# File 'lib/spoom/deadcode/indexer.rb', line 396

def reference_constant(name, node)
  @index.reference(Reference.new(name: name, kind: Reference::Kind::Constant, location: node_location(node)))
end

#reference_method(name, node) ⇒ Object



401
402
403
# File 'lib/spoom/deadcode/indexer.rb', line 401

def reference_method(name, node)
  @index.reference(Reference.new(name: name, kind: Reference::Kind::Method, location: node_location(node)))
end

#visit(node) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/spoom/deadcode/indexer.rb', line 35

def visit(node)
  return unless node

  @nodes_nesting << node
  super
  @nodes_nesting.pop
  @previous_node = node
end

#visit_alias_method_node(node) ⇒ Object



45
46
47
# File 'lib/spoom/deadcode/indexer.rb', line 45

def visit_alias_method_node(node)
  reference_method(node.old_name.slice, node)
end

#visit_and_node(node) ⇒ Object



50
51
52
53
# File 'lib/spoom/deadcode/indexer.rb', line 50

def visit_and_node(node)
  reference_method(node.operator_loc.slice, node)
  super
end

#visit_block_argument_node(node) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/spoom/deadcode/indexer.rb', line 56

def visit_block_argument_node(node)
  expression = node.expression
  case expression
  when Prism::SymbolNode
    reference_method(expression.unescaped, expression)
  else
    visit(expression)
  end
end

#visit_call_and_write_node(node) ⇒ Object



67
68
69
70
71
72
# File 'lib/spoom/deadcode/indexer.rb', line 67

def visit_call_and_write_node(node)
  visit(node.receiver)
  reference_method(node.read_name.to_s, node)
  reference_method(node.write_name.to_s, node)
  visit(node.value)
end

#visit_call_node(node) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/spoom/deadcode/indexer.rb', line 91

def visit_call_node(node)
  visit_send(
    Send.new(
      node: node,
      name: node.name.to_s,
      recv: node.receiver,
      args: node.arguments&.arguments || [],
      block: node.block,
    ),
  )
end

#visit_call_operator_write_node(node) ⇒ Object



75
76
77
78
79
80
# File 'lib/spoom/deadcode/indexer.rb', line 75

def visit_call_operator_write_node(node)
  visit(node.receiver)
  reference_method(node.read_name.to_s, node)
  reference_method(node.write_name.to_s, node)
  visit(node.value)
end

#visit_call_or_write_node(node) ⇒ Object



83
84
85
86
87
88
# File 'lib/spoom/deadcode/indexer.rb', line 83

def visit_call_or_write_node(node)
  visit(node.receiver)
  reference_method(node.read_name.to_s, node)
  reference_method(node.write_name.to_s, node)
  visit(node.value)
end

#visit_class_node(node) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/spoom/deadcode/indexer.rb', line 104

def visit_class_node(node)
  constant_path = node.constant_path.slice

  if constant_path.start_with?("::")
    full_name = constant_path.delete_prefix("::")

    # We found a top level definition such as `class ::A; end`, we need to reset the name nesting
    old_nesting = @names_nesting.dup
    @names_nesting.clear
    @names_nesting << full_name

    define_class(T.must(constant_path.split("::").last), full_name, node)

    # We do not call `super` here because we don't want to visit the `constant` again
    visit(node.superclass) if node.superclass
    visit(node.body)

    # Restore the name nesting once we finished visited the class
    @names_nesting.clear
    @names_nesting = old_nesting
  else
    @names_nesting << constant_path
    define_class(T.must(constant_path.split("::").last), @names_nesting.join("::"), node)

    # We do not call `super` here because we don't want to visit the `constant` again
    visit(node.superclass) if node.superclass
    visit(node.body)

    @names_nesting.pop
  end
end

#visit_constant_and_write_node(node) ⇒ Object



137
138
139
140
# File 'lib/spoom/deadcode/indexer.rb', line 137

def visit_constant_and_write_node(node)
  reference_constant(node.name.to_s, node)
  visit(node.value)
end

#visit_constant_operator_write_node(node) ⇒ Object



143
144
145
146
# File 'lib/spoom/deadcode/indexer.rb', line 143

def visit_constant_operator_write_node(node)
  reference_constant(node.name.to_s, node)
  visit(node.value)
end

#visit_constant_or_write_node(node) ⇒ Object



149
150
151
152
# File 'lib/spoom/deadcode/indexer.rb', line 149

def visit_constant_or_write_node(node)
  reference_constant(node.name.to_s, node)
  visit(node.value)
end

#visit_constant_path_write_node(node) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/spoom/deadcode/indexer.rb', line 155

def visit_constant_path_write_node(node)
  parent = node.target.parent
  name = node.target.child.slice

  if parent
    visit(parent)

    parent_name = parent.slice
    full_name = [*@names_nesting, parent_name, name].compact.join("::")
    define_constant(name, full_name, node)
  else
    define_constant(name, name, node)
  end

  visit(node.value)
end

#visit_constant_read_node(node) ⇒ Object



173
174
175
# File 'lib/spoom/deadcode/indexer.rb', line 173

def visit_constant_read_node(node)
  reference_constant(node.name.to_s, node)
end

#visit_constant_write_node(node) ⇒ Object



178
179
180
181
182
183
# File 'lib/spoom/deadcode/indexer.rb', line 178

def visit_constant_write_node(node)
  name = node.name.to_s
  full_name = [*@names_nesting, name].join("::")
  define_constant(name, full_name, node)
  visit(node.value)
end

#visit_def_node(node) ⇒ Object



186
187
188
189
190
191
# File 'lib/spoom/deadcode/indexer.rb', line 186

def visit_def_node(node)
  name = node.name.to_s
  define_method(name, [*@names_nesting, name].join("::"), node)

  super
end

#visit_local_variable_and_write_node(node) ⇒ Object



194
195
196
197
198
199
# File 'lib/spoom/deadcode/indexer.rb', line 194

def visit_local_variable_and_write_node(node)
  name = node.name.to_s
  reference_method(name, node)
  reference_method("#{name}=", node)
  visit(node.value)
end

#visit_local_variable_operator_write_node(node) ⇒ Object



202
203
204
205
206
207
# File 'lib/spoom/deadcode/indexer.rb', line 202

def visit_local_variable_operator_write_node(node)
  name = node.name.to_s
  reference_method(name, node)
  reference_method("#{name}=", node)
  visit(node.value)
end

#visit_local_variable_or_write_node(node) ⇒ Object



210
211
212
213
214
215
# File 'lib/spoom/deadcode/indexer.rb', line 210

def visit_local_variable_or_write_node(node)
  name = node.name.to_s
  reference_method(name, node)
  reference_method("#{name}=", node)
  visit(node.value)
end

#visit_local_variable_write_node(node) ⇒ Object



218
219
220
221
# File 'lib/spoom/deadcode/indexer.rb', line 218

def visit_local_variable_write_node(node)
  visit(node.value)
  reference_method("#{node.name}=", node)
end

#visit_module_node(node) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/spoom/deadcode/indexer.rb', line 224

def visit_module_node(node)
  constant_path = node.constant_path.slice

  if constant_path.start_with?("::")
    full_name = constant_path.delete_prefix("::")

    # We found a top level definition such as `class ::A; end`, we need to reset the name nesting
    old_nesting = @names_nesting.dup
    @names_nesting.clear
    @names_nesting << full_name

    define_module(T.must(constant_path.split("::").last), full_name, node)

    visit(node.body)

    # Restore the name nesting once we finished visited the class
    @names_nesting.clear
    @names_nesting = old_nesting
  else
    @names_nesting << constant_path
    define_module(T.must(constant_path.split("::").last), @names_nesting.join("::"), node)

    # We do not call `super` here because we don't want to visit the `constant` again
    visit(node.body)

    @names_nesting.pop
  end
end

#visit_multi_write_node(node) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/spoom/deadcode/indexer.rb', line 254

def visit_multi_write_node(node)
  node.lefts.each do |const|
    case const
    when Prism::ConstantTargetNode, Prism::ConstantPathTargetNode
      name = const.slice
      define_constant(T.must(name.split("::").last), [*@names_nesting, name].join("::"), const)
    when Prism::LocalVariableTargetNode
      reference_method("#{const.name}=", node)
    end
  end
  visit(node.value)
end

#visit_or_node(node) ⇒ Object



268
269
270
271
# File 'lib/spoom/deadcode/indexer.rb', line 268

def visit_or_node(node)
  reference_method(node.operator_loc.slice, node)
  super
end

#visit_send(send) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/spoom/deadcode/indexer.rb', line 274

def visit_send(send)
  visit(send.recv)

  case send.name
  when "attr_reader"
    send.args.each do |arg|
      next unless arg.is_a?(Prism::SymbolNode)

      name = arg.unescaped
      define_attr_reader(name, [*@names_nesting, name].join("::"), arg)
    end
  when "attr_writer"
    send.args.each do |arg|
      next unless arg.is_a?(Prism::SymbolNode)

      name = arg.unescaped
      define_attr_writer("#{name}=", "#{[*@names_nesting, name].join("::")}=", arg)
    end
  when "attr_accessor"
    send.args.each do |arg|
      next unless arg.is_a?(Prism::SymbolNode)

      name = arg.unescaped
      full_name = [*@names_nesting, name].join("::")
      define_attr_reader(name, full_name, arg)
      define_attr_writer("#{name}=", "#{full_name}=", arg)
    end
  else
    @plugins.each do |plugin|
      plugin.internal_on_send(self, send)
    end

    reference_method(send.name, send.node)

    case send.name
    when "<", ">", "<=", ">="
      # For comparison operators, we also reference the `<=>` method
      reference_method("<=>", send.node)
    end

    visit_all(send.args)
    visit(send.block)
  end
end