@@ -5,9 +5,9 @@ module RubyLsp
55 # A minimalistic type checker to try to resolve types that can be inferred without requiring a type system or
66 # annotations
77 class TypeInferrer
8- #: (RubyIndexer::Index index ) -> void
9- def initialize ( index )
10- @index = index
8+ #: (Rubydex::Graph ) -> void
9+ def initialize ( graph )
10+ @graph = graph
1111 end
1212
1313 #: (NodeContext node_context) -> Type?
@@ -81,11 +81,10 @@ def infer_receiver_for_call_node(node, node_context)
8181 receiver_name = RubyIndexer ::Index . constant_name ( receiver )
8282 return unless receiver_name
8383
84- resolved_receiver = @index . resolve ( receiver_name , node_context . nesting )
85- name = resolved_receiver &.first &.name
86- return unless name
84+ resolved_receiver = @graph . resolve_constant ( receiver_name , node_context . nesting )
85+ return unless resolved_receiver
8786
88- *parts , last = name . split ( "::" )
87+ *parts , last = resolved_receiver . name . split ( "::" )
8988 return Type . new ( "#{ last } ::<#{ last } >" ) if parts . empty?
9089
9190 Type . new ( "#{ parts . join ( "::" ) } ::#{ last } ::<#{ last } >" )
@@ -96,12 +95,14 @@ def infer_receiver_for_call_node(node, node_context)
9695 # When invoking `new`, we recursively infer the type of the receiver to get the class type its being invoked
9796 # on and then return the attached version of that type, since it's being instantiated.
9897 type = infer_receiver_for_call_node ( receiver , node_context )
99-
10098 return unless type
10199
102100 # If the method `new` was overridden, then we cannot assume that it will return a new instance of the class
103- new_method = @index . resolve_method ( "new" , type . name ) &.first
104- return if new_method && new_method . owner &.name != "Class"
101+ declaration = @graph [ type . name ] #: as Rubydex::Namespace?
102+ return unless declaration
103+
104+ new_method = declaration . find_member ( "new()" )
105+ return if new_method && new_method . owner . name != "Class"
105106
106107 type . attached
107108 elsif raw_receiver
@@ -121,11 +122,11 @@ def guess_type(raw_receiver, nesting)
121122 . map ( &:capitalize )
122123 . join
123124
124- entries = @index . resolve ( guessed_name , nesting ) || @index . first_unqualified_const ( guessed_name )
125- name = entries &. first &. name
126- return unless name
125+ declaration = @graph . resolve_constant ( guessed_name , nesting )
126+ declaration ||= @graph . search ( guessed_name ) . first
127+ return unless declaration
127128
128- GuessedType . new ( name )
129+ GuessedType . new ( declaration . name )
129130 end
130131
131132 #: (NodeContext node_context) -> Type
@@ -148,7 +149,6 @@ def self_receiver_handling(node_context)
148149 #: (NodeContext node_context) -> Type?
149150 def infer_receiver_for_class_variables ( node_context )
150151 nesting_parts = node_context . nesting . dup
151-
152152 return Type . new ( "Object" ) if nesting_parts . empty?
153153
154154 nesting_parts . reverse_each do |part |
@@ -157,9 +157,11 @@ def infer_receiver_for_class_variables(node_context)
157157 nesting_parts . pop
158158 end
159159
160- receiver_name = nesting_parts . join ( "::" )
161- resolved_receiver = @index . resolve ( receiver_name , node_context . nesting ) &.first
162- return unless resolved_receiver &.name
160+ resolved_receiver = @graph . resolve_constant (
161+ nesting_parts . last , #: as !nil
162+ nesting_parts [ 0 ...-1 ] , #: as !nil
163+ )
164+ return unless resolved_receiver
163165
164166 Type . new ( resolved_receiver . name )
165167 end
0 commit comments