How can I develop custom cop and ensure it passes `zeitwerk:check`

67 Views Asked by At

According to this site https://docs.rubocop.org/rubocop/development.html#implementation, it says cop code will be like this.

module RuboCop
  module Cop
    module Style
      class SimplifyNotEmptyWithAny < Base
        ...
      end
    end
  end
end

And I followed it in Project Root/lib/rubocop/cop/style/simplify_not_empty_with_any.rb.

But when I command rails zeitwerk:check, it did not work.

Hold on, I am eager loading the application.
bin/rails aborted!
NameError: uninitialized constant RuboCop::Cop::Style::Base (NameError)

      class SimplifyNotEmptyWithAny < Base
                                      ^^^^
Did you mean?  Base64

How can I solve it?

Do you have any ideas?

I tried reuiqre 'rubocop' or require 'rubocop/cop/base' but it did not solve.

[UPDATE] I tried it class SimplifyNotEmptyWithAny < RuboCop::Cop::Base but got same error.

2

There are 2 best solutions below

1
On

I think you should try with this one,

module RuboCop
  module Cop
    module Style
      class SimplifyNotEmptyWithAny < RuboCop::Cop::Base
      ...
      end
    end
  end
end
0
On

I found this answer.

  1. File path should be like this lib/rubo_cop/cop/style/simplify_not_empty_with_any.rb (because of pascal case of RuboCop)
  2. It need the line require 'rubocop' at the beginning.
    • but if you have 2 files of custom cop, no need to write second file. So I recommend that would be better to write in config/environments/test.rb

FYI: https://github.com/rubocop/rubocop/discussions/12517