method_source
method_source copied to clipboard
Multiline method call with block raises MethodSource::SourceNotFoundError
Related to #22
For blocks, source_location
reports the line where the block opens as its line number. If the block is part of a multiline expression, this can be partway through the expression.
Maybe expression_at
needs to start looking backwards (at line numbers prior to the provided one) if the first line has a syntax error?
class A
attr_reader :blk
def initialize(_stuff, &blk)
@blk = blk
end
end
a = A.new("a" => 1, "b" => 2) do
"Block!"
end
b = A.new("a" => 1,
"b" => 2) do # line 14
"Block!"
end
puts a.blk.source #=> works
puts b.blk.source_location #=> a.rb 14
puts b.blk.source #=> MethodSource::SourceNotFoundError
MethodSource::SourceNotFoundError: Could not parse source for #<Proc:0x00007f85ebcb34a0 ~/a.rb:14>: (eval):2: syntax error, unexpected =>, expecting end-of-input
"b" => 2) do
^~
from ~/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/method_source-1.0.0/lib/method_source.rb:29:in `rescue in source_helper'
Caused by SyntaxError: (eval):2: syntax error, unexpected =>, expecting end-of-input
"b" => 2) do
^~
from ~/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/method_source-1.0.0/lib/method_source/code_helpers.rb:71:in `eval'