如何在 运行 rubocop 上禁用“没有为 'COP' 消息给出的部门警告”

How to disable 'warning no department given for 'COP' message on running rubocop

在我的 .rubocop.yml 中有一些用于禁用某些风格警察的配置。

Documentation:
  Enabled: false
ClassAndModuleChildren:
  Enabled: false
LineLength:
  Max: 120
GuardClause:
  Enabled: false
IfUnlessModifier: 
  Enabled: false

当 运行 rubocop 在终端中它工作正常并像往常一样禁用不需要的样式警察和棉绒但每次它运行时我都会收到所有禁用警察的警告错误:

Warning: no department given for Documentation.

有没有办法禁用警告消息?

合格的警察名字是Department/CopName。例如Style/Documentation是合格的,Documentation是不合格的。

documentation表示:

Qualifying cop name with its type, e.g., Style, is recommended, but not necessary as long as the cop name is unique across all types.

但他们对不合格的名称显示警告。发生这种情况 here:

# RuboCop::Cop::Registry
def qualified_cop_name(name, path, shall_warn = true)
  badge = Badge.parse(name)
  if shall_warn && department_missing?(badge, name)
    print_warning(name, path)
  end
  return name if registered?(badge)

  potential_badges = qualify_badge(badge)

  case potential_badges.size
  when 0 then name # No namespace found. Deal with it later in caller.
  when 1 then resolve_badge(badge, potential_badges.first, path)
  else raise AmbiguousCopName.new(badge, path, potential_badges)
  end
end

shall_warn 只有在使用 --auto-correct 选项时才 false。目前无法禁用它。

消除警告的唯一方法是在您的配置中包含每个警察的部门,例如:

Style/Documentation:
  Enabled: false
Style/ClassAndModuleChildren:
  Enabled: false
Metrics/LineLength:
  Max: 120
Style/GuardClause:
  Enabled: false
Style/IfUnlessModifier: 
  Enabled: false