Class: RuboCop::Cop::Team

Inherits:
Object
  • Object
show all
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). rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cops, config = nil, options = {}) ⇒ Team

Returns a new instance of Team.

 61 62 63 64 65 66 67 68 69 70
# File 'lib/rubocop/cop/team.rb', line 61 def initialize(cops, config = nil, options = {}) @cops = cops @config = config @options = options reset @ready = true @registry = Registry.new(cops, options.dup) validate_config end

Instance Attribute Details

#copsObject (readonly)

Returns the value of attribute cops.

 57 58 59
# File 'lib/rubocop/cop/team.rb', line 57 def cops @cops end

#errorsObject (readonly)

Returns the value of attribute errors.

 57 58 59
# File 'lib/rubocop/cop/team.rb', line 57 def errors @errors end

#updated_source_fileObject (readonly) Also known as: updated_source_file?

Returns the value of attribute updated_source_file.

 57 58 59
# File 'lib/rubocop/cop/team.rb', line 57 def updated_source_file @updated_source_file end

#warningsObject (readonly)

Returns the value of attribute warnings.

 57 58 59
# File 'lib/rubocop/cop/team.rb', line 57 def warnings @warnings end

Class Method Details

.forces_for(cops) ⇒ Array<Force>

Returns needed for the given cops.

Returns:

  • (Array<Force>)

    needed for the given cops

 43 44 45 46 47 48 49 50 51 52 53 54 55
# 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

Returns with cops assembled from the given ‘cop_classes`.

Returns:

  • (Team)

    with cops assembled from the given ‘cop_classes`

 28 29 30 31
# File 'lib/rubocop/cop/team.rb', line 28 def self.mobilize(cop_classes, config, options = {}) cops = mobilize_cops(cop_classes, config, options) new(cops, config, options) end

.mobilize_cops(cop_classes, config, options = {}) ⇒ Array<Cop::Base>

Returns:

 34 35 36 37 38 39 40
# File 'lib/rubocop/cop/team.rb', line 34 def self.mobilize_cops(cop_classes, config, options = {}) cop_classes = Registry.new(cop_classes.to_a, options) unless cop_classes.is_a?(Registry) cop_classes.map do |cop_class| cop_class.new(config, options) end end

.new(cop_or_classes, config, options = {}) ⇒ Team

Returns:

 15 16 17 18 19 20 21 22 23 24 25
# File 'lib/rubocop/cop/team.rb', line 15 def self.new(cop_or_classes, config, options = {}) # 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, options) end super end

Instance Method Details

#autocorrect?Boolean

Returns:

  • (Boolean)
 72 73 74
# File 'lib/rubocop/cop/team.rb', line 72 def autocorrect? @options[:autocorrect] end

#debug?Boolean

Returns:

  • (Boolean)
 76 77 78
# File 'lib/rubocop/cop/team.rb', line 76 def debug? @options[:debug] end

#external_dependency_checksumObject

 128 129 130 131 132 133 134 135
# 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

#forcesObject

Deprecated.
 120 121 122 123 124 125 126
# 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

#inspect_file(processed_source) ⇒ Object

 82 83 84 85 86 87 88
# 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

 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
# 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