vaibhavatul47
vaibhavatul47

Reputation: 2895

Why is rubocop failing only on my branch changes?

In our project rubocop.yml we have following check for number of lines in a class:

ClassLength:
    Max: 150 # Default 100

There's a file in lib/utils/foo.rb which is already more than 200 lines. If I run rubocop inspection on master branch then rubocop runs fine without any errors.

Now, in my feature/cool_feature branch I added 5 lines to this lib/utils/foo.rb class. Now if I run rubocop in my branch it fails with an following error:

Offenses:

lib/utils/foo.rb:1:1: C: Class has too many lines. [151/150]
  1. Why is this file failing rubocop test when it is already having more than 150 lines in master branch? (Note: rubocop runs without any error on master branch)
  2. Why the error message shows the class to have only 151 lines when this class has 214 lines?

Upvotes: 1

Views: 284

Answers (1)

VonC
VonC

Reputation: 1324218

bbatsov/rubocop/lib/rubocop/cop/metrics/class_length.rb calls check_code_length(node), which calls code_length(node)

target_line_numbers = body_line_numbers -
                      line_numbers_of_inner_nodes(node, :module, :class)

target_line_numbers.reduce(0) do |length, line_number|
  source_line = processed_source[line_number]
  next length if irrelevant_line(source_line)

That means it will only count relevant lines (non-empty, non-comment)

So check if the file in master actually has more than 150 non-empty, non-comment lines.

See "Rubocop error 'Class definition is too long ruby'" for more.

Upvotes: 1

Related Questions