【发布时间】: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