Class: RuboCop::Formatter::DisabledConfigFormatter

Inherits:
BaseFormatter
  • Object
show all
Defined in:
lib/rubocop/formatter/disabled_config_formatter.rb

Overview

This formatter displays a YAML configuration file where all cops that detected any offenses are configured to not detect the offense.

Constant Summary collapse

HEADING =
['# This configuration was generated by `rubocop --auto-gen-config`',
 "# on #{Time.now} using RuboCop version #{Version.version}.",
 '# The point is for the user to remove these configuration records',
 '# one by one as the offenses are removed from the code base.',
 '# Note that changes in the inspected code, or installation of new',
 '# versions of RuboCop, may require this file to be generated again.']
.join("\n")
COPS =
Cop::Cop.all.group_by(&:cop_name)

Class Attribute Summary collapse

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#file_started, #initialize, #started

Constructor Details

This class inherits a constructor from RuboCop::Formatter::BaseFormatter

Class Attribute Details

.config_to_allow_offensesObject

Returns the value of attribute config_to_allow_offenses.



22
23
24
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 22

def config_to_allow_offenses
  @config_to_allow_offenses
end

Instance Method Details

#file_finished(_file, offenses) ⇒ Object



25
26
27
28
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 25

def file_finished(_file, offenses)
  @cops_with_offenses ||= Hash.new(0)
  offenses.each { |o| @cops_with_offenses[o.cop_name] += 1 }
end

#finished(_inspected_files) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 30

def finished(_inspected_files)
  output.puts HEADING

  # Syntax isn't a real cop and it can't be disabled.
  @cops_with_offenses.delete('Syntax')

  @cops_with_offenses.sort.each do |cop_name, offense_count|
    output.puts
    cfg = self.class.config_to_allow_offenses[cop_name]
    cfg ||= { 'Enabled' => false }
    output_cop_comments(output, cfg, cop_name, offense_count)
    output.puts "#{cop_name}:"
    cfg.each { |key, value| output.puts "  #{key}: #{value}" }
  end
  puts "Created #{output.path}."
  puts "Run `rubocop --config #{output.path}`, or"
  puts "add inherit_from: #{output.path} in a .rubocop.yml file."
end

#output_cop_comments(output, cfg, cop_name, offense_count) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 49

def output_cop_comments(output, cfg, cop_name, offense_count)
  output.puts "# Offense count: #{offense_count}"
  if COPS[cop_name] && COPS[cop_name].first.new.support_autocorrect?
    output.puts '# Cop supports --auto-correct.'
  end

  default_cfg = RuboCop::ConfigLoader.default_configuration[cop_name]
  return unless default_cfg

  params = default_cfg.keys - %w(Description StyleGuide Enabled) -
           cfg.keys
  return if params.empty?

  output.puts "# Configuration parameters: #{params.join(', ')}."
end