Class: Rubocop::Cop::Cop
- Inherits:
-
Object
- Object
- Rubocop::Cop::Cop
- 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.
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
-
.config ⇒ Object
Returns the value of attribute config.
Instance Attribute Summary collapse
-
#autocorrect ⇒ Object
Returns the value of attribute autocorrect.
-
#corrections ⇒ Object
readonly
Returns the value of attribute corrections.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#disabled_lines ⇒ Object
writeonly
Sets the attribute disabled_lines.
-
#offences ⇒ Object
Returns the value of attribute offences.
Class Method Summary collapse
- .all ⇒ Object
- .cop_name ⇒ Object
- .cop_type ⇒ Object
- .inherited(subclass) ⇒ Object
- .lint? ⇒ Boolean
- .rails? ⇒ Boolean
- .style? ⇒ Boolean
Instance Method Summary collapse
- #add_offence(severity, location, message) ⇒ Object
- #autocorrect_action(node, *args) ⇒ Object
- #do_autocorrect(node, *args) ⇒ Object
- #ignore_node(node) ⇒ Object
-
#initialize ⇒ Cop
constructor
A new instance of Cop.
- #name ⇒ Object
Constructor Details
#initialize ⇒ Cop
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
.config ⇒ Object
Returns the value of attribute config.
37 38 39 |
# File 'lib/rubocop/cop/cop.rb', line 37 def config @config end |
Instance Attribute Details
#autocorrect ⇒ Object
Returns the value of attribute autocorrect.
29 30 31 |
# File 'lib/rubocop/cop/cop.rb', line 29 def autocorrect @autocorrect end |
#corrections ⇒ Object (readonly)
Returns the value of attribute corrections.
31 32 33 |
# File 'lib/rubocop/cop/cop.rb', line 31 def corrections @corrections end |
#debug ⇒ Object
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
30 31 32 |
# File 'lib/rubocop/cop/cop.rb', line 30 def disabled_lines=(value) @disabled_lines = value end |
#offences ⇒ Object
Returns the value of attribute offences.
27 28 29 |
# File 'lib/rubocop/cop/cop.rb', line 27 def offences @offences end |
Class Method Details
.all ⇒ Object
40 41 42 |
# File 'lib/rubocop/cop/cop.rb', line 40 def self.all @all.clone end |
.cop_name ⇒ Object
48 49 50 |
# File 'lib/rubocop/cop/cop.rb', line 48 def self.cop_name name.to_s.split('::').last end |
.cop_type ⇒ Object
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
60 61 62 |
# File 'lib/rubocop/cop/cop.rb', line 60 def self.lint? cop_type == :lint end |
.rails? ⇒ Boolean
64 65 66 |
# File 'lib/rubocop/cop/cop.rb', line 64 def self.rails? cop_type == :rails end |
.style? ⇒ 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, ) unless @disabled_lines && @disabled_lines.include?(location.line) = debug ? "#{name}: #{}" : @offences << Offence.new(severity, location, , 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 |
#name ⇒ Object
90 91 92 |
# File 'lib/rubocop/cop/cop.rb', line 90 def name self.class.cop_name end |