Class: Rubocop::Cop::Cop

Inherits:
Object
  • Object
show all
Extended by:
AST::Sexp
Defined in:
lib/rubocop/cop/cop.rb

Overview

A scaffold for concrete cops.

The Cop class is meant to be extended.

Cops track offences and can autocorrect them of the fly.

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.

Examples:


class CustomCop < Cop
  def investigate(processed_source)
    # Do custom processing
  end
end

Direct Known Subclasses

Lint::AssignmentInCondition, Lint::BlockAlignment, Lint::EmptyEnsure, Lint::EndAlignment, Lint::EndInMethod, Lint::EnsureReturn, Lint::Eval, Lint::HandleExceptions, Lint::LiteralInCondition, Lint::Loop, Lint::RescueException, Lint::ShadowingOuterLocalVariable, Lint::UnreachableCode, Lint::UnusedLocalVariable, Lint::UselessAssignment, Lint::UselessComparison, Lint::Void, Rails::Validation, Style::AccessControl, Style::Alias, Style::AlignParameters, Style::AndOr, Style::AsciiComments, Style::AsciiIdentifiers, Style::Attr, Style::AvoidClassVars, Style::AvoidFor, Style::AvoidGlobalVars, Style::AvoidPerlBackrefs, Style::AvoidPerlisms, Style::BeginBlock, Style::BlockComments, Style::BlockNesting, Style::Blocks, Style::CaseEquality, Style::CaseIndentation, Style::CharacterLiteral, Style::ClassAndModuleCamelCase, Style::ClassMethods, Style::CollectionMethods, Style::ColonMethodCall, Style::CommentAnnotation, Style::ConstantName, Style::DefWithParentheses, Style::DefWithoutParentheses, Style::Documentation, Style::DotPosition, Style::EmptyLineBetweenDefs, Style::EmptyLines, Style::EmptyLiteral, Style::Encoding, Style::EndBlock, Style::EndOfLine, Style::FavorJoin, Style::FavorSprintf, Style::FavorUnlessOverNegatedIf, Style::FavorUntilOverNegatedWhile, Style::HashSyntax, Style::IfUnlessModifier, Style::IfWithSemicolon, Style::Lambda, Style::LeadingCommentSpace, Style::LineContinuation, Style::LineLength, Style::MethodAndVariableSnakeCase, Style::MethodCallParentheses, Style::MethodLength, Style::ModuleFunction, Style::MultilineIfThen, Style::MultilineTernaryOperator, Style::NestedTernaryOperator, Style::Not, Style::NumericLiterals, Style::OneLineConditional, Style::OpMethod, Style::ParameterLists, Style::ParenthesesAroundCondition, Style::Proc, Style::ReduceArguments, Style::RedundantBegin, Style::RedundantReturn, Style::RedundantSelf, Style::RegexpLiteral, Style::RescueModifier, Style::Semicolon, Style::SignalException, Style::SingleLineMethods, Style::SpaceAfterColon, Style::SpaceAfterComma, Style::SpaceAfterControlKeyword, Style::SpaceAfterSemicolon, Style::SpaceAroundBraces, Style::SpaceAroundEqualsInParameterDefault, Style::SpaceAroundOperators, Style::SpaceInsideBrackets, Style::SpaceInsideHashLiteralBraces, Style::SpaceInsideParens, Style::StringLiterals, Style::SymbolArray, Style::SymbolName, Style::Tab, Style::TrailingWhitespace, Style::TrivialAccessors, Style::UnlessElse, Style::VariableInterpolation, Style::WhenThen, Style::WhileUntilDo, Style::WhileUntilModifier, Style::WordArray

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCop

Returns a new instance of Cop.



68
69
70
71
72
73
74
# File 'lib/rubocop/cop/cop.rb', line 68

def initialize
  @offences = []
  @debug = false
  @autocorrect = false
  @ignored_nodes = []
  @corrections = []
end

Class Attribute Details

.configObject

Returns the value of attribute config.



37
38
39
# File 'lib/rubocop/cop/cop.rb', line 37

def config
  @config
end

Instance Attribute Details

#autocorrectObject

Returns the value of attribute autocorrect.



29
30
31
# File 'lib/rubocop/cop/cop.rb', line 29

def autocorrect
  @autocorrect
end

#correctionsObject (readonly)

Returns the value of attribute corrections.



31
32
33
# File 'lib/rubocop/cop/cop.rb', line 31

def corrections
  @corrections
end

#debugObject

Returns the value of attribute debug.



28
29
30
# File 'lib/rubocop/cop/cop.rb', line 28

def debug
  @debug
end

#disabled_lines=(value) ⇒ Object (writeonly)

Sets the attribute disabled_lines

Parameters:

  • value

    the value to set the attribute disabled_lines to.



30
31
32
# File 'lib/rubocop/cop/cop.rb', line 30

def disabled_lines=(value)
  @disabled_lines = value
end

#offencesObject

Returns the value of attribute offences.



27
28
29
# File 'lib/rubocop/cop/cop.rb', line 27

def offences
  @offences
end

Class Method Details

.allObject



40
41
42
# File 'lib/rubocop/cop/cop.rb', line 40

def self.all
  @all.clone
end

.cop_nameObject



48
49
50
# File 'lib/rubocop/cop/cop.rb', line 48

def self.cop_name
  name.to_s.split('::').last
end

.cop_typeObject



52
53
54
# File 'lib/rubocop/cop/cop.rb', line 52

def self.cop_type
  name.to_s.split('::')[-2].downcase.to_sym
end

.inherited(subclass) ⇒ Object



44
45
46
# File 'lib/rubocop/cop/cop.rb', line 44

def self.inherited(subclass)
  @all << subclass
end

.lint?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/rubocop/cop/cop.rb', line 60

def self.lint?
  cop_type == :lint
end

.rails?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/rubocop/cop/cop.rb', line 64

def self.rails?
  cop_type == :rails
end

.style?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/rubocop/cop/cop.rb', line 56

def self.style?
  cop_type == :style
end

Instance Method Details

#add_offence(severity, location, message) ⇒ Object



83
84
85
86
87
88
# File 'lib/rubocop/cop/cop.rb', line 83

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, *args) ⇒ Object



80
81
# File 'lib/rubocop/cop/cop.rb', line 80

def autocorrect_action(node, *args)
end

#do_autocorrect(node, *args) ⇒ Object



76
77
78
# File 'lib/rubocop/cop/cop.rb', line 76

def do_autocorrect(node, *args)
  autocorrect_action(node, *args) if autocorrect
end

#ignore_node(node) ⇒ Object



94
95
96
# File 'lib/rubocop/cop/cop.rb', line 94

def ignore_node(node)
  @ignored_nodes << node
end

#nameObject



90
91
92
# File 'lib/rubocop/cop/cop.rb', line 90

def name
  self.class.cop_name
end