Skip to content

Commit 09336d1

Browse files
committed
Impl to_raw_source_with_prism
1 parent abc43d0 commit 09336d1

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

lib/rspec/parameterized/core/composite_parser.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,34 @@ def self.to_raw_source_with_parser(obj)
3333
# @param obj [Object]
3434
# @return [String]
3535
def self.to_raw_source_with_prism(obj)
36-
# TODO: Impl
37-
raise "TODO: Impl"
36+
return obj.inspect unless obj.is_a?(Proc)
37+
38+
filename, linenum = obj.source_location
39+
ast = parse_with_prism(filename, linenum)
40+
41+
return "" unless ast
42+
43+
ast.source.source.strip
3844
end
3945
private_class_method :to_raw_source_with_prism
46+
47+
# @param filename [String]
48+
# @param linenum [Integer]
49+
#
50+
# @return [Prism::ParseResult,nil]
51+
def self.parse_with_prism(filename, linenum)
52+
buf = []
53+
File.open(filename, "rb").each_with_index do |line, index|
54+
next if index < linenum - 1
55+
buf << line
56+
57+
ret = Prism.parse(buf.join("\n"))
58+
return ret if ret.success?
59+
end
60+
61+
nil
62+
end
63+
private_class_method :parse_with_prism
4064
end
4165
end
4266
end

0 commit comments

Comments
 (0)