为什么 rubocop 仅在我的分支更改时失败?

Why is rubocop failing only on my branch changes?

在我们的项目 rubocop.yml 中,我们对 class 中的行数进行了以下检查:

ClassLength:
    Max: 150 # Default 100

lib/utils/foo.rb中有一个文件已经超过200行。如果我 运行 rubocop 检查 master 分支,那么 rubocop 运行 没问题。

现在,在我的 feature/cool_feature 分支中,我向 lib/utils/foo.rb class 添加了 5 行。现在,如果我 运行 rubocop 在我的分支中,它会失败并出现以下错误:

Offenses:

lib/utils/foo.rb:1:1: C: Class has too many lines. [151/150]
  1. 为什么这个文件在 master 分支中已经有超过 150 行时却没有通过 rubocop 测试? (注意:rubocop 运行s 在 master 分支上没有任何错误)
  2. 为什么错误消息显示 class 只有 151 行,而 class 有 214 行?

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)

也就是说只会统计relevant lines(非空,不注释)

所以检查 master 中的文件是否真的有超过 150 行非空、非注释行。

有关更多信息,请参阅“Rubocop error 'Class definition is too long ruby'”。