seattlerb/ruby_parser

Rescue blocks don't need a begin statement in define_method since Ruby 2.5

Closed this issue · 3 comments

Hi! 👋First of all, thank you so much for all the work on this gem!

I maintain the https://github.com/DamirSvrtan/fasterer gem and a user has recently seen an issue with fasterer's parsing (which under the hood uses ruby_parser).

It seems that running this code is valid since ruby 2.5:

define_method :bar  do
  puts "bar"
  rescue
end

bar

In ruby 2.4 running that code would invoke a syntax error:

test.rb:3: syntax error, unexpected keyword_rescue, expecting keyword_end
  rescue
        ^

However, it seems that the ruby_parser needs to be updated to have parity with this behavior, because running that syntax on ruby 2.5 raises an error:

test.rb - Racc::ParseError - (string):3 :: parse error on value ["rescue", 3] (kRESCUE)

cc @EdwardDiehl @graywolf for visibility.

It seems my fix in #264 does not handle method calls with arguments and no parentheses.

IIRC, method calls w/ args, a block, and no parens are called commands.

Fixed:

s(:block,
 s(:iter,
  s(:call, nil, :define_method, s(:lit, :bar)),
  0,
  s(:rescue,
   s(:call, nil, :puts, s(:str, "bar")),
   s(:resbody, s(:array), nil))),
 s(:call, nil, :bar))