Class: RuboCop::Cop::Team
- Inherits:
-
Object
- Object
- RuboCop::Cop::Team
- 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).
Instance Attribute Summary collapse
-
#cops ⇒ Object
readonly
Returns the value of attribute cops.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#updated_source_file ⇒ Object
(also: #updated_source_file?)
readonly
Returns the value of attribute updated_source_file.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Class Method Summary collapse
-
.forces_for(cops) ⇒ Array<Force>
Needed for the given cops.
-
.mobilize(cop_classes, config, options = {}) ⇒ Team
With cops assembled from the given ‘cop_classes`.
- .mobilize_cops(cop_classes, config, options = {}) ⇒ Array<Cop::Base>
- .new(cop_or_classes, config, options = {}) ⇒ Team
Instance Method Summary collapse
- #autocorrect? ⇒ Boolean
- #debug? ⇒ Boolean
- #external_dependency_checksum ⇒ Object
- #forces ⇒ Object deprecated Deprecated.
-
#initialize(cops, config = nil, options = {}) ⇒ Team
constructor
A new instance of Team.
- #inspect_file(processed_source) ⇒ Object
- #investigate(processed_source, offset: 0, original: processed_source) ⇒ Commissioner::InvestigationReport
Constructor Details
#initialize(cops, config = nil, options = {}) ⇒ Team
Returns a new instance of Team.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rubocop/cop/team.rb', line 55 def initialize(cops, config = nil, = {}) @cops = cops @config = config @options = reset @ready = true @registry = Registry.new(cops, .dup) validate_config end |
Instance Attribute Details
#cops ⇒ Object (readonly)
Returns the value of attribute cops.
51 52 53 |
# File 'lib/rubocop/cop/team.rb', line 51 def cops @cops end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
51 52 53 |
# File 'lib/rubocop/cop/team.rb', line 51 def errors @errors end |
#updated_source_file ⇒ Object (readonly) Also known as: updated_source_file?
Returns the value of attribute updated_source_file.
51 52 53 |
# File 'lib/rubocop/cop/team.rb', line 51 def updated_source_file @updated_source_file end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
51 52 53 |
# File 'lib/rubocop/cop/team.rb', line 51 def warnings @warnings end |
Class Method Details
.forces_for(cops) ⇒ Array<Force>
Returns needed for the given cops.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rubocop/cop/team.rb', line 37 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`.
22 23 24 25 |
# File 'lib/rubocop/cop/team.rb', line 22 def self.mobilize(cop_classes, config, = {}) cops = mobilize_cops(cop_classes, config, ) new(cops, config, ) end |
.mobilize_cops(cop_classes, config, options = {}) ⇒ Array<Cop::Base>
28 29 30 31 32 33 34 |
# File 'lib/rubocop/cop/team.rb', line 28 def self.mobilize_cops(cop_classes, config, = {}) cop_classes = Registry.new(cop_classes.to_a, ) unless cop_classes.is_a?(Registry) cop_classes.map do |cop_class| cop_class.new(config, ) end end |
.new(cop_or_classes, config, options = {}) ⇒ Team
14 15 16 17 18 19 |
# File 'lib/rubocop/cop/team.rb', line 14 def self.new(cop_or_classes, config, = {}) # Support v0 api: return mobilize(cop_or_classes, config, ) if cop_or_classes.first.is_a?(Class) super end |
Instance Method Details
#autocorrect? ⇒ Boolean
66 67 68 |
# File 'lib/rubocop/cop/team.rb', line 66 def autocorrect? @options[:autocorrect] end |
#debug? ⇒ Boolean
70 71 72 |
# File 'lib/rubocop/cop/team.rb', line 70 def debug? @options[:debug] end |
#external_dependency_checksum ⇒ Object
114 115 116 117 |
# File 'lib/rubocop/cop/team.rb', line 114 def external_dependency_checksum keys = cops.filter_map(&:external_dependency_checksum) Digest::SHA1.hexdigest(keys.join) end |
#forces ⇒ Object
110 111 112 |
# File 'lib/rubocop/cop/team.rb', line 110 def forces @forces ||= self.class.forces_for(cops) end |
#inspect_file(processed_source) ⇒ Object
76 77 78 |
# File 'lib/rubocop/cop/team.rb', line 76 def inspect_file(processed_source) investigate(processed_source).offenses end |
#investigate(processed_source, offset: 0, original: processed_source) ⇒ Commissioner::InvestigationReport
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rubocop/cop/team.rb', line 81 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 |