@@ -122,56 +122,51 @@ def initialize(node, point_type: :name, name: nil)
122122 end
123123 end
124124
125- OPT_GETCONSTANT_PATH = ( RUBY_VERSION . split ( "." ) . map { |s | s . to_i } <=> [ 3 , 2 ] ) >= 0
126- private_constant :OPT_GETCONSTANT_PATH
127-
128125 def spot
129126 return nil unless @node
130127
131- if OPT_GETCONSTANT_PATH
132- # In Ruby 3.2 or later, a nested constant access (like `Foo::Bar::Baz`)
133- # is compiled to one instruction (opt_getconstant_path).
134- # @node points to the node of the whole `Foo::Bar::Baz` even if `Foo`
135- # or `Foo::Bar` causes NameError.
136- # So we try to spot the sub-node that causes the NameError by using
137- # `NameError#name`.
138- case @node . type
139- when :COLON2
140- subnodes = [ ]
141- node = @node
142- while node . type == :COLON2
143- node2 , const = node . children
144- subnodes << node if const == @name
145- node = node2
146- end
147- if node . type == :CONST || node . type == :COLON3
148- if node . children . first == @name
149- subnodes << node
150- end
151-
152- # If we found only one sub-node whose name is equal to @name, use it
153- return nil if subnodes . size != 1
154- @node = subnodes . first
155- else
156- # Do nothing; opt_getconstant_path is used only when the const base is
157- # NODE_CONST (`Foo`) or NODE_COLON3 (`::Foo`)
158- end
159- when :constant_path_node
160- subnodes = [ ]
161- node = @node
162-
163- begin
164- subnodes << node if node . name == @name
165- end while ( node = node . parent ) . is_a? ( Prism ::ConstantPathNode )
166-
167- if node . is_a? ( Prism ::ConstantReadNode ) && node . name == @name
128+ # In Ruby 3.2 or later, a nested constant access (like `Foo::Bar::Baz`)
129+ # is compiled to one instruction (opt_getconstant_path).
130+ # @node points to the node of the whole `Foo::Bar::Baz` even if `Foo`
131+ # or `Foo::Bar` causes NameError.
132+ # So we try to spot the sub-node that causes the NameError by using
133+ # `NameError#name`.
134+ case @node . type
135+ when :COLON2
136+ subnodes = [ ]
137+ node = @node
138+ while node . type == :COLON2
139+ node2 , const = node . children
140+ subnodes << node if const == @name
141+ node = node2
142+ end
143+ if node . type == :CONST || node . type == :COLON3
144+ if node . children . first == @name
168145 subnodes << node
169146 end
170147
171148 # If we found only one sub-node whose name is equal to @name, use it
172149 return nil if subnodes . size != 1
173150 @node = subnodes . first
151+ else
152+ # Do nothing; opt_getconstant_path is used only when the const base is
153+ # NODE_CONST (`Foo`) or NODE_COLON3 (`::Foo`)
174154 end
155+ when :constant_path_node
156+ subnodes = [ ]
157+ node = @node
158+
159+ begin
160+ subnodes << node if node . name == @name
161+ end while ( node = node . parent ) . is_a? ( Prism ::ConstantPathNode )
162+
163+ if node . is_a? ( Prism ::ConstantReadNode ) && node . name == @name
164+ subnodes << node
165+ end
166+
167+ # If we found only one sub-node whose name is equal to @name, use it
168+ return nil if subnodes . size != 1
169+ @node = subnodes . first
175170 end
176171
177172 case @node . type
0 commit comments