seattlerb / ruby_parser

ruby_parser is a ruby parser written in pure ruby. It outputs s-expressions which can be manipulated and converted back to ruby via the ruby2ruby gem.
http://www.zenspider.com/projects/ruby_parser.html
476 stars 100 forks source link

Contents of removed begin..end gets wrong line number #290

Closed mvz closed 5 years ago

mvz commented 5 years ago
[7] pry(main)> ENV['VERBOSE'] = 'true'
=> "true"
[8] pry(main)> RubyParser.for_current_ruby.parse("begin\nfoo\nend\n")
=> s(:call, nil, :foo).line(1)

The call to foo is on line 2, but gets parsed as line 1.

zenspider commented 5 years ago

OK... philosophical question:

begin
  a
rescue
  b
rescue
  c
end

becomes:

s(:begin,
  s(:rescue,
    s(:call, nil, :a).line(2),
    s(:resbody, s(:array),
      s(:call, nil, :b)),
    s(:resbody, s(:array),
      s(:call, nil, :c))
   ).line(2)
 ).line(1)

(several line numbers omitted for readability)

When remove_begin gets called, it normally sets the inner thing (:rescue on line "2") to the line of the begin it is removing (line 1)... If I fix your bug above, I change the behavior below so that the rescue is now line 2 and not line 1.

Does this FEEL right? (yes, I realize that the rescue token is on line 3... that's a separate matter)

zenspider commented 5 years ago

I think yes, but I'm totally open to debating this...

In which case, this is fixed.

mvz commented 5 years ago

Does this FEEL right?

Yes, I think so.