2022-03-09 19:08:36 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'set'
|
|
|
|
require 'rubocop'
|
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
require_relative '../todo_dir'
|
2022-07-20 08:08:44 -04:00
|
|
|
require_relative '../cop_todo'
|
2022-09-12 17:10:38 -04:00
|
|
|
require_relative '../formatter/graceful_formatter'
|
2022-03-09 19:08:36 -05:00
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Formatter
|
|
|
|
# This formatter dumps a YAML configuration file per cop rule
|
|
|
|
# into `.rubocop_todo/**/*.yml` which contains detected offenses.
|
|
|
|
#
|
|
|
|
# For example, this formatter stores offenses for `RSpec/VariableName`
|
|
|
|
# in `.rubocop_todo/rspec/variable_name.yml`.
|
|
|
|
class TodoFormatter < BaseFormatter
|
2022-05-18 14:08:05 -04:00
|
|
|
DEFAULT_BASE_DIRECTORY = File.expand_path('../../.rubocop_todo', __dir__)
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :base_directory
|
|
|
|
end
|
|
|
|
|
|
|
|
self.base_directory = DEFAULT_BASE_DIRECTORY
|
|
|
|
|
|
|
|
def initialize(output, _options = {})
|
|
|
|
@directory = self.class.base_directory
|
2022-07-20 08:08:44 -04:00
|
|
|
@todos = Hash.new { |hash, cop_name| hash[cop_name] = CopTodo.new(cop_name) }
|
2022-03-09 19:08:36 -05:00
|
|
|
@todo_dir = TodoDir.new(directory)
|
2022-05-18 14:08:05 -04:00
|
|
|
@config_inspect_todo_dir = load_config_inspect_todo_dir
|
|
|
|
@config_old_todo_yml = load_config_old_todo_yml
|
2022-03-09 19:08:36 -05:00
|
|
|
check_multiple_configurations!
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_finished(file, offenses)
|
|
|
|
return if offenses.empty?
|
|
|
|
|
|
|
|
file = relative_path(file)
|
|
|
|
|
|
|
|
offenses.map(&:cop_name).tally.each do |cop_name, offense_count|
|
|
|
|
@todos[cop_name].record(file, offense_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def finished(_inspected_files)
|
|
|
|
@todos.values.sort_by(&:cop_name).each do |todo|
|
2022-07-20 08:08:44 -04:00
|
|
|
todo.previously_disabled = previously_disabled?(todo)
|
2022-09-12 17:10:38 -04:00
|
|
|
todo.grace_period = grace_period?(todo)
|
|
|
|
validate_todo!(todo)
|
2022-07-20 08:08:44 -04:00
|
|
|
path = @todo_dir.write(todo.cop_name, todo.to_yaml)
|
2022-03-09 19:08:36 -05:00
|
|
|
|
|
|
|
output.puts "Written to #{relative_path(path)}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
def self.with_base_directory(directory)
|
|
|
|
old = base_directory
|
|
|
|
self.base_directory = directory
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
self.base_directory = old
|
|
|
|
end
|
|
|
|
|
2022-03-09 19:08:36 -05:00
|
|
|
private
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
attr_reader :directory
|
|
|
|
|
2022-03-09 19:08:36 -05:00
|
|
|
def relative_path(path)
|
2022-05-18 14:08:05 -04:00
|
|
|
parent = File.expand_path('..', directory)
|
2022-03-09 19:08:36 -05:00
|
|
|
path.delete_prefix("#{parent}/")
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_multiple_configurations!
|
|
|
|
cop_names = @config_inspect_todo_dir.keys & @config_old_todo_yml.keys
|
|
|
|
return if cop_names.empty?
|
|
|
|
|
|
|
|
list = cop_names.sort.map { |cop_name| "- #{cop_name}" }.join("\n")
|
|
|
|
raise "Multiple configurations found for cops:\n#{list}\n"
|
|
|
|
end
|
|
|
|
|
2022-09-12 17:10:38 -04:00
|
|
|
def config_for(todo)
|
2022-03-09 19:08:36 -05:00
|
|
|
cop_name = todo.cop_name
|
|
|
|
|
2022-09-12 17:10:38 -04:00
|
|
|
@config_old_todo_yml[cop_name] || @config_inspect_todo_dir[cop_name] || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def previously_disabled?(todo)
|
|
|
|
config = config_for(todo)
|
2022-03-09 19:08:36 -05:00
|
|
|
return false if config.empty?
|
|
|
|
|
|
|
|
config['Enabled'] == false
|
|
|
|
end
|
|
|
|
|
2022-09-12 17:10:38 -04:00
|
|
|
def grace_period?(todo)
|
|
|
|
config = config_for(todo)
|
|
|
|
|
|
|
|
GracefulFormatter.grace_period?(todo.cop_name, config)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_todo!(todo)
|
|
|
|
return unless todo.previously_disabled && todo.grace_period
|
|
|
|
|
|
|
|
raise "#{todo.cop_name}: Cop must be enabled to use `#{GracefulFormatter.grace_period_key_value}`."
|
|
|
|
end
|
|
|
|
|
2022-05-18 14:08:05 -04:00
|
|
|
def load_config_inspect_todo_dir
|
2022-03-09 19:08:36 -05:00
|
|
|
@todo_dir.list_inspect.each_with_object({}) do |path, combined|
|
|
|
|
config = YAML.load_file(path)
|
|
|
|
combined.update(config) if Hash === config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Load YAML configuration from `.rubocop_todo.yml`.
|
|
|
|
# We consider this file already old, obsolete, and to be removed soon.
|
2022-05-18 14:08:05 -04:00
|
|
|
def load_config_old_todo_yml
|
2022-03-09 19:08:36 -05:00
|
|
|
path = File.expand_path(File.join(directory, '../.rubocop_todo.yml'))
|
|
|
|
config = YAML.load_file(path) if File.exist?(path)
|
|
|
|
|
|
|
|
config || {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|