Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline method call with block raises MethodSource::SourceNotFoundError #71

Open
dleavitt opened this issue Aug 19, 2021 · 0 comments · May be fixed by #75
Open

Multiline method call with block raises MethodSource::SourceNotFoundError #71

dleavitt opened this issue Aug 19, 2021 · 0 comments · May be fixed by #75

Comments

@dleavitt
Copy link

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'
ccutrer added a commit to ccutrer/method_source that referenced this issue Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant