Class: RubyIndexer::IndexVisitor

Inherits:
Prism::Visitor
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_indexer/lib/ruby_indexer/visitor.rb

Instance Method Summary collapse

Constructor Details

#initialize(index, parse_result, file_path) ⇒ IndexVisitor

Returns a new instance of IndexVisitor.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 9

def initialize(index, parse_result, file_path)
  @index = index
  @file_path = file_path
  @stack = T.let([], T::Array[String])
   = T.let(
    parse_result.comments.to_h do |c|
      [c.location.start_line, c]
    end,
    T::Hash[Integer, Prism::Comment],
  )

  super()
end

Instance Method Details

#visit_call_node(node) ⇒ Object



118
119
120
121
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 118

def visit_call_node(node)
  message = node.message
  handle_private_constant(node) if message == "private_constant"
end

#visit_class_node(node) ⇒ Object



24
25
26
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 24

def visit_class_node(node)
  add_class_entry(node)
end

#visit_constant_and_write_node(node) ⇒ Object



106
107
108
109
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 106

def visit_constant_and_write_node(node)
  name = fully_qualify_name(node.name.to_s)
  add_constant(node, name)
end

#visit_constant_operator_write_node(node) ⇒ Object



112
113
114
115
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 112

def visit_constant_operator_write_node(node)
  name = fully_qualify_name(node.name.to_s)
  add_constant(node, name)
end

#visit_constant_or_write_node(node) ⇒ Object



100
101
102
103
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 100

def visit_constant_or_write_node(node)
  name = fully_qualify_name(node.name.to_s)
  add_constant(node, name)
end

#visit_constant_path_and_write_node(node) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 84

def visit_constant_path_and_write_node(node)
  # ignore variable constants like `var::FOO` or `self.class::FOO`
  target = node.target
  return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

  name = fully_qualify_name(target.location.slice)
  add_constant(node, name)
end

#visit_constant_path_operator_write_node(node) ⇒ Object



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

def visit_constant_path_operator_write_node(node)
  # ignore variable constants like `var::FOO` or `self.class::FOO`
  target = node.target
  return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

  name = fully_qualify_name(target.location.slice)
  add_constant(node, name)
end

#visit_constant_path_or_write_node(node) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 64

def visit_constant_path_or_write_node(node)
  # ignore variable constants like `var::FOO` or `self.class::FOO`
  target = node.target
  return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

  name = fully_qualify_name(target.location.slice)
  add_constant(node, name)
end

#visit_constant_path_write_node(node) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 54

def visit_constant_path_write_node(node)
  # ignore variable constants like `var::FOO` or `self.class::FOO`
  target = node.target
  return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

  name = fully_qualify_name(target.location.slice)
  add_constant(node, name)
end

#visit_constant_write_node(node) ⇒ Object



94
95
96
97
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 94

def visit_constant_write_node(node)
  name = fully_qualify_name(node.name.to_s)
  add_constant(node, name)
end

#visit_def_node(node) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 124

def visit_def_node(node)
  method_name = node.name.to_s
  comments = collect_comments(node)
  case node.receiver
  when nil
    @index << Entry::InstanceMethod.new(method_name, @file_path, node.location, comments, node.parameters)
  when Prism::SelfNode
    @index << Entry::SingletonMethod.new(method_name, @file_path, node.location, comments, node.parameters)
  end
end

#visit_module_node(node) ⇒ Object



29
30
31
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 29

def visit_module_node(node)
  add_module_entry(node)
end

#visit_multi_write_node(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 34

def visit_multi_write_node(node)
  value = node.value
  values = value.is_a?(Prism::ArrayNode) && value.opening_loc ? value.elements : []

  [*node.lefts, *node.rest, *node.rights].each_with_index do |target, i|
    current_value = values[i]
    # The moment we find a splat on the right hand side of the assignment, we can no longer figure out which value
    # gets assigned to what
    values.clear if current_value.is_a?(Prism::SplatNode)

    case target
    when Prism::ConstantTargetNode
      add_constant(target, fully_qualify_name(target.name.to_s), current_value)
    when Prism::ConstantPathTargetNode
      add_constant(target, fully_qualify_name(target.slice), current_value)
    end
  end
end