lib/rubocop/cop/cop.rb in rubocop-0.9.1 vs lib/rubocop/cop/cop.rb in rubocop-0.10.0

- old
+ new

@@ -1,34 +1,36 @@ # encoding: utf-8 module Rubocop module Cop - # A basic wrapper around Parser's tokens. - class Token - attr_reader :pos, :type, :text - - def initialize(pos, type, text) - @pos, @type, @text = pos, type, text - end - - def to_s - "[[#{@pos.line}, #{@pos.column}], #{@type}, #{@text.inspect}]" - end - end - # A scaffold for concrete cops. # # The Cop class is meant to be extended. # # Cops track offences and can autocorrect them of the fly. - class Cop < Parser::Rewriter + # + # 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 + class Cop extend AST::Sexp attr_accessor :offences attr_accessor :debug attr_accessor :autocorrect attr_writer :disabled_lines + attr_reader :corrections @all = [] @config = {} class << self @@ -65,36 +67,21 @@ def initialize @offences = [] @debug = false @autocorrect = false + @ignored_nodes = [] + @corrections = [] end - def inspect(source_buffer, source, tokens, ast, comments) - if autocorrect - filename = source_buffer.instance_variable_get(:@name) - new_source = rewrite(source_buffer, ast) - unless new_source == source_buffer.source - File.open(filename, 'w') { |f| f.write(new_source) } - source_buffer.instance_variable_set(:@source, nil) - source_buffer.read - end - else - process(ast) - end - end - def do_autocorrect(node) autocorrect_action(node) if autocorrect end def autocorrect_action(node) end - def ignore_node(node) - end - def add_offence(severity, location, message) unless @disabled_lines && @disabled_lines.include?(location.line) message = debug ? "#{name}: #{message}" : message @offences << Offence.new(severity, location, message, name) end @@ -102,10 +89,30 @@ def name self.class.cop_name end + def ignore_node(node) + @ignored_nodes << node + end + private + + 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 && + ignored_node.loc.expression.end_pos >= expression.end_pos + return true + end + end + + false + end + + def ignored_node?(node) + @ignored_nodes.include?(node) + end def on_node(syms, sexp, excludes = []) yield sexp if Array(syms).include?(sexp.type) return if Array(excludes).include?(sexp.type)