Method: TypeProf::Core::Service#references

Defined in:
lib/typeprof/core/service.rb

#references(path, pos) ⇒ Object

: (String, TypeProf::CodePosition) -> Array[[String?, TypeProf::CodeRange]]?



222
223
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
252
253
254
255
256
257
258
259
# File 'lib/typeprof/core/service.rb', line 222

def references(path, pos)
  refs = []
  @rb_text_nodes[path]&.retrieve_at(pos) do |node|
    case node
    when AST::DefNode
      if node.mid_code_range.include?(pos)
        node.boxes(:mdef) do |mdef|
          me = @genv.resolve_method(mdef.cpath, mdef.singleton, mdef.mid)
          if me
            me.method_call_boxes.each do |box|
              node = box.node
              refs << [node.lenv.path, node.code_range]
            end
          end
        end
      end
    # TODO: Callsite
    when AST::ConstantReadNode
      if node.cname_code_range.include?(pos)
        node.boxes(:cread) do |cread_box|
          @genv.resolve_const(cread_box.const_read.cpath).read_boxes.each do |box|
            node = box.node
            refs << [node.lenv.path, node.code_range]
          end
        end
      end
    when AST::ConstantWriteNode
      if node.cname_code_range && node.cname_code_range.include?(pos) && node.static_cpath
        @genv.resolve_const(node.static_cpath).read_boxes.each do |box|
          node = box.node
          refs << [node.lenv.path, node.code_range]
        end
      end
    end
  end
  refs = refs.uniq
  return refs.empty? ? nil : refs
end