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).

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.


17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/cop/team.rb', line 17

def initialize(cops, config = nil, options = {})
  @cops = cops
  @config = config
  @options = options
  reset
  @ready = true

  validate_config
end

Instance Attribute Details

#copsObject (readonly)

Returns the value of attribute cops


13
14
15
# File 'lib/rubocop/cop/team.rb', line 13

def cops
  @cops
end

#errorsObject (readonly)

Returns the value of attribute errors


13
14
15
# File 'lib/rubocop/cop/team.rb', line 13

def errors
  @errors
end

#updated_source_fileObject (readonly) Also known as: updated_source_file?

Returns the value of attribute updated_source_file


13
14
15
# File 'lib/rubocop/cop/team.rb', line 13

def updated_source_file
  @updated_source_file
end

#warningsObject (readonly)

Returns the value of attribute warnings


13
14
15
# File 'lib/rubocop/cop/team.rb', line 13

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


99
100
101
102
103
104
105
106
107
108
# File 'lib/rubocop/cop/team.rb', line 99

def self.forces_for(cops)
  needed = Hash.new { |h, k| h[k] = [] }
  cops.each do |cop|
    Array(cop.class.joining_forces).each { |force| needed[force] << cop }
  end

  needed.map do |force_class, joining_cops|
    force_class.new(joining_cops)
  end
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`


36
37
38
39
# File 'lib/rubocop/cop/team.rb', line 36

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::Cop>

Returns:


42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/team.rb', line 42

def self.mobilize_cops(cop_classes, config, options = {})
  cop_classes = Registry.new(cop_classes.to_a) unless cop_classes.is_a?(Registry)
  only = options.fetch(:only, [])
  safe = options.fetch(:safe, false)
  cop_classes.enabled(config, only, only_safe: safe).map do |cop_class|
    cop_class.new(config, options)
  end
end

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

Returns:


28
29
30
31
32
33
# File 'lib/rubocop/cop/team.rb', line 28

def self.new(cop_or_classes, config, options = {})
  # Support v0 api:
  return mobilize(cop_or_classes, config, options) if cop_or_classes.first.is_a?(Class)

  super
end

Instance Method Details

#autocorrect?Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/rubocop/cop/team.rb', line 51

def autocorrect?
  @options[:auto_correct]
end

#debug?Boolean

Returns:

  • (Boolean)

55
56
57
# File 'lib/rubocop/cop/team.rb', line 55

def debug?
  @options[:debug]
end

#external_dependency_checksumObject


110
111
112
113
# File 'lib/rubocop/cop/team.rb', line 110

def external_dependency_checksum
  keys = cops.map(&:external_dependency_checksum).compact
  Digest::SHA1.hexdigest(keys.join)
end

#forcesObject

Deprecated.

94
95
96
# File 'lib/rubocop/cop/team.rb', line 94

def forces
  @forces ||= self.class.forces_for(cops)
end

#inspect_file(processed_source) ⇒ Object


61
62
63
# File 'lib/rubocop/cop/team.rb', line 61

def inspect_file(processed_source)
  investigate(processed_source).offenses
end

#investigate(processed_source) ⇒ Commissioner::InvestigationReport


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubocop/cop/team.rb', line 66

def investigate(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 auto-correcting cops by themselves, and only
  # run the other cops when no corrections are left
  on_duty = roundup_relevant_cops(processed_source.file_path)

  autocorrect_cops, other_cops = on_duty.partition(&:autocorrect?)

  report = investigate_partial(autocorrect_cops, processed_source)

  unless autocorrect(processed_source, report)
    # 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))
  end

  process_errors(processed_source.path, report.errors)

  report
ensure
  @ready = false
end