123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::TodoAudit Private

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/rubocop/todo_audit.rb

Overview

Audits .rubocop_todo.yml for Exclude entries that are no longer needed: files a cop would not flag anymore, or files that no longer exist. Used by the --report-unused-todo-entries option.

Class Method Summary

Instance Method Summary

Constructor Details

.new(config_store, options) ⇒ TodoAudit

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 11

def initialize(config_store, options)
  @config_store = config_store
  @options = options
end

Instance Method Details

#absolute(path) (private)

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 153

def absolute(path)
  File.expand_path(path, File.dirname(todo_path))
end

#audit_config(config) (private)

A copy of the configuration with the todo exclusions removed for the audited cops, so their offenses in the listed files become visible. Memoized per underlying Config so cop lookups stay warm across files.

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 130

def audit_config(config)
  @audit_configs ||= {}.compare_by_identity
  @audit_configs[config] ||= build_audit_config(config)
end

#auditable_cop?(cop_name, cop_config) ⇒ Boolean (private)

Only registered cops with an Exclude list can be audited - for an unknown cop (e.g. from an extension that is not loaded in this run) the absence of offenses proves nothing.

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 55

def auditable_cop?(cop_name, cop_config)
  cop_name.include?('/') &&
    cop_config.is_a?(Hash) &&
    cop_config['Exclude'].is_a?(Array) &&
    Cop::Registry.global.contains_cop_matching?([cop_name])
end

#build_audit_config(config) (private)

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 135

def build_audit_config(config)
  hash = config.to_hash.dup

  @todo_entries.group_by(&:cop_name).each do |cop_name, cop_entries|
    cop_config = hash[cop_name]
    next unless cop_config.is_a?(Hash) && cop_config['Exclude'].is_a?(Array)

    hash[cop_name] = subtract_excludes(cop_config, cop_entries)
  end

  Config.create(hash, config.loaded_path, check: false)
end

#expand_path(path) (private)

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 73

def expand_path(path)
  absolute_path = absolute(path)
  files = File.file?(absolute_path) ? [absolute_path] : []
  return files unless PathUtil.glob?(path)

  files | target_files(path).select do |file|
    PathUtil.match_path?(absolute_path, file)
  end
end

#inspect_file(file) (private)

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 102

def inspect_file(file)
  config = audit_config(@config_store.for_file(file))
  team = Cop::Team.mobilize(todo_cops, config, @options.merge(autocorrect: false))

  processed_source = ProcessedSource.from_file(
    file, config.target_ruby_version, parser_engine: config.parser_engine
  )
  processed_source.config = config
  # CommentConfig reads this registry to recognize cops disabled in the
  # config. A filtered registry would treat `# rubocop:enable` of such a
  # cop as redundant and keep a stale todo entry.
  processed_source.registry = Cop::Registry.global

  team.investigate(processed_source).offenses.reject(&:disabled?)
end

#needed?(entry) ⇒ Boolean (private)

An entry is needed when any expanded path still reports an offense for its cop. Stop at the first hit so live glob entries skip most matches.

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 64

def needed?(entry)
  expand_path(entry.path).any? { |file| offending_cops(file).include?(entry.cop_name) }
end

#offending_cops(file) (private)

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 68

def offending_cops(file)
  @offending_cops ||= {}
  @offending_cops[file] ||= inspect_file(file).to_set(&:cop_name)
end

#scan_root(path) (private)

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 94

def scan_root(path)
  segments = path.split('/', -1)
  literal = segments.take_while { |segment| !PathUtil.glob?(segment) }
  prefix = literal.join('/')
  prefix = '/' if prefix.empty? && path.start_with?('/')
  absolute(prefix.empty? ? '.' : prefix)
end

#subtract_excludes(cop_config, cop_entries) (private)

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 148

def subtract_excludes(cop_config, cop_entries)
  removals = cop_entries.map { |entry| absolute(entry.path) }
  cop_config.merge('Exclude' => cop_config['Exclude'] - removals)
end

#target_files(path) (private)

Scan from the longest glob-free prefix of the entry as written, so a glob in any directory segment (e.g. */models/.rb) is covered. Only the entry’s own segments are inspected; metacharacters in the todo file’s parent directories never move the scan root.

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 87

def target_files(path)
  directory = scan_root(path)
  @target_files ||= {}
  @target_files[directory] ||= TargetFinder.new(@config_store, @options)
                                           .target_files_in_dir(directory)
end

#todo_cops (private)

Registry limited to cops named in the todo, so each file only pays for the audits that can keep an entry.

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 120

def todo_cops
  @todo_cops ||= begin
    names = @todo_entries.map(&:cop_name).uniq
    Cop::Registry.global.filter_by_badge { |badge| badge.match_name?(names) }
  end
end

#todo_exclude_entries (private)

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 41

def todo_exclude_entries
  yaml = YAML.safe_load_file(todo_path, permitted_classes: [Regexp, Symbol], aliases: true)
  return [] unless yaml.is_a?(Hash)

  yaml.flat_map do |cop_name, cop_config|
    next [] unless auditable_cop?(cop_name, cop_config)

    cop_config['Exclude'].grep(String).map { |path| Entry.new(cop_name, path) }
  end
end

#todo_file

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 28

def todo_file
  CLI::Command::AutoGenerateConfig::AUTO_GENERATED_FILE
end

#todo_path (private)

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 34

def todo_path
  return @todo_path if defined?(@todo_path)

  path = File.expand_path(todo_file)
  @todo_path = File.exist?(path) ? path : nil
end

#unused_entries ⇒ Array<Entry>?

Returns:

  • (Array<Entry>, nil) —

    the unused entries, or nil when there is no todo file to audit

[ GitHub ]

  
# File 'lib/rubocop/todo_audit.rb', line 18

def unused_entries
  return nil unless todo_path

  entries = todo_exclude_entries
  return [] if entries.empty?

  @todo_entries = entries
  entries.reject { |entry| needed?(entry) }
end