Ruby #rubocop:禁用度量值/AbcSize

Ruby #rubocop:禁用度量值/AbcSize,ruby,chef-infra,rubocop,Ruby,Chef Infra,Rubocop,我真的被困在这一部分: 如果我禁用#rubocop:disable Metrics/AbcSize,那么我将得到以下错误: ruby -v : ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin14] rubocop -V 0.52.1 (using Parser 2.4.0.2, running on ruby 2.4.2 x86_64-darwin14) $ rubocop . Inspecting 7 files ...

我真的被困在这一部分:

如果我禁用
#rubocop:disable Metrics/AbcSize
,那么我将得到以下错误:

ruby -v : ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin14]

rubocop -V
0.52.1 (using Parser 2.4.0.2, running on ruby 2.4.2 x86_64-darwin14)


$ rubocop .
Inspecting 7 files
.....W.

Offenses:

recipes/default.rb:13:1: W: Lint/MissingCopEnableDirective: Re-enable Metrics/AbcSize cop with # rubocop:enable after disabling it.
# rubocop:disable Metrics/AbcSize
^

7 files inspected, 1 offense detected
如果我在脚本中启用了
rubocop
,则得到以下结果:

rubocop .
Inspecting 7 files
.....C.

Offenses:

recipes/default.rb:31:1: C: Metrics/AbcSize: Assignment Branch Condition size for check_tropo_versions is too high. [33.02/20]
def check_tropo_versions ...
^^^^^^^^^^^^^^^^^^^^^^^^

7 files inspected, 1 offense detected
我的脚本中有几行:

# rubocop:enable Metrics/AbcSize

require 'nokogiri'
Chef.event_handler do
  on :resource_updated do |resource, _action|
    if resource.declared_type.to_s == 'remote_file' && resource.cookbook_name == 'tropo-patch' && resource.recipe_name == 'default'
      puts "#{Time.now.strftime('%Y%m%d%H%M%S')},#{resource.path},#{resource.source[0]}"
      File.open('/var/log/tropo-patch.log', 'a') { |f| f.write("#{Time.now.strftime('%Y%m%d%H%M%S')},#{resource.path},#{resource.source[0]}\n") }
    end
  end
end
我无法从全局配置文件中禁用
rubocop
,但如果它可以解决问题,我也会尝试这样做:

Metrics/AbcSize:
  Description: >-
                 A calculated magnitude based on number of assignments,
                 branches, and conditions.
  Reference: 'http://c2.com/cgi/wiki?AbcMetric'
  Enabled: true

Rubocop基本上抱怨的是,它要求您在方法关闭后启用cop,因此cop可以对文件的其他方法生效。如果在文件的第一行禁用它,则该文件中的所有方法都将禁用它。为整个文件禁用COP不是一个好主意,因为禁用COP时应该明确且有意识

本例中的修复方法只是在方法之前禁用cop,然后在方法之后启用它

例如:

# rubocop:disable Metrics/AbcSize

require 'nokogiri'
Chef.event_handler do
  on :resource_updated do |resource, _action|
    if resource.declared_type.to_s == 'remote_file' && resource.cookbook_name == 'tropo-patch' && resource.recipe_name == 'default'
      puts "#{Time.now.strftime('%Y%m%d%H%M%S')},#{resource.path},#{resource.source[0]}"
      File.open('/var/log/tropo-patch.log', 'a') { |f| f.write("#{Time.now.strftime('%Y%m%d%H%M%S')},#{resource.path},#{resource.source[0]}\n") }
    end
  end
end

# rubocop:enable Metrics/AbcSize
此外,如果您需要禁用多个COP,您可以使用以下方法启用所有COP:

# rubocop:enable all
希望有帮助


编辑:在看过汤姆·洛德对你的问题的评论后,我注意到你没有发布违反警察规则的实际方法。尽管如此,如果您将
disable
enable
语句放在正确的位置,我提供的解决方案应该可以工作。此外,我支持Tom所说的,如果您向我们展示代码,我们可能会改进该方法,并且您不需要为该方法禁用cop。

使用
cookstyle
代替
rubocop
。对于linting cookbook代码,它有更好的默认设置。

您可以仅为此方法禁用cop,而不是在文件顶部添加
\rubocop:disable
,只需像这样标记您的违规者即可

def检查对流层版本\rubocop:禁用度量/AbcSize
# ...

错误显示:禁用后启用。您是否尝试过在复杂代码之前编写
#rubocop:disable Metrics/AbcSize
;然后还要在代码之后编写
#rubocop:enable Metrics/AbcSize
?(即,在该方法的正下方,在同一文件中。)此外,您还没有显示
check\u tropo\u versions
的源代码。与其只是禁用该方法的样式指南,不如在问题中显示它,我们将建议编写代码的更好方法;从而使其符合rubocop?…除了复杂的方法实际上是
检查对流层版本
;问题中没有说明其来源。