class Rubocop::Cop::Cop
A scaffold for concrete cops.
The Cop
class is meant to be extended.
Cops track offences and can autocorrect them of the fly.
Attributes
all[RW]
config[RW]
autocorrect[RW]
debug[RW]
disabled_lines[W]
offences[RW]
Public Class Methods
cop_name()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 43 def self.cop_name name.to_s.split('::').last end
cop_type()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 47 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 39 def self.inherited(subclass) all << subclass end
lint?()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 55 def self.lint? cop_type == :lint end
new()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 63 def initialize @offences = [] @debug = false @autocorrect = false end
rails?()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 59 def self.rails? cop_type == :rails end
style?()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 51 def self.style? cop_type == :style end
Public Instance Methods
add_offence(severity, location, message)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 93 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 end
autocorrect_action(node)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 87 def autocorrect_action(node) end
do_autocorrect(node)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 83 def do_autocorrect(node) autocorrect_action(node) if autocorrect end
ignore_node(node)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 90 def ignore_node(node) end
inspect(source_buffer, source, tokens, ast, comments)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 69 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
name()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 100 def name self.class.cop_name end
Private Instance Methods
command?(name, node)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 118 def command?(name, node) return unless node.type == :send receiver, method_name, _args = *node # commands have no explicit receiver !receiver && method_name == name end
on_node(syms, sexp, excludes = []) { |sexp| ... }
click to toggle source
# File lib/rubocop/cop/cop.rb, line 106 def on_node(syms, sexp, excludes = []) yield sexp if Array(syms).include?(sexp.type) return if Array(excludes).include?(sexp.type) sexp.children.each do |elem| if elem.is_a?(Parser::AST::Node) on_node(syms, elem, excludes) { |s| yield s } end end end
source_range(source_buffer, preceding_lines, begin_column, column_count)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 127 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