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). rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#cops ⇒ Object
readonly
Returns the value of attribute cops.
-
#defer_corrections ⇒ Object
private
When set to true, the corrected source is not written back to the inspected file; it is exposed through
#updated_sourceinstead. -
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#standby_registry ⇒ Object
writeonly
private
Registry used to mobilize cops that were not instantiated because they are disabled in the config, when a comment directive opts them back in for a file.
-
#updated_source ⇒ Object
readonly
private
The corrected source of the last investigation, if corrections were made with
#defer_correctionsenabled. -
#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
- #investigate_fragments(fragments, original:) ⇒ Array<Offense>
Constructor Details
#initialize(cops, config = nil, options = {}) ⇒ Team
Returns a new instance of Team.
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rubocop/cop/team.rb', line 89 def initialize(cops, config = nil, = {}) @cops = cops @config = config @options = @standby_registry = nil @standby_cops = {} reset @ready = true @registry = Registry.new(cops, .dup) validate_config end |
Instance Attribute Details
#cops ⇒ Object (readonly)
Returns the value of attribute cops.
69 70 71 |
# File 'lib/rubocop/cop/team.rb', line 69 def cops @cops end |
#defer_corrections ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
When set to true, the corrected source is not written back to the
inspected file; it is exposed through #updated_source instead.
74 75 76 |
# File 'lib/rubocop/cop/team.rb', line 74 def defer_corrections @defer_corrections end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
69 70 71 |
# File 'lib/rubocop/cop/team.rb', line 69 def errors @errors end |
#standby_registry=(value) ⇒ Object (writeonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Registry used to mobilize cops that were not instantiated because they are disabled in the config, when a comment directive opts them back in for a file.
85 86 87 |
# File 'lib/rubocop/cop/team.rb', line 85 def standby_registry=(value) @standby_registry = value end |
#updated_source ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The corrected source of the last investigation, if corrections were
made with #defer_corrections enabled.
79 80 81 |
# File 'lib/rubocop/cop/team.rb', line 79 def updated_source @updated_source end |
#updated_source_file ⇒ Object (readonly) Also known as: updated_source_file?
Returns the value of attribute updated_source_file.
69 70 71 |
# File 'lib/rubocop/cop/team.rb', line 69 def updated_source_file @updated_source_file end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
69 70 71 |
# File 'lib/rubocop/cop/team.rb', line 69 def warnings @warnings end |
Class Method Details
.forces_for(cops) ⇒ Array<Force>
Returns needed for the given cops.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rubocop/cop/team.rb', line 55 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.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rubocop/cop/team.rb', line 31 def self.mobilize(cop_classes, config, = {}) if cop_classes.is_a?(Registry) # Instantiate only the enabled cops so that disabled lazy-loaded cops are not loaded. # The registry is kept on standby to mobilize a disabled cop when a comment directive # opts it back in. cops = cop_classes.enabled(config).map { |cop_class| cop_class.new(config, ) } team = new(cops, config, ) team.standby_registry = cop_classes team else new(mobilize_cops(cop_classes, config, ), config, ) end end |
.mobilize_cops(cop_classes, config, options = {}) ⇒ Array<Cop::Base>
46 47 48 49 50 51 52 |
# File 'lib/rubocop/cop/team.rb', line 46 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
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rubocop/cop/team.rb', line 18 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 |
Instance Method Details
#autocorrect? ⇒ Boolean
102 103 104 |
# File 'lib/rubocop/cop/team.rb', line 102 def autocorrect? @options[:autocorrect] end |
#debug? ⇒ Boolean
106 107 108 |
# File 'lib/rubocop/cop/team.rb', line 106 def debug? @options[:debug] end |
#external_dependency_checksum ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/rubocop/cop/team.rb', line 152 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 ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/rubocop/cop/team.rb', line 144 def forces warn Rainbow(<<~WARNING).yellow, uplevel: 1 `forces` is deprecated. WARNING @forces ||= self.class.forces_for(cops) end |
#inspect_file(processed_source) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/rubocop/cop/team.rb', line 112 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
121 122 123 124 125 |
# File 'lib/rubocop/cop/team.rb', line 121 def investigate(processed_source, offset: 0, original: processed_source) result = investigate_with_corrector(processed_source, offset: offset, original: original) autocorrect(processed_source, result.corrector) result.report end |
#investigate_fragments(fragments, original:) ⇒ Array<Offense>
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rubocop/cop/team.rb', line 128 def investigate_fragments(fragments, original:) @updated_source_file = false offenses, errors, warnings, corrector = fragments.each_with_object([[], [], [], nil]) do |fragment, data| investigate_fragment(fragment, original, data) end autocorrect(original, corrector) @errors = errors @warnings = warnings offenses end |