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
- .new(config_store, options) ⇒ TodoAudit constructor Internal use only
Instance Method Summary
- #todo_file Internal use only
- #unused_entries ⇒ Array<Entry>? Internal use only
- #absolute(path) private Internal use only
-
#audit_config(config, entries)
private
Internal use only
A copy of the configuration with the todo exclusions removed for the audited cops, so their offenses in the listed files become visible.
-
#auditable_cop?(cop_name, cop_config) ⇒ Boolean
private
Internal use only
Only registered cops with an
Excludelist can be audited - for an unknown cop (e.g. - #inspect_file(file, entries) private Internal use only
-
#offending_cops_without_todo(entries)
private
Internal use only
Inspects every file the todo entries mention, with the todo exclusions subtracted from the configuration, and returns the names of the cops that still report offenses, keyed by absolute file path.
- #subtract_excludes(cop_config, cop_entries) private Internal use only
- #todo_exclude_entries private Internal use only
- #todo_path private Internal use only
Constructor Details
.new(config_store, options) ⇒ TodoAudit
# File 'lib/rubocop/todo_audit.rb', line 11
def initialize(config_store, ) @config_store = config_store @options = end
Instance Method Details
#absolute(path) (private)
[ GitHub ]# File 'lib/rubocop/todo_audit.rb', line 107
def absolute(path) File.(path, File.dirname(todo_path)) end
#audit_config(config, entries) (private)
A copy of the configuration with the todo exclusions removed for the audited cops, so their offenses in the listed files become visible.
# File 'lib/rubocop/todo_audit.rb', line 89
def audit_config(config, entries) hash = config.to_hash.dup 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
#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.
#inspect_file(file, entries) (private)
[ GitHub ]# File 'lib/rubocop/todo_audit.rb', line 74
def inspect_file(file, entries) config = audit_config(@config_store.for_file(file), entries) team = Cop::Team.mobilize(Cop::Registry.global, config, @options.merge(autocorrect: false)) processed_source = ProcessedSource.from_file( file, config.target_ruby_version, parser_engine: config.parser_engine ) processed_source.config = config processed_source.registry = Cop::Registry.global team.investigate(processed_source).offenses.reject(&:disabled?) end
#offending_cops_without_todo(entries) (private)
Inspects every file the todo entries mention, with the todo exclusions subtracted from the configuration, and returns the names of the cops that still report offenses, keyed by absolute file path.
# File 'lib/rubocop/todo_audit.rb', line 65
def offending_cops_without_todo(entries) files = entries.map { |entry| absolute(entry.path) }.uniq.select { |file| File.file?(file) } files.to_h do |file| offenses = inspect_file(file, entries) [file, offenses.to_set(&:cop_name)] end end
#subtract_excludes(cop_config, cop_entries) (private)
[ GitHub ]# File 'lib/rubocop/todo_audit.rb', line 102
def subtract_excludes(cop_config, cop_entries) removals = cop_entries.map { |entry| absolute(entry.path) } cop_config.merge('Exclude' => cop_config['Exclude'] - removals) 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.(todo_file) @todo_path = File.exist?(path) ? path : nil end
#unused_entries ⇒ Array<Entry>?
# File 'lib/rubocop/todo_audit.rb', line 18
def unused_entries return nil unless todo_path entries = todo_exclude_entries return [] if entries.empty? offending_cops = offending_cops_without_todo(entries) entries.reject { |entry| offending_cops[absolute(entry.path)]&.include?(entry.cop_name) } end