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)
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. - #build_audit_config(config) private Internal use only
- #expand_path(path) private Internal use only
- #inspect_file(file) private Internal use only
-
#needed?(entry) ⇒ Boolean
private
Internal use only
An entry is needed when any expanded path still reports an offense for its cop.
- #offending_cops(file) private Internal use only
- #scan_root(path) private Internal use only
- #subtract_excludes(cop_config, cop_entries) private Internal use only
-
#target_files(path)
private
Internal use only
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. -
#todo_cops
private
Internal use only
Registry limited to cops named in the todo, so each file only pays for the audits that can keep an entry.
- #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 153
def absolute(path) File.(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.
# 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.
#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 (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.
# File 'lib/rubocop/todo_audit.rb', line 64
def needed?(entry) (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 ]#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.
# 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.
#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? @todo_entries = entries entries.reject { |entry| needed?(entry) } end