class RuboCop::Cop::Team
FIXME
Constants
- DEFAULT_OPTIONS
- INCOMPATIBLE_COPS
If these cops try to autocorrect the same file at the same time, bad things are liable to happen
Attributes
errors[R]
updated_source_file[R]
updated_source_file?[R]
warnings[R]
Public Class Methods
new(cop_classes, config, options = nil)
click to toggle source
# File lib/rubocop/cop/team.rb, line 23 def initialize(cop_classes, config, options = nil) @cop_classes = cop_classes @config = config @options = options || DEFAULT_OPTIONS @errors = [] @warnings = [] validate_config end
Public Instance Methods
autocorrect?()
click to toggle source
# File lib/rubocop/cop/team.rb, line 33 def autocorrect? @options[:auto_correct] end
cops()
click to toggle source
# File lib/rubocop/cop/team.rb, line 75 def cops @cops ||= @cop_classes.select { |c| cop_enabled?(c) }.map do |cop_class| cop_class.new(@config, @options) end end
debug?()
click to toggle source
# File lib/rubocop/cop/team.rb, line 37 def debug? @options[:debug] end
forces()
click to toggle source
# File lib/rubocop/cop/team.rb, line 81 def forces @forces ||= forces_for(cops) end
forces_for(cops)
click to toggle source
# File lib/rubocop/cop/team.rb, line 85 def forces_for(cops) Force.all.each_with_object([]) do |force_class, forces| joining_cops = cops.select { |cop| cop.join_force?(force_class) } next if joining_cops.empty? forces << force_class.new(joining_cops) end end
inspect_file(processed_source)
click to toggle source
# File lib/rubocop/cop/team.rb, line 41 def inspect_file(processed_source) # If we got any syntax errors, return only the syntax offenses. unless processed_source.valid_syntax? return Lint::Syntax.offenses_from_processed_source(processed_source) end # 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 autocorrect_cops, other_cops = cops.partition(&:autocorrect?) offenses = [] errors = {} if autocorrect_cops.any? commissioner = Commissioner.new(autocorrect_cops, forces_for(autocorrect_cops)) offenses = commissioner.investigate(processed_source) if autocorrect(processed_source.buffer, autocorrect_cops) # We corrected some errors. Another round of inspection will be # done, and any other offenses will be caught then, so we don't # need to continue. return offenses end errors = commissioner.errors end commissioner = Commissioner.new(other_cops, forces_for(other_cops)) offenses.concat(commissioner.investigate(processed_source)) errors.merge!(commissioner.errors) process_commissioner_errors(processed_source.path, errors) offenses end
Private Instance Methods
autocorrect(buffer, cops)
click to toggle source
# File lib/rubocop/cop/team.rb, line 100 def autocorrect(buffer, cops) @updated_source_file = false return unless autocorrect? new_source = autocorrect_all_cops(buffer, cops) return if new_source == buffer.source if @options[:stdin] # holds source read in from stdin, when --stdin option is used @options[:stdin] = new_source else filename = buffer.name File.open(filename, 'wb') { |f| f.write(new_source) } end @updated_source_file = true end
autocorrect_all_cops(buffer, cops)
click to toggle source
# File lib/rubocop/cop/team.rb, line 118 def autocorrect_all_cops(buffer, cops) corrector = Corrector.new(buffer) skip = Set.new cops.each do |cop| next if cop.corrections.empty? next if skip.include?(cop.class) corrector.corrections.concat(cop.corrections) incompatible = INCOMPATIBLE_COPS[cop.class] skip.merge(incompatible) if incompatible end if !corrector.corrections.empty? corrector.rewrite else buffer.source end end
cop_enabled?(cop_class)
click to toggle source
# File lib/rubocop/cop/team.rb, line 95 def cop_enabled?(cop_class) @config.cop_enabled?(cop_class) || (@options[:only] || []).include?(cop_class.cop_name) end
handle_error(e, message)
click to toggle source
# File lib/rubocop/cop/team.rb, line 165 def handle_error(e, message) @errors << message warn message if debug? puts e.message, e.backtrace else warn 'To see the complete backtrace run rubocop -d.' end end
handle_warning(e, message)
click to toggle source
# File lib/rubocop/cop/team.rb, line 159 def handle_warning(e, message) @warnings << message warn message puts e.backtrace if debug? end
process_commissioner_errors(file, file_errors)
click to toggle source
# File lib/rubocop/cop/team.rb, line 143 def process_commissioner_errors(file, file_errors) file_errors.each do |cop, errors| errors.each do |e| if e.is_a?(Warning) handle_warning(e, Rainbow("#{e.message} (from file: " "#{file})").yellow) else handle_error(e, Rainbow("An error occurred while #{cop.name}" " cop was inspecting #{file}.").red) end end end end
validate_config()
click to toggle source
# File lib/rubocop/cop/team.rb, line 137 def validate_config cops.each do |cop| cop.validate_config if cop.respond_to?(:validate_config) end end