Doesn't work direct from Ruby CLI
Fryguy opened this issue · 2 comments
As a test, I tried the following
foo.rb:
class Foo
3.times |x|
puts x
end
end
And none of the following work
$ ruby -r dead_end foo.rb
foo.rb:6: syntax error, unexpected end, expecting end-of-input
$ ruby -r dead_end -c foo.rb
foo.rb:6: syntax error, unexpected end, expecting end-of-input
I assume this is probably a Ruby ordering thing in that it parses your script before applying the requires, but it's very surprising.
If instead, I do this, it works fine.
bar.rb:
require "foo"
Not sure if there's an actual fix that can be done if this is a Ruby core issue, but wanted to bring to your attention, particularly since one of your goals is eventually getting this into ruby core in a similar way to did_you_mean.
Thanks for trying it out. It does work if you just require the file:
$ ruby -r dead_end -r ./foo.rb -e puts 'hi'
Run `$ dead_end /private/tmp/foo.rb` for more options
DeadEnd: Unmatched `end` detected
This code has an unmatched `end`. Ensure that all `end` lines
in your code have a matching syntax keyword (`def`, `do`, etc.)
and that you don't have any extra `end` lines.
file: /private/tmp/foo.rb
simplified:
1 class Foo
❯ 2 3.times |x|
❯ 4 end
5 end
The reason has to do with how I'm hooking into Ruby to know when a syntax error has occurred. I'm monkeypatching require
, load
, and require_relative
https://github.com/zombocom/dead_end/blob/a598e7617e06b6016277b90944c5d2bd51ba70d1/lib/dead_end/auto.rb#L16-L20
It looks like running Ruby code directly from $ ruby
doesn't use one of these. There is a CLI that ships with this gem though:
$ dead_end foo.rb
DeadEnd: Unmatched `end` detected
This code has an unmatched `end`. Ensure that all `end` lines
in your code have a matching syntax keyword (`def`, `do`, etc.)
and that you don't have any extra `end` lines.
file: /private/tmp/foo.rb
simplified:
1 class Foo
❯ 2 3.times |x|
❯ 4 end
5 end
Not sure if there's an actual fix that can be done if this is a Ruby core issue, but wanted to bring to your attention, particularly since one of your goals is eventually getting this into ruby core in a similar way to did_you_mean
100% thanks! Every little bit of behavior that's unexpected is good to know about.
Works with Ruby 3.2 now. Closing