Skip to content

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

Description

@dleavitt

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'

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions