@@ -41,8 +41,20 @@ def on_class_node_enter(node)
41
41
42
42
#: (Prism::ClassNode) -> void
43
43
def on_class_node_leave ( node ) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
44
+ @spec_group_id_stack . pop
44
45
super
46
+ end
47
+
48
+ #: (Prism::ModuleNode) -> void
49
+ def on_module_node_enter ( node ) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
50
+ @spec_group_id_stack << nil
51
+ super
52
+ end
53
+
54
+ #: (Prism::ModuleNode) -> void
55
+ def on_module_node_leave ( node ) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
45
56
@spec_group_id_stack . pop
57
+ super
46
58
end
47
59
48
60
#: (Prism::CallNode) -> void
@@ -61,6 +73,12 @@ def on_call_node_enter(node)
61
73
def on_call_node_leave ( node )
62
74
return unless node . name == :describe && !node . receiver
63
75
76
+ current_group = @spec_group_id_stack . last
77
+ return unless current_group . is_a? ( DescribeGroup )
78
+
79
+ description = extract_description ( node )
80
+ return unless description && current_group . id . end_with? ( description )
81
+
64
82
@spec_group_id_stack . pop
65
83
end
66
84
@@ -76,6 +94,8 @@ def handle_describe(node)
76
94
return unless description
77
95
78
96
parent = latest_group
97
+ return unless parent
98
+
79
99
id = case parent
80
100
when Requests ::Support ::TestItem
81
101
"#{ parent . id } ::#{ description } "
@@ -135,26 +155,50 @@ def extract_description(node)
135
155
end
136
156
end
137
157
138
- #: -> (Requests::Support::TestItem | ResponseBuilders::TestCollection)
158
+ #: -> (Requests::Support::TestItem | ResponseBuilders::TestCollection)?
139
159
def latest_group
160
+ # If we haven't found anything yet, then return the response builder
140
161
return @response_builder if @spec_group_id_stack . compact . empty?
162
+ # If we found something that isn't a group last, then we're inside a random module or class, but not a spec
163
+ # group
164
+ return unless @spec_group_id_stack . last
141
165
142
- first_class_index = @spec_group_id_stack . rindex { |i | i . is_a? ( ClassGroup ) } || 0
143
- first_class = @spec_group_id_stack [ 0 ] #: as !nil
144
- item = @response_builder [ first_class . id ] #: as !nil
166
+ # Specs using at least one class as a group require special handling
167
+ closest_class_index = @spec_group_id_stack . rindex { |i | i . is_a? ( ClassGroup ) }
145
168
146
- # Descend into child items from the beginning all the way to the latest class group, ignoring describes
147
- @spec_group_id_stack [ 1 .. first_class_index ] #: as !nil
148
- . each do | group |
149
- next unless group . is_a? ( ClassGroup )
169
+ if closest_class_index
170
+ first_class_index = @spec_group_id_stack . index { | i | i . is_a? ( ClassGroup ) } #: as !nil
171
+ first_class = @spec_group_id_stack [ first_class_index ] #: as !nil
172
+ item = @response_builder [ first_class . id ] #: as !nil
150
173
151
- item = item [ group . id ] #: as !nil
174
+ # Descend into child items from the beginning all the way to the latest class group, ignoring describes
175
+ @spec_group_id_stack [ first_class_index + 1 ..closest_class_index ] #: as !nil
176
+ . each do |group |
177
+ next unless group . is_a? ( ClassGroup )
178
+
179
+ item = item [ group . id ] #: as !nil
180
+ end
181
+
182
+ # From the class forward, we must take describes into account
183
+ @spec_group_id_stack [ closest_class_index + 1 ..] #: as !nil
184
+ . each do |group |
185
+ next unless group
186
+
187
+ item = item [ group . id ] #: as !nil
188
+ end
189
+
190
+ return item
152
191
end
153
192
154
- # From the class forward, we must take describes into account
155
- @spec_group_id_stack [ first_class_index + 1 ..] #: as !nil
193
+ # Specs only using describes
194
+ first_group = @spec_group_id_stack . find { |i | i . is_a? ( DescribeGroup ) }
195
+ return unless first_group
196
+
197
+ item = @response_builder [ first_group . id ] #: as !nil
198
+
199
+ @spec_group_id_stack [ 1 ..] #: as !nil
156
200
. each do |group |
157
- next unless group
201
+ next unless group . is_a? ( DescribeGroup )
158
202
159
203
item = item [ group . id ] #: as !nil
160
204
end
0 commit comments