Parent

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Cop

A scaffold for concrete cops.

The Cop class is meant to be extended.

Cops track offenses and can autocorrect them of the fly.

A commissioner object is responsible for traversing the AST and invoking the specific callbacks on each cop. If a cop needs to do its own processing of the AST or depends on something else, it should define the `investigate` method and do the processing there.

@example

class CustomCop < Cop
  def investigate(processed_source)
    # Do custom processing
  end
end

Attributes

config[R]
corrections[R]
offenses[R]
processed_source[RW]

Public Class Methods

all() click to toggle source
# File lib/rubocop/cop/cop.rb, line 55
def self.all
  @all.clone
end
cop_name() click to toggle source
# File lib/rubocop/cop/cop.rb, line 80
def self.cop_name
  @cop_name ||= name.to_s.split('::').last(2).join('/')
end
cop_type() click to toggle source
# File lib/rubocop/cop/cop.rb, line 84
def self.cop_type
  name.to_s.split('::')[-2].downcase.to_sym
end
inherited(subclass) click to toggle source
# File lib/rubocop/cop/cop.rb, line 76
def self.inherited(subclass)
  @all << subclass
end
lint?() click to toggle source
# File lib/rubocop/cop/cop.rb, line 88
def self.lint?
  cop_type == :lint
end
new(config = nil, options = nil) click to toggle source
# File lib/rubocop/cop/cop.rb, line 96
def initialize(config = nil, options = nil)
  @config = config || Config.new
  @options = options || { auto_correct: false, debug: false }

  @offenses = []
  @corrections = []
end
non_rails() click to toggle source
# File lib/rubocop/cop/cop.rb, line 72
def self.non_rails
  @all.without_type(:rails)
end
qualified_cop_name(name, origin) click to toggle source
# File lib/rubocop/cop/cop.rb, line 59
def self.qualified_cop_name(name, origin)
  return name if name.include?('/')
  found_ns = @all.types.map(&:capitalize).select do |ns|
    @all.map(&:cop_name).include?("#{ns}/#{name}")
  end

  case found_ns.size
  when 0 then name # No namespace found. Deal with it later in caller.
  when 1 then "#{found_ns.first}/#{name}"
  else fail AmbiguousCopName, "`#{name}` used in #{origin}"
  end
end
rails?() click to toggle source
# File lib/rubocop/cop/cop.rb, line 92
def self.rails?
  cop_type == :rails
end

Public Instance Methods

add_offense(node, loc, message = nil, severity = nil) click to toggle source
# File lib/rubocop/cop/cop.rb, line 132
def add_offense(node, loc, message = nil, severity = nil)
  location = loc.is_a?(Symbol) ? node.loc.send(loc) : loc

  return unless enabled_line?(location.line)

  # Don't include the same location twice for one cop.
  return if @offenses.find { |o| o.location == location }

  severity = custom_severity || severity || default_severity

  message ||= message(node)
  message = display_cop_names? ? "#{name}: #{message}" : message

  corrected = begin
                autocorrect(node) if autocorrect?
                autocorrect?
              rescue CorrectionNotPossible
                false
              end
  @offenses << Offense.new(severity, location, message, name, corrected)
  yield if block_given?
end
autocorrect?() click to toggle source
# File lib/rubocop/cop/cop.rb, line 112
def autocorrect?
  @options[:auto_correct] && support_autocorrect?
end
config_to_allow_offenses() click to toggle source
# File lib/rubocop/cop/cop.rb, line 155
def config_to_allow_offenses
  Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name]
end
config_to_allow_offenses=(hash) click to toggle source
# File lib/rubocop/cop/cop.rb, line 159
def config_to_allow_offenses=(hash)
  Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] =
    hash
end
cop_config() click to toggle source
# File lib/rubocop/cop/cop.rb, line 108
def cop_config
  @config.for_cop(self)
end
cop_name() click to toggle source
# File lib/rubocop/cop/cop.rb, line 164
def cop_name
  self.class.cop_name
end
Also aliased as: name
debug?() click to toggle source
# File lib/rubocop/cop/cop.rb, line 116
def debug?
  @options[:debug]
end
display_cop_names?() click to toggle source
# File lib/rubocop/cop/cop.rb, line 120
def display_cop_names?
  debug? || @options[:display_cop_names]
end
exclude_file?(file) click to toggle source
# File lib/rubocop/cop/cop.rb, line 174
def exclude_file?(file)
  file_name_matches_any?(file, 'Exclude', false)
end
include_file?(file) click to toggle source
# File lib/rubocop/cop/cop.rb, line 170
def include_file?(file)
  file_name_matches_any?(file, 'Include', true)
end
join_force?(_force_class) click to toggle source
# File lib/rubocop/cop/cop.rb, line 104
def join_force?(_force_class)
  false
end
message(_node = nil) click to toggle source
# File lib/rubocop/cop/cop.rb, line 124
def message(_node = nil)
  self.class::MSG
end
name() click to toggle source
Alias for: cop_name
relevant_file?(file) click to toggle source
# File lib/rubocop/cop/cop.rb, line 178
def relevant_file?(file)
  include_file?(file) && !exclude_file?(file)
end
support_autocorrect?() click to toggle source
# File lib/rubocop/cop/cop.rb, line 128
def support_autocorrect?
  respond_to?(:autocorrect, true)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.