Class: RuboCop::Cop::Team
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubocop/cop/team.rb |
Overview
A group of cops, ready to be called on duty to inspect files. Team is responsible for selecting only relevant cops to be sent on duty, as well as insuring that the needed forces are sent along with them.
For performance reasons, Team will first dispatch cops & forces in two groups, first the ones needed for autocorrection (if any), then the rest (unless autocorrections happened).
Class Method Summary
Instance Attribute Summary
- #autocorrect? ⇒ Boolean readonly
- #cops readonly
- #debug? ⇒ Boolean readonly
- #errors readonly
-
#updated_source_file?
readonly
Alias for #updated_source_file.
- #warnings readonly
Instance Method Summary
- #external_dependency_checksum
- #forces deprecated Deprecated.
- #initialize(cops, config = nil, options = {}) ⇒ Team constructor
- #inspect_file(processed_source)
- #investigate(processed_source, offset: 0, original: processed_source) ⇒ Commissioner::InvestigationReport
- #updated_source_file (also: #updated_source_file?) readonly
- #autocorrect(processed_source, report, original:, offset:) readonly private
- #autocorrect_report(report, offset:, original:) private
- #be_ready private
- #collate_corrections(report, offset:, original:) private
- #each_corrector(report) private
- #handle_error(error, location, cop) private
- #handle_warning(error, location) private
- #investigate_partial(cops, processed_source, offset:, original:) ⇒ Commissioner::InvestigationReport private
- #process_errors(file, errors) private
- #reset private
- #roundup_relevant_cops(processed_source) ⇒ Array<cop> private
- #support_target_rails_version?(cop) ⇒ Boolean private
- #support_target_ruby_version?(cop) ⇒ Boolean private
- #suppress_clobbering private
- #validate_config private
Constructor Details
.new(cop_or_classes, config, options = {}) ⇒ Team
# File 'lib/rubocop/cop/team.rb', line 15
def self.new(cop_or_classes, config, = {}) # Support v0 api: if cop_or_classes.first.is_a?(Class) warn Rainbow(<<~WARNING).yellow, uplevel: 1 `Team.new` with cop classes is deprecated. Use `Team.mobilize` instead. WARNING return mobilize(cop_or_classes, config, ) end super end
#initialize(cops, config = nil, options = {}) ⇒ Team
Class Method Details
.forces_for(cops) ⇒ Array
<Force>
# File 'lib/rubocop/cop/team.rb', line 43
def self.forces_for(cops) needed = Hash.new { |h, k| h[k] = [] } cops.each do |cop| forces = cop.class.joining_forces if forces.is_a?(Array) forces.each { |force| needed[force] << cop } elsif forces needed[forces] << cop end end needed.map { |force_class, joining_cops| force_class.new(joining_cops) } end
.mobilize(cop_classes, config, options = {}) ⇒ Team
.mobilize_cops(cop_classes, config, options = {}) ⇒ Array
<Cop::Base>
Instance Attribute Details
#autocorrect? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/rubocop/cop/team.rb', line 72
def autocorrect? @options[:autocorrect] end
#cops (readonly)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 57
attr_reader :errors, :warnings, :updated_source_file, :cops
#debug? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/rubocop/cop/team.rb', line 76
def debug? @options[:debug] end
#errors (readonly)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 57
attr_reader :errors, :warnings, :updated_source_file, :cops
#updated_source_file? (readonly)
Alias for #updated_source_file.
# File 'lib/rubocop/cop/team.rb', line 59
alias updated_source_file? updated_source_file
#warnings (readonly)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 57
attr_reader :errors, :warnings, :updated_source_file, :cops
Instance Method Details
#autocorrect(processed_source, report, original:, offset:) (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 139
def autocorrect(processed_source, report, original:, offset:) @updated_source_file = false return unless autocorrect? return if report.processed_source.parser_error new_source = autocorrect_report(report, original: original, offset: offset) return unless new_source if @options[:stdin] # holds source read in from stdin, when --stdin option is used @options[:stdin] = new_source else filename = processed_source.file_path File.write(filename, new_source) end @updated_source_file = true end
#autocorrect_report(report, offset:, original:) (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 203
def autocorrect_report(report, offset:, original:) corrector = collate_corrections(report, offset: offset, original: original) corrector.rewrite unless corrector.empty? end
#be_ready (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 158
def be_ready return if @ready reset @cops.map!(&:ready) @ready = true end
#collate_corrections(report, offset:, original:) (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 209
def collate_corrections(report, offset:, original:) corrector = Corrector.new(original) each_corrector(report) do |to_merge| suppress_clobbering do if offset.positive? corrector.import!(to_merge, offset: offset) else corrector.merge!(to_merge) end end end corrector end
#each_corrector(report) (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 225
def each_corrector(report) skips = Set.new report.cop_reports.each do |cop_report| cop = cop_report.cop corrector = cop_report.corrector next if corrector.nil? || corrector.empty? next if skips.include?(cop.class) yield corrector skips.merge(cop.class.autocorrect_incompatible_with) end end
#external_dependency_checksum
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 128
def external_dependency_checksum # The external dependency checksums are cached per RuboCop team so that # the checksums don't need to be recomputed for each file. @external_dependency_checksum ||= begin keys = cops.filter_map(&:external_dependency_checksum) Digest::SHA1.hexdigest(keys.join) end end
#forces
Deprecated.
# File 'lib/rubocop/cop/team.rb', line 120
def forces warn Rainbow(<<~WARNING).yellow, uplevel: 1 `forces` is deprecated. WARNING @forces ||= self.class.forces_for(cops) end
#handle_error(error, location, cop) (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 277
def handle_error(error, location, cop) = Rainbow("An error occurred while #{cop.name} cop was inspecting #{location}.").red @errors << warn if debug? puts error., error.backtrace else warn 'To see the complete backtrace run rubocop -d.' end end
#handle_warning(error, location) (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 269
def handle_warning(error, location) = Rainbow("#{error.} (from file: #{location})").yellow @warnings << warn puts error.backtrace if debug? end
#inspect_file(processed_source)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 82
def inspect_file(processed_source) warn Rainbow(<<~WARNING).yellow, uplevel: 1 `inspect_file` is deprecated. Use `investigate` instead. WARNING investigate(processed_source).offenses end
#investigate(processed_source, offset: 0, original: processed_source) ⇒ Commissioner::InvestigationReport
# File 'lib/rubocop/cop/team.rb', line 91
def investigate(processed_source, offset: 0, original: processed_source) be_ready # The autocorrection process may have to be repeated multiple times # until there are no corrections left to perform # To speed things up, run autocorrecting cops by themselves, and only # run the other cops when no corrections are left on_duty = roundup_relevant_cops(processed_source) autocorrect_cops, other_cops = on_duty.partition(&:autocorrect?) report = investigate_partial(autocorrect_cops, processed_source, offset: offset, original: original) unless autocorrect(processed_source, report, offset: offset, original: original) # If we corrected some errors, another round of inspection will be # done, and any other offenses will be caught then, so only need # to check other_cops if no correction was done report = report.merge(investigate_partial(other_cops, processed_source, offset: offset, original: original)) end process_errors(processed_source.path, report.errors) report ensure @ready = false end
#investigate_partial(cops, processed_source, offset:, original:) ⇒ Commissioner::InvestigationReport (private)
# File 'lib/rubocop/cop/team.rb', line 172
def investigate_partial(cops, processed_source, offset:, original:) commissioner = Commissioner.new(cops, self.class.forces_for(cops), @options) commissioner.investigate(processed_source, offset: offset, original: original) end
#process_errors(file, errors) (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 252
def process_errors(file, errors) errors.each do |error| line = ":#{error.line}" if error.line column = ":#{error.column}" if error.column location = "#{file}#{line}#{column}" cause = error.cause if cause.is_a?(Warning) handle_warning(cause, location) elsif cause.is_a?(Force::HookError) handle_error(cause.cause, location, cause.joining_cop) else handle_error(cause, location, error.cop) end end end
#reset (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 166
def reset @errors = [] @warnings = [] end
#roundup_relevant_cops(processed_source) ⇒ Array
<cop
> (private)
# File 'lib/rubocop/cop/team.rb', line 178
def roundup_relevant_cops(processed_source) cops.select do |cop| next true if processed_source.comment_config.cop_opted_in?(cop) next false if cop.excluded_file?(processed_source.file_path) next false unless @registry.enabled?(cop, @config) support_target_ruby_version?(cop) && support_target_rails_version?(cop) end end
#support_target_rails_version?(cop) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/team.rb', line 194
def support_target_rails_version?(cop) # In this case, the rails version was already checked by `#excluded_file?` return true if defined?(RuboCop::Rails::TargetRailsVersion::USES_REQUIRES_GEM_API) return true unless cop.class.respond_to?(:support_target_rails_version?) cop.class.support_target_rails_version?(cop.target_rails_version) end
#support_target_ruby_version?(cop) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/team.rb', line 188
def support_target_ruby_version?(cop) return true unless cop.class.respond_to?(:support_target_ruby_version?) cop.class.support_target_ruby_version?(cop.target_ruby_version) end
#suppress_clobbering (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 240
def suppress_clobbering yield rescue ::Parser::ClobberingError # ignore Clobbering errors end
#updated_source_file (readonly) Also known as: #updated_source_file?
[ GitHub ]#validate_config (private)
[ GitHub ]# File 'lib/rubocop/cop/team.rb', line 246
def validate_config cops.each do |cop| cop.validate_config if cop.respond_to?(:validate_config) end end