Skip to content

Commit 1f9b696

Browse files
authored
Merge pull request rails#53936 from jsharpify/jsharpify/prism-parsing
railties/test_parser: Cache prism work done in definition_for
2 parents c51aaaa + fbb37c0 commit 1f9b696

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

railties/lib/rails/test_unit/test_parser.rb

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,32 @@ module TestUnit
1313
# Parse a test file to extract the line ranges of all tests in both
1414
# method-style (def test_foo) and declarative-style (test "foo" do)
1515
module TestParser
16+
@begins_to_ends = {}
1617
# Helper to translate a method object into the path and line range where
1718
# the method was defined.
1819
def self.definition_for(method)
1920
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
2125

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
2736
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
3337

34-
queue.concat(node.compact_child_nodes)
38+
queue.concat(node.compact_child_nodes)
39+
end
40+
begins_to_ends
3541
end
36-
37-
nil
38-
end
3942
end
4043
end
4144
end

0 commit comments

Comments
 (0)