我真的被这部分卡住了:
如果我禁用了# 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发布于 2018-01-05 01:19:19
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另外,如果您需要禁用多个cops,您可以使用以下命令来启用所有cops:
# rubocop:enable all希望这能有所帮助!
编辑:在看了汤姆·洛德对你的问题的评论后,我注意到你还没有发布违反cop规则的实际方法。不过,如果您将disable和enable语句放在正确的位置,那么我提供的解决方案应该是有效的。此外,我赞同Tom的说法,如果您向我们展示代码,我们可能会改进方法,并且您将不需要禁用该方法的cop。
发布于 2020-11-19 18:37:33
您只能禁用此方法的cop,而不是在文件的顶部添加# rubocop:disable,只需像这样标记您的违规者
def check_tropo_versions # rubocop:disable Metrics/AbcSize
# ...发布于 2018-01-05 02:05:38
在这里使用cookstyle代替rubocop。对于linting食谱代码,它有更好的默认设置。
https://stackoverflow.com/questions/48100213
复制相似问题