2

Let's consider the following class:

class App
  def call
    # ...
  end

  private

  def foo
    # ...
  end

  def bar
    # ... 
  end
end

Is it possible to configure rubocop to 'complain' about Metrics/AbcSize for all methods when Assignment Branch Condition size is higher than 15 with an exception for call methods?

For call methods it should 'complain' only if Assignment Branch Condition size is higher than 30.

Thanks in advance.

Marian13
  • 7,740
  • 2
  • 47
  • 51

4 Answers4

1

RuboCop doesn't support this. You can't have different limits for different parts of the code.

Andy Waite
  • 10,785
  • 4
  • 33
  • 47
1

Andy Waite is correct that it's not baked in by default, but you can create your own custom cop for these if you want. I've never done it personally, but this article seems to explain it pretty well:

https://medium.com/@DmytroVasin/how-to-add-a-custom-cop-to-rubocop-47abf82f820a

If I were to take a whack at it, I would take a look at the source code for Metrics/ABcSize and mimic its behavior, except for when it encounters a method name #call.

The source code is here: https://github.com/rubocop-hq/rubocop/blob/master/lib/rubocop/cop/metrics/abc_size.rb

Matthew
  • 659
  • 1
  • 4
  • 19
1

How about this:

def a
  ...
end

# rubocop:disable Metrics/AbcSize
def call
   ...
end
# rubocop:enable Metrics/AbcSize

def b
  ...
end
Huy Vo
  • 2,418
  • 6
  • 22
  • 43
1

With the increased use of functional objects, I see this request come up more and more, and so I went and implemented an IgnoredMethods configuration option for the three method complexty cops:

  • Metrics/AbcSize
  • Metrics/CyclomaticComplexity
  • Metrics/PerceivedComplexity

The pull request is already merged, so with the release of the next version of RuboCop (likely to be 0.81.0), you will be able to do this:

# rubocop.yml

Metrics/AbcSize:
  IgnoredMethods:
    - 'call'

Note that this will disable the cop for all #call methods, including class methods, throughout the code base.

I know this is slightly different from what you were looking for, but for most intents and purposes, disabling the cop is as good as setting the maximum to 30. ;-)

Drenmi
  • 8,492
  • 4
  • 42
  • 51