【问题标题】:# rubocop:disable Metrics/AbcSize# rubocop:disable Metrics/AbcSize
【发布时间】:2018-06-14 11:24:54
【问题描述】:

我真的被困在这部分了:

如果我禁用# 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:disable Metrics/AbcSize之前复杂的代码; 然后还在代码后面写# rubocop:enable Metrics/AbcSize? (即直接在该方法的下方,在同一个文件中。)
  • 另外,您还没有显示check_tropo_versions 的源代码。而不是仅仅禁用该方法的样式指南,也许您可​​以在您的问题中展示它,我们会建议一种更好的编写代码的方法;从而使其符合 rubocop 标准?

标签: ruby chef-infra rubocop


【解决方案1】:

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

在您的情况下,修复只是简单地在您的方法之前禁用 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

此外,如果您需要禁用多个警察,您可以使用以下命令启用所有警察:

# rubocop:enable all

希望有帮助!

编辑:在查看 Tom Lord 对您的问题的评论后,我注意到您尚未发布违反警察规则的实际方法。不过,如果您将 disableenable 语句放在正确的位置,我提供的解决方案应该可以工作。此外,我赞同 Tom 所说的话,如果您向我们展示代码,我们可能会改进该方法,并且您不需要禁用该方法的 cop。

【讨论】:

  • ...除了复杂的方法其实是check_tropo_versions;问题中未显示其来源。
【解决方案2】:

您只能为此方法禁用警察,而不是在文件顶部添加# rubocop:disable,只需像这样标记您的罪犯

def check_tropo_versions # rubocop:disable Metrics/AbcSize
# ...

【讨论】:

    【解决方案3】:

    在此处使用cookstyle 代替rubocop。它为 linting cookbook 代码提供了更好的默认值。

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 2020-05-11
      • 1970-01-01
      • 2019-10-17
      • 2016-04-26
      • 2022-12-27
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多