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

Inconsistent line numbers for Method.source_location #3421

Open
catlee opened this issue Jan 17, 2025 · 3 comments
Open

Inconsistent line numbers for Method.source_location #3421

catlee opened this issue Jan 17, 2025 · 3 comments
Labels

Comments

@catlee
Copy link

catlee commented Jan 17, 2025

In Ruby 3.4, the following code returns different results depending on which parser is used:

# test.rb
class Foo
  define_method "foo" \
  "bar" do

  end
end

p Foo.instance_method(:foobar).source_location

With ruby --parser=parse.y test.rb I get ["test.rb", 2]
With ruby --parser=prism test.rb I get ["test.rb", 3]
I'm using ruby 3.4.0preview2 (2024-10-07 master 32c733f57b) +PRISM [arm64-darwin23]

@paracycle
Copy link
Collaborator

Same behaviour on Ruby 3.4.1 as well:

ruby -v --parser=prism test.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin23]
["test.rb", 3]ruby -v --parser=parse.y test.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) [arm64-darwin23]
["test.rb", 2]

@kddnewton
Copy link
Collaborator

I think parse.y is wrong here, the block clearly starts on line 3. I will bring this up for discussion.

@catlee
Copy link
Author

catlee commented Jan 21, 2025

I discovered this in the context of fixing a bug in a test plugin which allows you to run tests by line number.

e.g. in code like this:

1 class FooTest < TestCase
2   test "really long" \
3    "test name" do
4    assert ...
5   end
6 end

I think it's reasonable for a developer to want to be able to run the test which starts on line 2.

Rails uses method.source_location and node.start_line to determine which line numbers correspond to test methods: https://github.com/kddnewton/rails/blob/main/railties/lib/rails/test_unit/test_parser.rb#L19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants