【问题标题】:Why is rubocop failing only on my branch changes?为什么 rubocop 仅在我的分支更改上失败?
【发布时间】:2017-09-17 18:07:53
【问题描述】:

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

ClassLength:
    Max: 150 # Default 100

lib/utils/foo.rb 中有一个文件已经超过 200 行。如果我在 master 分支上运行 rubocop 检查,那么 rubocop 运行良好,没有任何错误。

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

Offenses:

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

【问题讨论】:

    标签: ruby-on-rails ruby git rubocop


    【解决方案1】:

    bbatsov/rubocop/lib/rubocop/cop/metrics/class_length.rb 调用check_code_length(node),后者调用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'”了解更多信息。

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 2014-01-01
      • 2020-04-12
      • 2012-01-10
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 2021-10-11
      • 2015-05-05
      相关资源
      最近更新 更多