lib/rubocop/cop/cop.rb in rubocop-0.6.1 vs lib/rubocop/cop/cop.rb in rubocop-0.7.0

- old
+ new

@@ -30,10 +30,11 @@ end end class Cop attr_accessor :offences + attr_accessor :debug attr_writer :correlations, :disabled_lines @all = [] @config = {} @@ -44,27 +45,32 @@ def self.inherited(subclass) all << subclass end + def self.cop_name + name.to_s.split('::').last + end + def initialize @offences = [] + @debug = false end def has_report? !@offences.empty? end def add_offence(severity, line_number, message) unless @disabled_lines && @disabled_lines.include?(line_number) - message = $options[:debug] ? "#{name}: #{message}" : message + message = debug ? "#{name}: #{message}" : message @offences << Offence.new(severity, line_number, message) end end def name - self.class.to_s.split('::')[-1] + self.class.cop_name end private def each_parent_of(sym, sexp) @@ -105,9 +111,31 @@ end def all_positions(sexp) return [sexp[2]] if sexp[0] =~ /^@/ sexp.grep(Array).reduce([]) { |a, e| a + all_positions(e) } + end + + def keywords(tokens) + # we need to keep track of the previous token to avoid + # interpreting :some_keyword as the keyword some_keyword + prev = Token.new(0, :init, '') + # same goes for defs so we need to track those as well + keywords = [] + + tokens.each do |t| + keywords << t if prev.type != :on_symbeg && t.type == :on_kw + # def with name that's a kw confuses Ripper.lex + penultimate = keywords[-2] + keywords.pop if penultimate && penultimate.text == 'def' + prev = t + end + + keywords + end + + def each_keyword(keyword, tokens) + keywords(tokens).select { |t| t.text == keyword }.each { |t| yield t } end end end end