class RubbyCop::Cop::Commissioner

Commissioner class is responsible for processing the AST and delegating work to the specified cops.

Constants

CopError

Attributes

errors[R]

Public Class Methods

callback_methods() click to toggle source
# File lib/rubbycop/cop/commissioner.rb, line 14
def self.callback_methods
  Parser::Meta::NODE_TYPES.map { |type| :"on_#{type}" }
end
new(cops, forces = [], options = {}) click to toggle source
# File lib/rubbycop/cop/commissioner.rb, line 18
def initialize(cops, forces = [], options = {})
  @cops = cops
  @forces = forces
  @options = options
  @callbacks = {}

  reset_errors
end

Public Instance Methods

investigate(processed_source) click to toggle source
# File lib/rubbycop/cop/commissioner.rb, line 53
def investigate(processed_source)
  reset_errors
  remove_irrelevant_cops(processed_source.buffer.name)
  reset_callbacks
  prepare(processed_source)
  invoke_custom_processing(@cops, processed_source)
  invoke_custom_processing(@forces, processed_source)
  walk(processed_source.ast) if processed_source.ast
  @cops.flat_map(&:offenses)
end

Private Instance Methods

invoke_custom_processing(cops_or_forces, processed_source) click to toggle source

There are cops/forces that require their own custom processing. If they define the investigate method, all input parameters passed to the commissioner will be passed to the cop too in order to do its own processing.

# File lib/rubbycop/cop/commissioner.rb, line 95
def invoke_custom_processing(cops_or_forces, processed_source)
  cops_or_forces.each do |cop|
    next unless cop.respond_to?(:investigate)

    with_cop_error_handling(cop) do
      cop.investigate(processed_source)
    end
  end
end
prepare(processed_source) click to toggle source

TODO: Bad design.

# File lib/rubbycop/cop/commissioner.rb, line 87
def prepare(processed_source)
  @cops.each { |cop| cop.processed_source = processed_source }
end
remove_irrelevant_cops(filename) click to toggle source
# File lib/rubbycop/cop/commissioner.rb, line 70
def remove_irrelevant_cops(filename)
  @cops.reject! { |cop| cop.excluded_file?(filename) }
  @cops.reject! do |cop|
    cop.class.respond_to?(:support_target_ruby_version?) &&
      !cop.class.support_target_ruby_version?(cop.target_ruby_version)
  end
  @cops.reject! do |cop|
    cop.class.respond_to?(:support_target_rails_version?) &&
      !cop.class.support_target_rails_version?(cop.target_rails_version)
  end
end
reset_callbacks() click to toggle source
# File lib/rubbycop/cop/commissioner.rb, line 82
def reset_callbacks
  @callbacks.clear
end
reset_errors() click to toggle source
# File lib/rubbycop/cop/commissioner.rb, line 66
def reset_errors
  @errors = Hash.new { |hash, k| hash[k] = [] }
end
with_cop_error_handling(cop, node = nil) { || ... } click to toggle source
# File lib/rubbycop/cop/commissioner.rb, line 105
def with_cop_error_handling(cop, node = nil)
  yield
rescue => e
  raise e if @options[:raise_error]
  if node
    line = node.loc.line
    column = node.loc.column
  end
  error = CopError.new(e, line, column)
  @errors[cop] << error
end