diff options
Diffstat (limited to 'rubocop/formatter/todo_formatter.rb')
-rw-r--r-- | rubocop/formatter/todo_formatter.rb | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/rubocop/formatter/todo_formatter.rb b/rubocop/formatter/todo_formatter.rb index 14b4242063e..662cc1551ff 100644 --- a/rubocop/formatter/todo_formatter.rb +++ b/rubocop/formatter/todo_formatter.rb @@ -14,13 +14,6 @@ module RuboCop # For example, this formatter stores offenses for `RSpec/VariableName` # in `.rubocop_todo/rspec/variable_name.yml`. class TodoFormatter < BaseFormatter - # Disable a cop which exceeds this limit. This way we ensure that we - # don't enable a cop by accident when moving it from - # .rubocop_todo.yml to .rubocop_todo/. - # We keep the cop disabled if it has been disabled previously explicitly - # via `Enabled: false` in .rubocop_todo.yml or .rubocop_todo/. - MAX_OFFENSE_COUNT = 15 - class Todo attr_reader :cop_name, :files, :offense_count @@ -41,12 +34,20 @@ module RuboCop end end - def initialize(output, options = {}) - directory = options.delete(:rubocop_todo_dir) || TodoDir::DEFAULT_TODO_DIR + 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 @todos = Hash.new { |hash, cop_name| hash[cop_name] = Todo.new(cop_name) } @todo_dir = TodoDir.new(directory) - @config_inspect_todo_dir = load_config_inspect_todo_dir(directory) - @config_old_todo_yml = load_config_old_todo_yml(directory) + @config_inspect_todo_dir = load_config_inspect_todo_dir + @config_old_todo_yml = load_config_old_todo_yml check_multiple_configurations! super @@ -71,10 +72,21 @@ module RuboCop end end + def self.with_base_directory(directory) + old = base_directory + self.base_directory = directory + + yield + ensure + self.base_directory = old + end + private + attr_reader :directory + def relative_path(path) - parent = File.expand_path('..', @todo_dir.directory) + parent = File.expand_path('..', directory) path.delete_prefix("#{parent}/") end @@ -84,7 +96,7 @@ module RuboCop yaml << '# Cop supports --auto-correct.' if todo.autocorrectable? yaml << "#{todo.cop_name}:" - if previously_disabled?(todo) && offense_count_exceeded?(todo) + if previously_disabled?(todo) yaml << " # Offense count: #{todo.offense_count}" yaml << ' # Temporarily disabled due to too many offenses' yaml << ' Enabled: false' @@ -99,10 +111,6 @@ module RuboCop yaml.join("\n") end - def offense_count_exceeded?(todo) - todo.offense_count > MAX_OFFENSE_COUNT - end - def check_multiple_configurations! cop_names = @config_inspect_todo_dir.keys & @config_old_todo_yml.keys return if cop_names.empty? @@ -121,7 +129,7 @@ module RuboCop config['Enabled'] == false end - def load_config_inspect_todo_dir(directory) + def load_config_inspect_todo_dir @todo_dir.list_inspect.each_with_object({}) do |path, combined| config = YAML.load_file(path) combined.update(config) if Hash === config @@ -130,7 +138,7 @@ module RuboCop # Load YAML configuration from `.rubocop_todo.yml`. # We consider this file already old, obsolete, and to be removed soon. - def load_config_old_todo_yml(directory) + def load_config_old_todo_yml path = File.expand_path(File.join(directory, '../.rubocop_todo.yml')) config = YAML.load_file(path) if File.exist?(path) |