Class: Rubocop::Cop::Commissioner

Inherits:
Parser::AST::Processor
  • Object
show all
Defined in:
lib/rubocop/cop/commissioner.rb

Overview

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

Constant Summary collapse

METHODS_NOT_DEFINED_IN_PARSER_PROCESSOR =
[
  :on_sym, :on_str, :on_int, :on_float
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cops, options = {}) ⇒ Commissioner

Returns a new instance of Commissioner.



31
32
33
34
35
# File 'lib/rubocop/cop/commissioner.rb', line 31

def initialize(cops, options = {})
  @cops = cops
  @options = options
  reset_errors
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/rubocop/cop/commissioner.rb', line 8

def errors
  @errors
end

Class Method Details

.call_super(callback) ⇒ Object

Methods that are not defined in Parser::AST::Processor won't have a super to call. So we should not attempt to invoke super when defining them.



23
24
25
26
27
28
29
# File 'lib/rubocop/cop/commissioner.rb', line 23

def self.call_super(callback)
  if METHODS_NOT_DEFINED_IN_PARSER_PROCESSOR.include?(callback)
    ''
  else
    'super'
  end
end

.callback_methodsObject



14
15
16
17
18
# File 'lib/rubocop/cop/commissioner.rb', line 14

def self.callback_methods
  Parser::AST::Processor.instance_methods.select do |method|
    method.to_s =~ /^on_/
  end + METHODS_NOT_DEFINED_IN_PARSER_PROCESSOR
end

Instance Method Details

#investigate(processed_source) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/commissioner.rb', line 52

def investigate(processed_source)
  reset_errors
  prepare(processed_source)
  invoke_cops_callback(processed_source)
  process(processed_source.ast) if processed_source.ast
  @cops.each_with_object([]) do |cop, offenses|
    filename = processed_source.buffer.name
    # ignore files that are of no interest to the cop in question
    offenses.concat(cop.offenses) if cop.relevant_file?(filename)
  end
end