class RubbyCop::Formatter::DisabledConfigFormatter

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

Constants

COPS
HEADING

Attributes

config_to_allow_offenses[RW]
detected_styles[RW]

Public Class Methods

new(output, options = {}) click to toggle source
Calls superclass method RubbyCop::Formatter::BaseFormatter::new
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 27
def initialize(output, options = {})
  super
  @cops_with_offenses ||= Hash.new(0)
  @files_with_offenses ||= {}
end

Public Instance Methods

file_finished(file, offenses) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 41
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) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 33
def file_started(_file, _file_info)
  @exclude_limit_option = @options[:exclude_limit]
  @exclude_limit = (
    @exclude_limit_option ||
    RubbyCop::Options::DEFAULT_MAXIMUM_EXCLUSION_ITEMS).to_i
  @show_offense_counts = !@options[:no_offense_counts]
end
finished(_inspected_files) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 49
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 `rubbycop --config #{output.path}`, or add `inherit_from: " \
       "#{output.path}` in a .rubbycop.yml file."
end

Private Instance Methods

command() click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 64
def command
  command = 'rubbycop --auto-gen-config'
  if @exclude_limit_option
    command += format(' --exclude-limit %d', @exclude_limit_option.to_i)
  end
  command += ' --no-offense-counts' if @options[:no_offense_counts]

  command
end
cop_config_params(default_cfg, cfg) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 103
def cop_config_params(default_cfg, cfg)
  default_cfg.keys -
    %w[Description StyleGuide Reference Enabled Exclude] -
    cfg.keys
end
default_config(cop_name) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 121
def default_config(cop_name)
  RubbyCop::ConfigLoader.default_configuration[cop_name]
end
excludes(offending_files, cop_name, parent) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 160
def excludes(offending_files, cop_name, parent)
  # Exclude properties in .rubbycop_todo.yml override default ones, as well
  # as any custom excludes in .rubbycop.yml, so in order to retain those
  # excludes we must copy them.
  # There can be multiple .rubbycop.yml files in subdirectories, but we
  # just look at the current working directory
  config = ConfigStore.new.for(parent)
  cfg = config[cop_name] || {}

  ((cfg['Exclude'] || []) + offending_files).uniq
end
output_cop(cop_name, offense_count) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 80
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) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 88
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 = default_config(cop_name)
  return unless default_cfg

  params = cop_config_params(default_cfg, cfg)
  return if params.empty?

  output_cop_param_comments(params, default_cfg)
end
output_cop_config(output, cfg, cop_name) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 125
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_cop_param_comments(params, default_cfg) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 109
def output_cop_param_comments(params, default_cfg)
  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_exclude_list(output, offending_files, cop_name) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 150
def output_exclude_list(output, offending_files, cop_name)
  require 'pathname'
  parent = Pathname.new(Dir.pwd)

  output.puts '  Exclude:'
  excludes(offending_files, cop_name, parent).each do |file|
    output_exclude_path(output, file, parent)
  end
end
output_exclude_path(output, file, parent) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 172
def output_exclude_path(output, file, parent)
  file_path = Pathname.new(file)
  relative = file_path.relative_path_from(parent)
  output.puts "    - '#{relative}'"
rescue ArgumentError
  output.puts "    - '#{file}'"
end
output_offending_files(output, cfg, cop_name) click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 139
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_offenses() click to toggle source
# File lib/rubbycop/formatter/disabled_config_formatter.rb, line 74
def output_offenses
  @cops_with_offenses.sort.each do |cop_name, offense_count|
    output_cop(cop_name, offense_count)
  end
end