Skip to content

Commit

Permalink
Reduce complexity in Team
Browse files Browse the repository at this point in the history
Split `inspect_file` into smaller methods.
  • Loading branch information
lumeet committed Jun 22, 2016
1 parent 0a89d45 commit 3c425ff
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions lib/rubocop/cop/team.rb
Expand Up @@ -19,6 +19,8 @@ class Team
debug: false
}.freeze

Investigation = Struct.new(:offenses, :errors)

attr_reader :errors, :warnings, :updated_source_file

alias updated_source_file? updated_source_file
Expand Down Expand Up @@ -47,32 +49,7 @@ def inspect_file(processed_source)
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
offenses(processed_source)
end

def cops
Expand All @@ -95,6 +72,40 @@ def forces_for(cops)

private

def offenses(processed_source)
# 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?)

autocorrect =
investigate(autocorrect_cops, processed_source) do |offenses|
# 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 if autocorrect(processed_source.buffer,
autocorrect_cops)
end

other = investigate(other_cops, processed_source)

errors = autocorrect.errors.merge(other.errors)
process_commissioner_errors(processed_source.path, errors)

autocorrect.offenses.concat(other.offenses)
end

def investigate(cops, processed_source)
return Investigation.new([], {}) if cops.empty?

commissioner = Commissioner.new(cops, forces_for(cops))
offenses = commissioner.investigate(processed_source)
yield offenses if block_given?

Investigation.new(offenses, commissioner.errors)
end

def cop_enabled?(cop_class)
@config.cop_enabled?(cop_class) ||
(@options[:only] || []).include?(cop_class.cop_name)
Expand Down

0 comments on commit 3c425ff

Please sign in to comment.