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::Debugger, Lint::EmptyEnsure, Lint::EndAlignment, Lint::EndInMethod, Lint::EnsureReturn, Lint::Eval, Lint::HandleExceptions, Lint::LiteralInCondition, Lint::Loop, Lint::ParenthesesAsGroupedExpression, Lint::RescueException, Lint::ShadowingOuterLocalVariable, Lint::Syntax, Lint::UnreachableCode, Lint::UselessAssignment, Lint::UselessComparison, Lint::UselessSetterCall, Lint::Void, Rails::HasAndBelongsToMany, Rails::Output, Rails::ReadAttribute, Rails::Validation, Style::AccessModifierIndentation, 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::BracesAroundHashParameters, Style::CaseEquality, Style::CaseIndentation, Style::CharacterLiteral, Style::ClassAndModuleCamelCase, Style::ClassLength, Style::ClassMethods, Style::ClassVars, Style::CollectionMethods, Style::ColonMethodCall, Style::CommentAnnotation, Style::ConstantName, Style::CyclomaticComplexity, Style::DefWithParentheses, Style::DefWithoutParentheses, Style::Documentation, Style::DotPosition, Style::EmptyLineBetweenDefs, Style::EmptyLines, Style::EmptyLinesAroundAccessModifier, Style::EmptyLinesAroundBody, 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::MethodCallParentheses, Style::MethodCalledOnDoEndBlock, Style::MethodLength, Style::MethodName, 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::RaiseArgs, Style::ReduceArguments, Style::RedundantBegin, Style::RedundantException, Style::RedundantReturn, Style::RedundantSelf, Style::RegexpLiteral, Style::RescueModifier, Style::Semicolon, Style::SignalException, Style::SingleLineMethods, Style::SpaceAfterColon, Style::SpaceAfterComma, Style::SpaceAfterControlKeyword, Style::SpaceAfterMethodName, Style::SpaceAfterNot, 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::VariableName, 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.
92
93
94
95
96
97
98
99
|
# File 'lib/rubocop/cop/cop.rb', line 92
def initialize(config = nil, options = nil)
@config = config || Config.new
@options = options || { auto_correct: false, debug: false }
@offences = []
@corrections = []
@ignored_nodes = []
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
55
56
57
|
# File 'lib/rubocop/cop/cop.rb', line 55
def config
@config
end
|
#corrections ⇒ Object
Returns the value of attribute corrections.
55
56
57
|
# File 'lib/rubocop/cop/cop.rb', line 55
def corrections
@corrections
end
|
#offences ⇒ Object
Returns the value of attribute offences.
55
56
57
|
# File 'lib/rubocop/cop/cop.rb', line 55
def offences
@offences
end
|
#processed_source ⇒ Object
56
57
58
|
# File 'lib/rubocop/cop/cop.rb', line 56
def processed_source
@processed_source
end
|
Class Method Details
.all ⇒ Object
60
61
62
|
# File 'lib/rubocop/cop/cop.rb', line 60
def self.all
@all.clone
end
|
.cop_name ⇒ Object
72
73
74
|
# File 'lib/rubocop/cop/cop.rb', line 72
def self.cop_name
name.to_s.split('::').last
end
|
.cop_type ⇒ Object
76
77
78
|
# File 'lib/rubocop/cop/cop.rb', line 76
def self.cop_type
name.to_s.split('::')[-2].downcase.to_sym
end
|
.inherited(subclass) ⇒ Object
68
69
70
|
# File 'lib/rubocop/cop/cop.rb', line 68
def self.inherited(subclass)
@all << subclass
end
|
.lint? ⇒ Boolean
84
85
86
|
# File 'lib/rubocop/cop/cop.rb', line 84
def self.lint?
cop_type == :lint
end
|
.non_rails ⇒ Object
64
65
66
|
# File 'lib/rubocop/cop/cop.rb', line 64
def self.non_rails
@all.without_type(:rails)
end
|
.rails? ⇒ Boolean
88
89
90
|
# File 'lib/rubocop/cop/cop.rb', line 88
def self.rails?
cop_type == :rails
end
|
.style? ⇒ Boolean
80
81
82
|
# File 'lib/rubocop/cop/cop.rb', line 80
def self.style?
cop_type == :style
end
|
Instance Method Details
#add_offence(severity, node, loc, message = nil) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/rubocop/cop/cop.rb', line 121
def add_offence(severity, node, loc, message = nil)
location = loc.is_a?(Symbol) ? node.loc.send(loc) : loc
return if disabled_line?(location.line)
message = message ? message : message(node)
message = debug? ? "#{name}: #{message}" : message
corrected = begin
autocorrect(node) if autocorrect?
autocorrect?
rescue CorrectionNotPossible
false
end
@offences << Offence.new(severity, location, message, name, corrected)
end
|
#autocorrect? ⇒ Boolean
105
106
107
|
# File 'lib/rubocop/cop/cop.rb', line 105
def autocorrect?
@options[:auto_correct] && support_autocorrect?
end
|
#convention(node, location, message = nil) ⇒ Object
138
139
140
|
# File 'lib/rubocop/cop/cop.rb', line 138
def convention(node, location, message = nil)
add_offence(:convention, node, location, message)
end
|
#cop_config ⇒ Object
101
102
103
|
# File 'lib/rubocop/cop/cop.rb', line 101
def cop_config
@config.for_cop(self)
end
|
#cop_name ⇒ Object
Also known as:
name
146
147
148
|
# File 'lib/rubocop/cop/cop.rb', line 146
def cop_name
self.class.cop_name
end
|
#debug? ⇒ Boolean
109
110
111
|
# File 'lib/rubocop/cop/cop.rb', line 109
def debug?
@options[:debug]
end
|
#ignore_node(node) ⇒ Object
152
153
154
|
# File 'lib/rubocop/cop/cop.rb', line 152
def ignore_node(node)
@ignored_nodes << node
end
|
#message(node = nil) ⇒ Object
113
114
115
|
# File 'lib/rubocop/cop/cop.rb', line 113
def message(node = nil)
self.class::MSG
end
|
#support_autocorrect? ⇒ Boolean
117
118
119
|
# File 'lib/rubocop/cop/cop.rb', line 117
def support_autocorrect?
respond_to?(:autocorrect, true)
end
|
#warning(node, location, message = nil) ⇒ Object
142
143
144
|
# File 'lib/rubocop/cop/cop.rb', line 142
def warning(node, location, message = nil)
add_offence(:warning, node, location, message)
end
|