lib/rubocop/cop/cop.rb in rubocop-0.8.3 vs lib/rubocop/cop/cop.rb in rubocop-0.9.0

- old
+ new

@@ -1,9 +1,10 @@ # 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 @@ -12,15 +13,21 @@ def to_s "[[#{@pos.line}, #{@pos.column}], #{@type}, #{@text.inspect}]" end end - class Cop < Parser::AST::Processor + # 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 extend AST::Sexp attr_accessor :offences attr_accessor :debug + attr_accessor :autocorrect attr_writer :disabled_lines @all = [] @config = {} @@ -35,30 +42,48 @@ def self.cop_name name.to_s.split('::').last end + def self.rails? + false + end + def initialize @offences = [] @debug = false + @autocorrect = false end - def has_report? - !@offences.empty? + 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 inspect(source, tokens, ast, comments) - process(ast) + def do_autocorrect(node) + autocorrect_action(node) if autocorrect end + def autocorrect_action(node) + end + def ignore_node(node) end - def add_offence(severity, line_number, message) - unless @disabled_lines && @disabled_lines.include?(line_number) + def add_offence(severity, location, message) + unless @disabled_lines && @disabled_lines.include?(location.line) message = debug ? "#{name}: #{message}" : message - @offences << Offence.new(severity, line_number, message) + @offences << Offence.new(severity, location, message, name) end end def name self.class.cop_name @@ -74,9 +99,28 @@ sexp.children.each do |elem| if elem.is_a?(Parser::AST::Node) on_node(syms, elem, excludes) { |s| yield s } end end + end + + def command?(name, node) + return unless node.type == :send + + receiver, method_name, _args = *node + + # commands have no explicit receiver + !receiver && method_name == name + end + + def source_range(source_buffer, preceding_lines, begin_column, + column_count) + newline_length = 1 + begin_pos = preceding_lines.reduce(0) do |a, e| + a + e.length + newline_length + end + begin_column + Parser::Source::Range.new(source_buffer, begin_pos, + begin_pos + column_count) end end end end