ruby-on-railsrubyrubocop

Uninitialized constant RuboCop::Cop::Base


I'm using gem 'rubocop', '~> 0.81.0', when I try to create a custom rubocop file like below:

lib/custom_cops/no_timecop.rb

return unless defined?(::RuboCop)

module CustomCops
  class NoTimecop < ::RuboCop::Cop::Base
    MSG = "`Timecop` の代わりにRails標準の `ActiveSupport::Testing::TimeHelpers` が使えませんか?".freeze

    def on_send(node)
      if node.source.include?("Timecop") && node.receiver.const_name == "Timecop"
        add_offense(node)
      end
    end
  end
end

I face with an error uninitialized constant RuboCop::Cop::Base

How can I fix this error?

I am not sure what I might be missing here, I'd appreciate some helpful feedback. Thank you!


Solution

  • You are using an old version of Rubocop, 0.81.0 was released more than three years ago and that version had indeed no Rubocop::Cop::Base class. Instead custom cop had to inherit from another class named Rubocop::Cop::Cop.

    That means, you have three options:

    1. Change your custom cop to inherit from Rubocop::Cop::Cop.
    2. Update the Rubocop dependency to at least 0.87.0, the version which introduced a Rubocop::Cop::Base class, but is still three years old.
    3. Or take the chance to update Rubocop to the latest version. At the time of writing, the maintainers suggest using gem 'rubocop', '~> 1.54', '>= 1.54.2'.

    I would, of course, recommend using the latest version, but it might require some effort, because some cops changed, new were added, and you will need to update your code to fix all newly found offenses.