@@ -13,29 +13,32 @@ module TestUnit
13
13
# Parse a test file to extract the line ranges of all tests in both
14
14
# method-style (def test_foo) and declarative-style (test "foo" do)
15
15
module TestParser
16
+ @begins_to_ends = { }
16
17
# Helper to translate a method object into the path and line range where
17
18
# the method was defined.
18
19
def self . definition_for ( method )
19
20
filepath , start_line = method . source_location
20
- queue = [ Prism . parse_file ( filepath ) . value ]
21
+ @begins_to_ends [ filepath ] ||= ranges ( filepath )
22
+ return unless end_line = @begins_to_ends [ filepath ] [ start_line ]
23
+ [ filepath , start_line ..end_line ]
24
+ end
21
25
22
- while ( node = queue . shift )
23
- case node . type
24
- when :def_node
25
- if node . location . start_line == start_line
26
- return [ filepath , start_line ..node . location . end_line ]
26
+ private
27
+ def self . ranges ( filepath )
28
+ queue = [ Prism . parse_file ( filepath ) . value ]
29
+ begins_to_ends = { }
30
+ while ( node = queue . shift )
31
+ case node . type
32
+ when :def_node
33
+ begins_to_ends [ node . location . start_line ] = node . location . end_line
34
+ when :call_node
35
+ begins_to_ends [ node . location . start_line ] = node . location . end_line
27
36
end
28
- when :call_node
29
- if node . location . start_line == start_line
30
- return [ filepath , start_line ..node . location . end_line ]
31
- end
32
- end
33
37
34
- queue . concat ( node . compact_child_nodes )
38
+ queue . concat ( node . compact_child_nodes )
39
+ end
40
+ begins_to_ends
35
41
end
36
-
37
- nil
38
- end
39
42
end
40
43
end
41
44
end
0 commit comments