@@ -31,6 +31,18 @@ def initialize(nesting, name, superclass, location, comments)
3131 end
3232 end
3333
34+ # This entry represents a constant assignment.
35+ class ConstantDefinition
36+ attr_reader :nesting , :name , :location , :comments
37+
38+ def initialize ( nesting , name , location , comments )
39+ @nesting = nesting
40+ @name = name
41+ @location = location
42+ @comments = comments
43+ end
44+ end
45+
3446 # This entry represents a module definition using the module keyword.
3547 class ModuleDefinition
3648 attr_reader :nesting , :name , :location , :comments
@@ -191,7 +203,7 @@ def location_for(iseq)
191203 end
192204
193205 def find_constant_path ( insns , index )
194- index -= 1 while insns [ index ] . is_a? ( Integer )
206+ index -= 1 while index >= 0 && ( insns [ index ] . is_a? ( Integer ) || ( insns [ index ] . is_a? ( Array ) && %i[ swap topn ] . include? ( insns [ index ] [ 0 ] ) ) )
195207 insn = insns [ index ]
196208
197209 if insn . is_a? ( Array ) && insn [ 0 ] == :opt_getconstant_path
@@ -338,6 +350,20 @@ def index_iseq(iseq, file_comments)
338350 location ,
339351 EntryComments . new ( file_comments , location )
340352 )
353+ when :setconstant
354+ next_nesting = current_nesting . dup
355+ name = insn [ 1 ]
356+
357+ _ , nesting = find_constant_path ( insns , index - 1 )
358+ next_nesting << nesting if nesting . any?
359+
360+ location = Location . new ( line , 0 )
361+ results << ConstantDefinition . new (
362+ next_nesting ,
363+ name ,
364+ location ,
365+ EntryComments . new ( file_comments , location )
366+ )
341367 when :opt_send_without_block , :send
342368 case insn [ 1 ] [ :mid ]
343369 when :attr_reader , :attr_writer , :attr_accessor
@@ -433,6 +459,22 @@ def visit_alias(node)
433459 super
434460 end
435461
462+ def visit_assign ( node )
463+ if node . target . is_a? ( VarField ) && node . target . value . is_a? ( Const )
464+ location =
465+ Location . new ( node . location . start_line , node . location . start_column )
466+
467+ results << ConstantDefinition . new (
468+ nesting . dup ,
469+ node . target . value . value . to_sym ,
470+ location ,
471+ comments_for ( node )
472+ )
473+ end
474+
475+ super
476+ end
477+
436478 def visit_class ( node )
437479 names = node . constant . accept ( ConstantNameVisitor . new )
438480 nesting << names
0 commit comments