rubyrubocop

How to configure Rubocop to skip counting log lines - Metrics/MethodLength AllowedPatterns/AllowedMethods


Running RuboCop results in the following error

$ rubocop
Running RuboCop...

Offenses:

C: Metrics/MethodLength: Method has too many lines. [25/10] (https://rubystyle.guide#short-methods)
def example_method ...
^^^^^^^^^^^^^^^^^^

Where the example_method is

def example_method
  Log.debug "Debug message 1"
  Log.debug "Debug message 2"
  Log.debug "Debug message 3"
  Log.debug "Debug message 4"
  Log.debug "Debug message 5"
  Log.debug "Debug message 6"
  Log.debug "Debug message 7"
  Log.debug "Debug message 8"
  Log.debug "Debug message 9"
  Log.debug "Debug message 10"
  Log.debug "Debug message 11"
  Log.debug "Debug message 12"
  Log.debug "Debug message 13"
  Log.debug "Debug message 14"
  Log.debug "Debug message 15"
  Log.debug "Debug message 16"
  Log.debug "Debug message 17"
  Log.debug "Debug message 18"
  Log.debug "Debug message 19"
  Log.debug "Debug message 20"
  Log.debug "Debug message 21"
  Log.debug "Debug message 22"
  Log.debug "Debug message 23"
  Log.debug "Debug message 24"
  Log.debug "Debug message 25"
end

Since I want to allow Log messages, I tried to tweak RuboCop configuration as follows (after looking at ref#1, ref#2, ref#3)

# .rubocop.yml
Metrics/MethodLength:
  AllowedPatterns:
    - '^\s*Log\.(debug|info|warn|error|fatal).*'

But when executing RuboCop the error output is the same (see the beginning of the question). How could I configure RuboCop Metrics/MethodLength to allow patterns/methods to not count calls for Log?


Solution

  • The AllowedPatterns option on the cop matches method names that should be allowed. This is methods that should be allowed to be longer than the limit, and NOT method calls that shouldn't count towards the limit.

    There is an option to count or not count comments, but nothing about other arbitrary lines.

    If you think this is something that Rubocop should support, open an issue or a PR in their repo, or fork it.