dalef
dalef

Reputation: 1941

How to ask Rubocop to check specific method/keyword usage?

I want to discourage the usage of unscoped in our codebase. Is there an existing linter in rubocop that allows me to specify keywords? If not, do I have to write a custom cop if I want to accomplish it?

Upvotes: 3

Views: 1311

Answers (1)

dalef
dalef

Reputation: 1941

My peer helped me. Looks like we will need to write a custom cop.

module RuboCop
  module Cop
    module Hired
      class Unscoped < Cop
        MSG = "Avoid using `unscoped`."

        def_node_matcher :unscoped?, <<-END
          (send _ :unscoped)
        END

        def on_send(node)
          return unless unscoped?(node)
          add_offense(node, :expression, MSG % node.source)
        end
      end
    end
  end
end

Drop it in a folder, like say lib/cops/ then add this to .rubocop.yml:

require:
- ./lib/cops/<whatever_you_called_the_file>.rb

see http://www.rubydoc.info/github/bbatsov/RuboCop/RuboCop/NodePattern

Upvotes: 3

Related Questions