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',
 '# `%s`',
 "# 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

#options, #output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#started

Constructor Details

#initialize(output, options = {}) ⇒ DisabledConfigFormatter

Returns a new instance of DisabledConfigFormatter.



28
29
30
31
32
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 28

def initialize(output, options = {})
  super
  @cops_with_offenses ||= Hash.new(0)
  @files_with_offenses ||= {}
end

Class Attribute Details

.config_to_allow_offensesObject

Returns the value of attribute config_to_allow_offenses.



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

def config_to_allow_offenses
  @config_to_allow_offenses
end

.detected_stylesObject

Returns the value of attribute detected_styles.



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

def detected_styles
  @detected_styles
end

Instance Method Details

#commandObject



63
64
65
66
67
68
69
70
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 63

def command
  command = 'rubocop --auto-gen-config'
  if @exclude_limit_option
    command += format(' --exclude-limit %d', @exclude_limit_option.to_i)
  end

  command
end

#file_finished(file, offenses) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 42

def file_finished(file, offenses)
  offenses.each do |o|
    @cops_with_offenses[o.cop_name] += 1
    @files_with_offenses[o.cop_name] ||= []
    @files_with_offenses[o.cop_name] << file
  end
end

#file_started(_file, _file_info) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 34

def file_started(_file, _file_info)
  @exclude_limit_option = @options[:exclude_limit]
  @exclude_limit = (
    @exclude_limit_option ||
    RuboCop::Options::DEFAULT_MAXIMUM_EXCLUSION_ITEMS).to_i
  @show_offense_counts = !@options[:no_offense_counts]
end

#finished(_inspected_files) ⇒ Object



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

def finished(_inspected_files)
  output.puts HEADING % command

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

  output_offenses

  puts "Created #{output.path}."
  puts "Run `rubocop --config #{output.path}`, or add `inherit_from: " \
       "#{output.path}` in a .rubocop.yml file."
end

#output_cop(cop_name, offense_count) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 78

def output_cop(cop_name, offense_count)
  output.puts
  cfg = self.class.config_to_allow_offenses[cop_name] || {}

  output_cop_comments(output, cfg, cop_name, offense_count)
  output_cop_config(output, cfg, cop_name)
end

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 86

def output_cop_comments(output, cfg, cop_name, offense_count)
  output.puts "# Offense count: #{offense_count}" if @show_offense_counts
  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 Reference Enabled Exclude) -
           cfg.keys
  return if params.empty?

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

  params.each do |param|
    value = default_cfg[param]
    if value.is_a?(Array)
      next if value.empty?
      output.puts "# #{param}: #{value.join(', ')}"
    end
  end
end

#output_cop_config(output, cfg, cop_name) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 111

def output_cop_config(output, cfg, cop_name)
  # 'Enabled' option will be put into file only if exclude
  # limit is exceeded.
  cfg_without_enabled = cfg.reject { |key| key == 'Enabled' }

  output.puts "#{cop_name}:"
  cfg_without_enabled.each do |key, value|
    value = value[0] if value.is_a?(Array)
    output.puts "  #{key}: #{value}"
  end

  output_offending_files(output, cfg_without_enabled, cop_name)
end

#output_exclude_list(output, offending_files, cop_name) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 136

def output_exclude_list(output, offending_files, cop_name)
  require 'pathname'
  parent = Pathname.new(Dir.pwd)

  # Exclude properties in .rubocop_todo.yml override default ones, as well
  # as any custom excludes in .rubocop.yml, so in order to retain those
  # excludes we must copy them.
  # There can be multiple .rubocop.yml files in subdirectories, but we
  # just look at the current working directory
  config = ConfigStore.new.for(parent)
  cfg = config[cop_name] || {}
  excludes = ((cfg['Exclude'] || []) + offending_files).uniq

  output.puts '  Exclude:'
  excludes.each do |file|
    file_path = Pathname.new(file)
    begin
      relative = file_path.relative_path_from(parent)
      output.puts "    - '#{relative}'"
    rescue ArgumentError
      output.puts "    - '#{file}'"
    end
  end
rescue LoadError
  # Fallback to Enabled: false for Ruby < 1.9.3
  output.puts '  Enabled: false'
end

#output_offending_files(output, cfg, cop_name) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 125

def output_offending_files(output, cfg, cop_name)
  return unless cfg.empty?

  offending_files = @files_with_offenses[cop_name].uniq.sort
  if offending_files.count > @exclude_limit
    output.puts '  Enabled: false'
  else
    output_exclude_list(output, offending_files, cop_name)
  end
end

#output_offensesObject



72
73
74
75
76
# File 'lib/rubocop/formatter/disabled_config_formatter.rb', line 72

def output_offenses
  @cops_with_offenses.sort.each do |cop_name, offense_count|
    output_cop(cop_name, offense_count)
  end
end