Class: Rubocop::Cop::Cop
- Inherits:
-
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.
Direct Known Subclasses
Lint::AssignmentInCondition, Lint::BlockAlignment, Lint::EmptyEnsure, Lint::EndAlignment, Lint::EndInMethod, Lint::EnsureReturn, Lint::Eval, Lint::HandleExceptions, Lint::LiteralInCondition, Lint::Loop, Lint::ParenthesesAsGroupedExpression, Lint::RescueException, Lint::ShadowingOuterLocalVariable, Lint::UnreachableCode, Lint::UselessAssignment, Lint::UselessComparison, Lint::UselessSetterCall, Lint::Void, Rails::HasAndBelongsToMany, Rails::ReadAttribute, Rails::Validation, Style::AccessControl, Style::Alias, Style::AlignArray, Style::AlignHash, Style::AlignParameters, Style::AndOr, Style::AsciiComments, Style::AsciiIdentifiers, Style::Attr, Style::BeginBlock, Style::BlockComments, Style::BlockNesting, Style::Blocks, Style::CaseEquality, Style::CaseIndentation, Style::CharacterLiteral, Style::ClassAndModuleCamelCase, Style::ClassMethods, Style::ClassVars, 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::EvenOdd, Style::FavorJoin, Style::FavorSprintf, Style::FavorUnlessOverNegatedIf, Style::FavorUntilOverNegatedWhile, Style::FinalNewline, Style::For, Style::GlobalVars, Style::HashMethods, Style::HashSyntax, Style::IfUnlessModifier, Style::IfWithSemicolon, Style::IndentationWidth, Style::Lambda, Style::LambdaCall, Style::LeadingCommentSpace, Style::LineLength, Style::MethodAndVariableSnakeCase, Style::MethodCallParentheses, Style::MethodLength, Style::ModuleFunction, Style::MultilineBlockChain, Style::MultilineIfThen, Style::MultilineTernaryOperator, Style::NestedTernaryOperator, Style::NilComparison, Style::Not, Style::NumericLiterals, Style::OneLineConditional, Style::OpMethod, Style::ParameterLists, Style::ParenthesesAroundCondition, Style::PerlBackrefs, 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::SpaceAfterMethodName, Style::SpaceAfterSemicolon, Style::SpaceAroundBlockBraces, Style::SpaceAroundEqualsInParameterDefault, Style::SpaceAroundOperators, Style::SpaceBeforeModifierKeyword, Style::SpaceInsideBrackets, Style::SpaceInsideHashLiteralBraces, Style::SpaceInsideParens, Style::SpecialGlobalVars, Style::StringLiterals, Style::SymbolArray, Style::SymbolName, Style::Tab, Style::TrailingBlankLines, Style::TrailingWhitespace, Style::TrivialAccessors, Style::UnlessElse, Style::VariableInterpolation, Style::WhenThen, Style::WhileUntilDo, Style::WhileUntilModifier, Style::WordArray
Constant Summary
collapse
- OPERATOR_METHODS =
%w(
| ^ & <=> == === =~ > >= < <= << >>
+ - * / % ** ~ +@ -@ [] []= ! != !~
).map(&:to_sym) + [:'`']
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config = nil, options = nil) ⇒ Cop
Returns a new instance of Cop.
82
83
84
85
86
87
88
89
|
# File 'lib/rubocop/cop/cop.rb', line 82
def initialize(config = nil, options = nil)
@config = config || Config.new
@options = options || { autocorrect: false, debug: false }
@offences = []
@corrections = []
@ignored_nodes = []
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
49
50
51
|
# File 'lib/rubocop/cop/cop.rb', line 49
def config
@config
end
|
#corrections ⇒ Object
Returns the value of attribute corrections.
49
50
51
|
# File 'lib/rubocop/cop/cop.rb', line 49
def corrections
@corrections
end
|
#offences ⇒ Object
Returns the value of attribute offences.
49
50
51
|
# File 'lib/rubocop/cop/cop.rb', line 49
def offences
@offences
end
|
#processed_source ⇒ Object
50
51
52
|
# File 'lib/rubocop/cop/cop.rb', line 50
def processed_source
@processed_source
end
|
Class Method Details
.all ⇒ Object
54
55
56
|
# File 'lib/rubocop/cop/cop.rb', line 54
def self.all
@all.clone
end
|
.cop_name ⇒ Object
62
63
64
|
# File 'lib/rubocop/cop/cop.rb', line 62
def self.cop_name
name.to_s.split('::').last
end
|
.cop_type ⇒ Object
66
67
68
|
# File 'lib/rubocop/cop/cop.rb', line 66
def self.cop_type
name.to_s.split('::')[-2].downcase.to_sym
end
|
.inherited(subclass) ⇒ Object
58
59
60
|
# File 'lib/rubocop/cop/cop.rb', line 58
def self.inherited(subclass)
@all << subclass
end
|
.lint? ⇒ Boolean
74
75
76
|
# File 'lib/rubocop/cop/cop.rb', line 74
def self.lint?
cop_type == :lint
end
|
.rails? ⇒ Boolean
78
79
80
|
# File 'lib/rubocop/cop/cop.rb', line 78
def self.rails?
cop_type == :rails
end
|
.style? ⇒ Boolean
70
71
72
|
# File 'lib/rubocop/cop/cop.rb', line 70
def self.style?
cop_type == :style
end
|
Instance Method Details
#add_offence(severity, node, loc, message = nil) ⇒ Object
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/rubocop/cop/cop.rb', line 110
def add_offence(severity, node, loc, message = nil)
location = loc.is_a?(Symbol) ? node.loc.send(loc) : loc
unless disabled_line?(location.line)
message = message ? message : message(node)
message = debug? ? "#{name}: #{message}" : message
@offences << Offence.new(severity, location, message, name)
autocorrect_action(node) if autocorrect?
end
end
|
#autocorrect? ⇒ Boolean
95
96
97
|
# File 'lib/rubocop/cop/cop.rb', line 95
def autocorrect?
@options[:autocorrect]
end
|
#autocorrect_action(node) ⇒ Object
103
104
|
# File 'lib/rubocop/cop/cop.rb', line 103
def autocorrect_action(node)
end
|
#convention(node, location, message = nil) ⇒ Object
121
122
123
|
# File 'lib/rubocop/cop/cop.rb', line 121
def convention(node, location, message = nil)
add_offence(:convention, node, location, message)
end
|
#cop_config ⇒ Object
91
92
93
|
# File 'lib/rubocop/cop/cop.rb', line 91
def cop_config
@config.for_cop(self)
end
|
#cop_name ⇒ Object
Also known as:
name
129
130
131
|
# File 'lib/rubocop/cop/cop.rb', line 129
def cop_name
self.class.cop_name
end
|
#debug? ⇒ Boolean
99
100
101
|
# File 'lib/rubocop/cop/cop.rb', line 99
def debug?
@options[:debug]
end
|
#ignore_node(node) ⇒ Object
135
136
137
|
# File 'lib/rubocop/cop/cop.rb', line 135
def ignore_node(node)
@ignored_nodes << node
end
|
#message(node = nil) ⇒ Object
106
107
108
|
# File 'lib/rubocop/cop/cop.rb', line 106
def message(node = nil)
self.class::MSG
end
|
#warning(node, location, message = nil) ⇒ Object
125
126
127
|
# File 'lib/rubocop/cop/cop.rb', line 125
def warning(node, location, message = nil)
add_offence(:warning, node, location, message)
end
|