@@ -18,13 +18,12 @@ class DeclarationListener
18
18
parse_result : Prism ::ParseResult ,
19
19
file_path : String ,
20
20
collect_comments : T ::Boolean ,
21
- enhancements : T ::Array [ Enhancement ] ,
22
21
) . void
23
22
end
24
- def initialize ( index , dispatcher , parse_result , file_path , collect_comments : false , enhancements : [ ] )
23
+ def initialize ( index , dispatcher , parse_result , file_path , collect_comments : false )
25
24
@index = index
26
25
@file_path = file_path
27
- @enhancements = enhancements
26
+ @enhancements = T . let ( Enhancement . all ( self ) , T :: Array [ Enhancement ] )
28
27
@visibility_stack = T . let ( [ Entry ::Visibility ::PUBLIC ] , T ::Array [ Entry ::Visibility ] )
29
28
@comments_by_line = T . let (
30
29
parse_result . comments . to_h do |c |
@@ -86,15 +85,9 @@ def initialize(index, dispatcher, parse_result, file_path, collect_comments: fal
86
85
87
86
sig { params ( node : Prism ::ClassNode ) . void }
88
87
def on_class_node_enter ( node )
89
- @visibility_stack . push ( Entry ::Visibility ::PUBLIC )
90
88
constant_path = node . constant_path
91
- name = constant_path . slice
92
-
93
- comments = collect_comments ( node )
94
-
95
89
superclass = node . superclass
96
-
97
- nesting = actual_nesting ( name )
90
+ nesting = actual_nesting ( constant_path . slice )
98
91
99
92
parent_class = case superclass
100
93
when Prism ::ConstantReadNode , Prism ::ConstantPathNode
@@ -113,53 +106,29 @@ def on_class_node_enter(node)
113
106
end
114
107
end
115
108
116
- entry = Entry :: Class . new (
109
+ add_class (
117
110
nesting ,
118
- @file_path ,
119
- Location . from_prism_location ( node . location , @code_units_cache ) ,
120
- Location . from_prism_location ( constant_path . location , @code_units_cache ) ,
121
- comments ,
122
- parent_class ,
111
+ node . location ,
112
+ constant_path . location ,
113
+ parent_class_name : parent_class ,
114
+ comments : collect_comments ( node ) ,
123
115
)
124
-
125
- @owner_stack << entry
126
- @index . add ( entry )
127
- @stack << name
128
116
end
129
117
130
118
sig { params ( node : Prism ::ClassNode ) . void }
131
119
def on_class_node_leave ( node )
132
- @stack . pop
133
- @owner_stack . pop
134
- @visibility_stack . pop
120
+ pop_namespace_stack
135
121
end
136
122
137
123
sig { params ( node : Prism ::ModuleNode ) . void }
138
124
def on_module_node_enter ( node )
139
- @visibility_stack . push ( Entry ::Visibility ::PUBLIC )
140
125
constant_path = node . constant_path
141
- name = constant_path . slice
142
-
143
- comments = collect_comments ( node )
144
-
145
- entry = Entry ::Module . new (
146
- actual_nesting ( name ) ,
147
- @file_path ,
148
- Location . from_prism_location ( node . location , @code_units_cache ) ,
149
- Location . from_prism_location ( constant_path . location , @code_units_cache ) ,
150
- comments ,
151
- )
152
-
153
- @owner_stack << entry
154
- @index . add ( entry )
155
- @stack << name
126
+ add_module ( constant_path . slice , node . location , constant_path . location , comments : collect_comments ( node ) )
156
127
end
157
128
158
129
sig { params ( node : Prism ::ModuleNode ) . void }
159
130
def on_module_node_leave ( node )
160
- @stack . pop
161
- @owner_stack . pop
162
- @visibility_stack . pop
131
+ pop_namespace_stack
163
132
end
164
133
165
134
sig { params ( node : Prism ::SingletonClassNode ) . void }
@@ -201,9 +170,7 @@ def on_singleton_class_node_enter(node)
201
170
202
171
sig { params ( node : Prism ::SingletonClassNode ) . void }
203
172
def on_singleton_class_node_leave ( node )
204
- @stack . pop
205
- @owner_stack . pop
206
- @visibility_stack . pop
173
+ pop_namespace_stack
207
174
end
208
175
209
176
sig { params ( node : Prism ::MultiWriteNode ) . void }
@@ -318,7 +285,7 @@ def on_call_node_enter(node)
318
285
end
319
286
320
287
@enhancements . each do |enhancement |
321
- enhancement . on_call_node_enter ( @owner_stack . last , node , @file_path , @code_units_cache )
288
+ enhancement . on_call_node_enter ( node )
322
289
rescue StandardError => e
323
290
@indexing_errors << <<~MSG
324
291
Indexing error in #{ @file_path } with '#{ enhancement . class . name } ' on call node enter enhancement: #{ e . message }
@@ -339,7 +306,7 @@ def on_call_node_leave(node)
339
306
end
340
307
341
308
@enhancements . each do |enhancement |
342
- enhancement . on_call_node_leave ( @owner_stack . last , node , @file_path , @code_units_cache )
309
+ enhancement . on_call_node_leave ( node )
343
310
rescue StandardError => e
344
311
@indexing_errors << <<~MSG
345
312
Indexing error in #{ @file_path } with '#{ enhancement . class . name } ' on call node leave enhancement: #{ e . message }
@@ -464,6 +431,98 @@ def on_alias_method_node_enter(node)
464
431
)
465
432
end
466
433
434
+ sig do
435
+ params (
436
+ name : String ,
437
+ node_location : Prism ::Location ,
438
+ signatures : T ::Array [ Entry ::Signature ] ,
439
+ visibility : Entry ::Visibility ,
440
+ comments : T . nilable ( String ) ,
441
+ ) . void
442
+ end
443
+ def add_method ( name , node_location , signatures , visibility : Entry ::Visibility ::PUBLIC , comments : nil )
444
+ location = Location . from_prism_location ( node_location , @code_units_cache )
445
+
446
+ @index . add ( Entry ::Method . new (
447
+ name ,
448
+ @file_path ,
449
+ location ,
450
+ location ,
451
+ comments ,
452
+ signatures ,
453
+ visibility ,
454
+ @owner_stack . last ,
455
+ ) )
456
+ end
457
+
458
+ sig do
459
+ params (
460
+ name : String ,
461
+ full_location : Prism ::Location ,
462
+ name_location : Prism ::Location ,
463
+ comments : T . nilable ( String ) ,
464
+ ) . void
465
+ end
466
+ def add_module ( name , full_location , name_location , comments : nil )
467
+ location = Location . from_prism_location ( full_location , @code_units_cache )
468
+ name_loc = Location . from_prism_location ( name_location , @code_units_cache )
469
+
470
+ entry = Entry ::Module . new (
471
+ actual_nesting ( name ) ,
472
+ @file_path ,
473
+ location ,
474
+ name_loc ,
475
+ comments ,
476
+ )
477
+
478
+ advance_namespace_stack ( name , entry )
479
+ end
480
+
481
+ sig do
482
+ params (
483
+ name_or_nesting : T . any ( String , T ::Array [ String ] ) ,
484
+ full_location : Prism ::Location ,
485
+ name_location : Prism ::Location ,
486
+ parent_class_name : T . nilable ( String ) ,
487
+ comments : T . nilable ( String ) ,
488
+ ) . void
489
+ end
490
+ def add_class ( name_or_nesting , full_location , name_location , parent_class_name : nil , comments : nil )
491
+ nesting = name_or_nesting . is_a? ( Array ) ? name_or_nesting : actual_nesting ( name_or_nesting )
492
+ entry = Entry ::Class . new (
493
+ nesting ,
494
+ @file_path ,
495
+ Location . from_prism_location ( full_location , @code_units_cache ) ,
496
+ Location . from_prism_location ( name_location , @code_units_cache ) ,
497
+ comments ,
498
+ parent_class_name ,
499
+ )
500
+
501
+ advance_namespace_stack ( T . must ( nesting . last ) , entry )
502
+ end
503
+
504
+ sig { params ( block : T . proc . params ( index : Index , base : Entry ::Namespace ) . void ) . void }
505
+ def register_included_hook ( &block )
506
+ owner = @owner_stack . last
507
+ return unless owner
508
+
509
+ @index . register_included_hook ( owner . name ) do |index , base |
510
+ block . call ( index , base )
511
+ end
512
+ end
513
+
514
+ sig { void }
515
+ def pop_namespace_stack
516
+ @stack . pop
517
+ @owner_stack . pop
518
+ @visibility_stack . pop
519
+ end
520
+
521
+ sig { returns ( T . nilable ( Entry ::Namespace ) ) }
522
+ def current_owner
523
+ @owner_stack . last
524
+ end
525
+
467
526
private
468
527
469
528
sig do
@@ -921,5 +980,13 @@ def actual_nesting(name)
921
980
922
981
corrected_nesting
923
982
end
983
+
984
+ sig { params ( short_name : String , entry : Entry ::Namespace ) . void }
985
+ def advance_namespace_stack ( short_name , entry )
986
+ @visibility_stack . push ( Entry ::Visibility ::PUBLIC )
987
+ @owner_stack << entry
988
+ @index . add ( entry )
989
+ @stack << short_name
990
+ end
924
991
end
925
992
end
0 commit comments