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

NoMethodError: undefined method `line' for nil:NilClass #306

Closed thrgamon closed 4 years ago

thrgamon commented 4 years ago

Hi,

I was using Ruby Critic the other day which calls flog which calls ruby_parser and noticed this error.

NoMethodError: undefined method `line' for nil:NilClass
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/ruby_parser-3.14.2/lib/ruby_parser_extras.rb:727:in `new_body'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/ruby_parser-3.14.2/lib/ruby26_parser.rb:3651:in `_reduce_15'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/2.6.0/racc/parser.rb:259:in `_racc_do_parse_c'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/2.6.0/racc/parser.rb:259:in `do_parse'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/ruby_parser-3.14.2/lib/ruby_parser_extras.rb:1298:in `block in process'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/2.6.0/timeout.rb:93:in `block in timeout'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/2.6.0/timeout.rb:33:in `block in catch'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/2.6.0/timeout.rb:33:in `catch'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/2.6.0/timeout.rb:33:in `catch'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/2.6.0/timeout.rb:108:in `timeout'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/ruby_parser-3.14.2/lib/ruby_parser_extras.rb:1286:in `process'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/ruby_parser-3.14.2/lib/ruby_parser.rb:36:in `block in process'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/ruby_parser-3.14.2/lib/ruby_parser.rb:33:in `each'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/ruby_parser-3.14.2/lib/ruby_parser.rb:33:in `process'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/flog-4.6.4/lib/flog.rb:224:in `flog_ruby!'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/flog-4.6.4/lib/flog.rb:194:in `flog_ruby'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/flog-4.6.4/lib/flog.rb:182:in `block in flog'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/flog-4.6.4/lib/flog.rb:177:in `each'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/flog-4.6.4/lib/flog.rb:177:in `flog'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/analysers/smells/flog.rb:35:in `add_smells_to'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/analysers/smells/flog.rb:21:in `block in run'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/core/analysed_modules_collection.rb:32:in `each'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/core/analysed_modules_collection.rb:32:in `each'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/analysers/smells/flog.rb:20:in `run'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/analysers_runner.rb:32:in `block in run'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/analysers_runner.rb:29:in `each'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/analysers_runner.rb:29:in `run'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/commands/default.rb:24:in `critique'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/commands/default.rb:19:in `execute'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/lib/rubycritic/cli/application.rb:21:in `execute'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/lib/ruby/gems/2.6.0/gems/rubycritic-4.4.1/bin/rubycritic:10:in `<top (required)>'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/bin/rubycritic:23:in `load'
  /Users/tom.gamon/.asdf/installs/ruby/2.6.2/bin/rubycritic:23:in `<top (required)>'

It seems to be stemming from this interesting block of code in our spec/support/factory_bot.rb

# frozen_string_literal: true
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods

  config.before(:suite) do
    FactoryBot.definition_file_paths = [Rails.root.join('spec', 'factories')]
    FactoryBot.find_definitions
    begin
      # FactoryBot::DatabaseCleaner.start
      # FactoryBot.lint
    ensure
      # FactoryBot::DatabaseCleaner.clean
    end
  end
end

Yay legacy codebases.

This seems like it is arguably valid ruby, so perhaps shouldn't cause an error? I looked to see if I could try and fix it, but I couldn't quite figure out how - happy to help out with a solution with some pointers.

Tom

presidentbeef commented 4 years ago

Issue here was the empty ensure block. I've opened a PR with a proposed fix.

thrgamon commented 4 years ago

Thank you!

zenspider commented 4 years ago

This should be fixed and released shortly.