lib/rubocop/cop/cop.rb in rubocop-0.12.0 vs lib/rubocop/cop/cop.rb in rubocop-0.13.0

- old
+ new

@@ -44,23 +44,15 @@ OPERATOR_METHODS = %w( | ^ & <=> == === =~ > >= < <= << >> + - * / % ** ~ +@ -@ [] []= ! != !~ ).map(&:to_sym) + [:'`'] - attr_accessor :offences - attr_accessor :debug - attr_accessor :autocorrect - attr_writer :disabled_lines - attr_reader :corrections + attr_reader :config, :offences, :corrections + attr_accessor :processed_source # TODO: Bad design. @all = CopStore.new - @config = {} - class << self - attr_accessor :config - end - def self.all @all.clone end def self.inherited(subclass) @@ -81,55 +73,78 @@ def self.lint? cop_type == :lint end - # Extracts the first line out of the description - def self.short_description - desc = full_description - desc ? desc.lines.first.strip : '' - end - - # Gets the full description of the cop or nil if no description is set. - def self.full_description - config['Description'] - end - def self.rails? cop_type == :rails end - def initialize + def initialize(config = nil, options = nil) + @config = config || Config.new + @options = options || { autocorrect: false, debug: false } + @offences = [] - @debug = false - @autocorrect = false - @ignored_nodes = [] @corrections = [] + @ignored_nodes = [] end - def do_autocorrect(node, *args) - autocorrect_action(node, *args) if autocorrect + def cop_config + @config.for_cop(self) end - def autocorrect_action(node, *args) + def autocorrect? + @options[:autocorrect] end - def add_offence(severity, location, message) - unless @disabled_lines && @disabled_lines.include?(location.line) - message = debug ? "#{name}: #{message}" : message + def debug? + @options[:debug] + end + + def autocorrect_action(node) + end + + def message(node = nil) + self.class::MSG + end + + def add_offence(severity, node, loc, message = nil) + location = loc.is_a?(Symbol) ? node.loc.send(loc) : loc + + unless disabled_line?(location.line) + message = message ? message : message(node) + message = debug? ? "#{name}: #{message}" : message @offences << Offence.new(severity, location, message, name) + autocorrect_action(node) if autocorrect? end end - def name + def convention(node, location, message = nil) + add_offence(:convention, node, location, message) + end + + def warning(node, location, message = nil) + add_offence(:warning, node, location, message) + end + + def cop_name self.class.cop_name end + alias_method :name, :cop_name + def ignore_node(node) @ignored_nodes << node end private + + def disabled_line?(line_number) + return false unless @processed_source + disabled_lines = @processed_source.disabled_lines_for_cops[name] + return false unless disabled_lines + disabled_lines.include?(line_number) + end def part_of_ignored_node?(node) expression = node.loc.expression @ignored_nodes.each do |ignored_node| if ignored_node.loc.expression.begin_pos <= expression.begin_pos &&