2
2
# frozen_string_literal: true
3
3
4
4
module RubyIndexer
5
- class IndexVisitor < YARP ::Visitor
5
+ class IndexVisitor < Prism ::Visitor
6
6
extend T ::Sig
7
7
8
- sig { params ( index : Index , parse_result : YARP ::ParseResult , file_path : String ) . void }
8
+ sig { params ( index : Index , parse_result : Prism ::ParseResult , file_path : String ) . void }
9
9
def initialize ( index , parse_result , file_path )
10
10
@index = index
11
11
@file_path = file_path
@@ -14,125 +14,125 @@ def initialize(index, parse_result, file_path)
14
14
parse_result . comments . to_h do |c |
15
15
[ c . location . start_line , c ]
16
16
end ,
17
- T ::Hash [ Integer , YARP ::Comment ] ,
17
+ T ::Hash [ Integer , Prism ::Comment ] ,
18
18
)
19
19
20
20
super ( )
21
21
end
22
22
23
- sig { override . params ( node : YARP ::ClassNode ) . void }
23
+ sig { override . params ( node : Prism ::ClassNode ) . void }
24
24
def visit_class_node ( node )
25
25
add_index_entry ( node , Index ::Entry ::Class )
26
26
end
27
27
28
- sig { override . params ( node : YARP ::ModuleNode ) . void }
28
+ sig { override . params ( node : Prism ::ModuleNode ) . void }
29
29
def visit_module_node ( node )
30
30
add_index_entry ( node , Index ::Entry ::Module )
31
31
end
32
32
33
- sig { override . params ( node : YARP ::MultiWriteNode ) . void }
33
+ sig { override . params ( node : Prism ::MultiWriteNode ) . void }
34
34
def visit_multi_write_node ( node )
35
35
value = node . value
36
- values = value . is_a? ( YARP ::ArrayNode ) && value . opening_loc ? value . elements : [ ]
36
+ values = value . is_a? ( Prism ::ArrayNode ) && value . opening_loc ? value . elements : [ ]
37
37
38
38
node . targets . each_with_index do |target , i |
39
39
current_value = values [ i ]
40
40
# The moment we find a splat on the right hand side of the assignment, we can no longer figure out which value
41
41
# gets assigned to what
42
- values . clear if current_value . is_a? ( YARP ::SplatNode )
42
+ values . clear if current_value . is_a? ( Prism ::SplatNode )
43
43
44
44
case target
45
- when YARP ::ConstantTargetNode
45
+ when Prism ::ConstantTargetNode
46
46
add_constant ( target , fully_qualify_name ( target . name . to_s ) , current_value )
47
- when YARP ::ConstantPathTargetNode
47
+ when Prism ::ConstantPathTargetNode
48
48
add_constant ( target , fully_qualify_name ( target . slice ) , current_value )
49
49
end
50
50
end
51
51
end
52
52
53
- sig { override . params ( node : YARP ::ConstantPathWriteNode ) . void }
53
+ sig { override . params ( node : Prism ::ConstantPathWriteNode ) . void }
54
54
def visit_constant_path_write_node ( node )
55
55
# ignore variable constants like `var::FOO` or `self.class::FOO`
56
56
target = node . target
57
- return unless target . parent . nil? || target . parent . is_a? ( YARP ::ConstantReadNode )
57
+ return unless target . parent . nil? || target . parent . is_a? ( Prism ::ConstantReadNode )
58
58
59
59
name = fully_qualify_name ( target . location . slice )
60
60
add_constant ( node , name )
61
61
end
62
62
63
- sig { override . params ( node : YARP ::ConstantPathOrWriteNode ) . void }
63
+ sig { override . params ( node : Prism ::ConstantPathOrWriteNode ) . void }
64
64
def visit_constant_path_or_write_node ( node )
65
65
# ignore variable constants like `var::FOO` or `self.class::FOO`
66
66
target = node . target
67
- return unless target . parent . nil? || target . parent . is_a? ( YARP ::ConstantReadNode )
67
+ return unless target . parent . nil? || target . parent . is_a? ( Prism ::ConstantReadNode )
68
68
69
69
name = fully_qualify_name ( target . location . slice )
70
70
add_constant ( node , name )
71
71
end
72
72
73
- sig { override . params ( node : YARP ::ConstantPathOperatorWriteNode ) . void }
73
+ sig { override . params ( node : Prism ::ConstantPathOperatorWriteNode ) . void }
74
74
def visit_constant_path_operator_write_node ( node )
75
75
# ignore variable constants like `var::FOO` or `self.class::FOO`
76
76
target = node . target
77
- return unless target . parent . nil? || target . parent . is_a? ( YARP ::ConstantReadNode )
77
+ return unless target . parent . nil? || target . parent . is_a? ( Prism ::ConstantReadNode )
78
78
79
79
name = fully_qualify_name ( target . location . slice )
80
80
add_constant ( node , name )
81
81
end
82
82
83
- sig { override . params ( node : YARP ::ConstantPathAndWriteNode ) . void }
83
+ sig { override . params ( node : Prism ::ConstantPathAndWriteNode ) . void }
84
84
def visit_constant_path_and_write_node ( node )
85
85
# ignore variable constants like `var::FOO` or `self.class::FOO`
86
86
target = node . target
87
- return unless target . parent . nil? || target . parent . is_a? ( YARP ::ConstantReadNode )
87
+ return unless target . parent . nil? || target . parent . is_a? ( Prism ::ConstantReadNode )
88
88
89
89
name = fully_qualify_name ( target . location . slice )
90
90
add_constant ( node , name )
91
91
end
92
92
93
- sig { override . params ( node : YARP ::ConstantWriteNode ) . void }
93
+ sig { override . params ( node : Prism ::ConstantWriteNode ) . void }
94
94
def visit_constant_write_node ( node )
95
95
name = fully_qualify_name ( node . name . to_s )
96
96
add_constant ( node , name )
97
97
end
98
98
99
- sig { override . params ( node : YARP ::ConstantOrWriteNode ) . void }
99
+ sig { override . params ( node : Prism ::ConstantOrWriteNode ) . void }
100
100
def visit_constant_or_write_node ( node )
101
101
name = fully_qualify_name ( node . name . to_s )
102
102
add_constant ( node , name )
103
103
end
104
104
105
- sig { override . params ( node : YARP ::ConstantAndWriteNode ) . void }
105
+ sig { override . params ( node : Prism ::ConstantAndWriteNode ) . void }
106
106
def visit_constant_and_write_node ( node )
107
107
name = fully_qualify_name ( node . name . to_s )
108
108
add_constant ( node , name )
109
109
end
110
110
111
- sig { override . params ( node : YARP ::ConstantOperatorWriteNode ) . void }
111
+ sig { override . params ( node : Prism ::ConstantOperatorWriteNode ) . void }
112
112
def visit_constant_operator_write_node ( node )
113
113
name = fully_qualify_name ( node . name . to_s )
114
114
add_constant ( node , name )
115
115
end
116
116
117
- sig { override . params ( node : YARP ::CallNode ) . void }
117
+ sig { override . params ( node : Prism ::CallNode ) . void }
118
118
def visit_call_node ( node )
119
119
message = node . message
120
120
handle_private_constant ( node ) if message == "private_constant"
121
121
end
122
122
123
123
private
124
124
125
- sig { params ( node : YARP ::CallNode ) . void }
125
+ sig { params ( node : Prism ::CallNode ) . void }
126
126
def handle_private_constant ( node )
127
127
arguments = node . arguments &.arguments
128
128
return unless arguments
129
129
130
130
first_argument = arguments . first
131
131
132
132
name = case first_argument
133
- when YARP ::StringNode
133
+ when Prism ::StringNode
134
134
first_argument . content
135
- when YARP ::SymbolNode
135
+ when Prism ::SymbolNode
136
136
first_argument . value
137
137
end
138
138
@@ -150,37 +150,37 @@ def handle_private_constant(node)
150
150
sig do
151
151
params (
152
152
node : T . any (
153
- YARP ::ConstantWriteNode ,
154
- YARP ::ConstantOrWriteNode ,
155
- YARP ::ConstantAndWriteNode ,
156
- YARP ::ConstantOperatorWriteNode ,
157
- YARP ::ConstantPathWriteNode ,
158
- YARP ::ConstantPathOrWriteNode ,
159
- YARP ::ConstantPathOperatorWriteNode ,
160
- YARP ::ConstantPathAndWriteNode ,
161
- YARP ::ConstantTargetNode ,
162
- YARP ::ConstantPathTargetNode ,
153
+ Prism ::ConstantWriteNode ,
154
+ Prism ::ConstantOrWriteNode ,
155
+ Prism ::ConstantAndWriteNode ,
156
+ Prism ::ConstantOperatorWriteNode ,
157
+ Prism ::ConstantPathWriteNode ,
158
+ Prism ::ConstantPathOrWriteNode ,
159
+ Prism ::ConstantPathOperatorWriteNode ,
160
+ Prism ::ConstantPathAndWriteNode ,
161
+ Prism ::ConstantTargetNode ,
162
+ Prism ::ConstantPathTargetNode ,
163
163
) ,
164
164
name : String ,
165
- value : T . nilable ( YARP ::Node ) ,
165
+ value : T . nilable ( Prism ::Node ) ,
166
166
) . void
167
167
end
168
168
def add_constant ( node , name , value = nil )
169
- value = node . value unless node . is_a? ( YARP ::ConstantTargetNode ) || node . is_a? ( YARP ::ConstantPathTargetNode )
169
+ value = node . value unless node . is_a? ( Prism ::ConstantTargetNode ) || node . is_a? ( Prism ::ConstantPathTargetNode )
170
170
comments = collect_comments ( node )
171
171
172
172
@index << case value
173
- when YARP ::ConstantReadNode , YARP ::ConstantPathNode
173
+ when Prism ::ConstantReadNode , Prism ::ConstantPathNode
174
174
Index ::Entry ::UnresolvedAlias . new ( value . slice , @stack . dup , name , @file_path , node . location , comments )
175
- when YARP ::ConstantWriteNode , YARP ::ConstantAndWriteNode , YARP ::ConstantOrWriteNode ,
176
- YARP ::ConstantOperatorWriteNode
175
+ when Prism ::ConstantWriteNode , Prism ::ConstantAndWriteNode , Prism ::ConstantOrWriteNode ,
176
+ Prism ::ConstantOperatorWriteNode
177
177
178
178
# If the right hand side is another constant assignment, we need to visit it because that constant has to be
179
179
# indexed too
180
180
visit ( value )
181
181
Index ::Entry ::UnresolvedAlias . new ( value . name . to_s , @stack . dup , name , @file_path , node . location , comments )
182
- when YARP ::ConstantPathWriteNode , YARP ::ConstantPathOrWriteNode , YARP ::ConstantPathOperatorWriteNode ,
183
- YARP ::ConstantPathAndWriteNode
182
+ when Prism ::ConstantPathWriteNode , Prism ::ConstantPathOrWriteNode , Prism ::ConstantPathOperatorWriteNode ,
183
+ Prism ::ConstantPathAndWriteNode
184
184
185
185
visit ( value )
186
186
Index ::Entry ::UnresolvedAlias . new ( value . target . slice , @stack . dup , name , @file_path , node . location , comments )
@@ -189,7 +189,7 @@ def add_constant(node, name, value = nil)
189
189
end
190
190
end
191
191
192
- sig { params ( node : T . any ( YARP ::ClassNode , YARP ::ModuleNode ) , klass : T . class_of ( Index ::Entry ) ) . void }
192
+ sig { params ( node : T . any ( Prism ::ClassNode , Prism ::ModuleNode ) , klass : T . class_of ( Index ::Entry ) ) . void }
193
193
def add_index_entry ( node , klass )
194
194
name = node . constant_path . location . slice
195
195
@@ -204,7 +204,7 @@ def add_index_entry(node, klass)
204
204
@stack . pop
205
205
end
206
206
207
- sig { params ( node : YARP ::Node ) . returns ( T ::Array [ String ] ) }
207
+ sig { params ( node : Prism ::Node ) . returns ( T ::Array [ String ] ) }
208
208
def collect_comments ( node )
209
209
comments = [ ]
210
210
0 commit comments