# encoding: utf-8 module Rubocop module Formatter # This formatter displays a YAML configuration file where all cops that # detected any offenses are configured to not detect the offense. class DisabledConfigFormatter < BaseFormatter 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") @config_to_allow_offenses = {} COPS = Cop::Cop.all.group_by { |c| c.cop_name } class << self attr_accessor :config_to_allow_offenses end def file_finished(file, offenses) @cops_with_offenses ||= Hash.new(0) offenses.each { |o| @cops_with_offenses[o.cop_name] += 1 } end def finished(inspected_files) output.puts HEADING @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 with --config #{output.path}, or" puts "add inherit_from: #{output.path} in a .rubocop.yml file." end 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] if default_cfg params = default_cfg.keys - %w(Description Enabled) - cfg.keys unless params.empty? output.puts "# Configuration parameters: #{params.join(', ')}." end end end end end end