Class: Rubocop::Cop::Cop
- Inherits:
-
Object
show all
- Extended by:
- AST::Sexp
- Includes:
- Util
- 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::DefaultScope, Rails::HasAndBelongsToMany, Rails::Output, Rails::ReadAttribute, Rails::Validation, Style::AccessModifierIndentation, Style::AccessorMethodName, 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::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::FlipFlop, 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::MethodDefParentheses, 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::PredicateName, Style::Proc, Style::RaiseArgs, Style::RedundantBegin, Style::RedundantException, Style::RedundantReturn, Style::RedundantSelf, Style::RegexpLiteral, Style::RescueModifier, Style::Semicolon, Style::SignalException, Style::SingleLineBlockParams, 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::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) + [:'`']
Constants included
from Util
Util::PROC_NEW_NODE
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Util
block_length, command?, comment_line?, const_name, first_part_of_call_chain, lambda?, lambda_or_proc?, line_range, on_node, proc?, range_with_surrounding_space, source_range, strip_quotes
Constructor Details
#initialize(config = nil, options = nil) ⇒ Cop
Returns a new instance of Cop.
93
94
95
96
97
98
99
100
|
# File 'lib/rubocop/cop/cop.rb', line 93
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.
56
57
58
|
# File 'lib/rubocop/cop/cop.rb', line 56
def config
@config
end
|
#corrections ⇒ Object
Returns the value of attribute corrections.
56
57
58
|
# File 'lib/rubocop/cop/cop.rb', line 56
def corrections
@corrections
end
|
#offences ⇒ Object
Returns the value of attribute offences.
56
57
58
|
# File 'lib/rubocop/cop/cop.rb', line 56
def offences
@offences
end
|
#processed_source ⇒ Object
57
58
59
|
# File 'lib/rubocop/cop/cop.rb', line 57
def processed_source
@processed_source
end
|
Class Method Details
.all ⇒ Object
61
62
63
|
# File 'lib/rubocop/cop/cop.rb', line 61
def self.all
@all.clone
end
|
.cop_name ⇒ Object
73
74
75
|
# File 'lib/rubocop/cop/cop.rb', line 73
def self.cop_name
name.to_s.split('::').last
end
|
.cop_type ⇒ Object
77
78
79
|
# File 'lib/rubocop/cop/cop.rb', line 77
def self.cop_type
name.to_s.split('::')[-2].downcase.to_sym
end
|
.inherited(subclass) ⇒ Object
69
70
71
|
# File 'lib/rubocop/cop/cop.rb', line 69
def self.inherited(subclass)
@all << subclass
end
|
.lint? ⇒ Boolean
85
86
87
|
# File 'lib/rubocop/cop/cop.rb', line 85
def self.lint?
cop_type == :lint
end
|
.non_rails ⇒ Object
65
66
67
|
# File 'lib/rubocop/cop/cop.rb', line 65
def self.non_rails
@all.without_type(:rails)
end
|
.rails? ⇒ Boolean
89
90
91
|
# File 'lib/rubocop/cop/cop.rb', line 89
def self.rails?
cop_type == :rails
end
|
.style? ⇒ Boolean
81
82
83
|
# File 'lib/rubocop/cop/cop.rb', line 81
def self.style?
cop_type == :style
end
|
Instance Method Details
#add_offence(node, loc, message = nil, severity = nil) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/rubocop/cop/cop.rb', line 122
def add_offence(node, loc, message = nil, severity = nil)
location = loc.is_a?(Symbol) ? node.loc.send(loc) : loc
return if disabled_line?(location.line)
severity = custom_severity || severity || default_severity
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)
yield if block_given?
end
|
#autocorrect? ⇒ Boolean
106
107
108
|
# File 'lib/rubocop/cop/cop.rb', line 106
def autocorrect?
@options[:auto_correct] && support_autocorrect?
end
|
#config_to_allow_offences ⇒ Object
#config_to_allow_offences=(hash) ⇒ Object
#cop_config ⇒ Object
102
103
104
|
# File 'lib/rubocop/cop/cop.rb', line 102
def cop_config
@config.for_cop(self)
end
|
#cop_name ⇒ Object
Also known as:
name
151
152
153
|
# File 'lib/rubocop/cop/cop.rb', line 151
def cop_name
self.class.cop_name
end
|
#debug? ⇒ Boolean
110
111
112
|
# File 'lib/rubocop/cop/cop.rb', line 110
def debug?
@options[:debug]
end
|
#exclude_file?(file) ⇒ Boolean
177
178
179
180
181
182
183
|
# File 'lib/rubocop/cop/cop.rb', line 177
def exclude_file?(file)
return false unless exclude_paths
exclude_paths.any? do |regex|
processed_source.buffer.name =~ /#{regex}/
end
end
|
#exclude_paths ⇒ Object
173
174
175
|
# File 'lib/rubocop/cop/cop.rb', line 173
def exclude_paths
cop_config && cop_config['Exclude']
end
|
#ignore_node(node) ⇒ Object
157
158
159
|
# File 'lib/rubocop/cop/cop.rb', line 157
def ignore_node(node)
@ignored_nodes << node
end
|
#include_file?(file) ⇒ Boolean
165
166
167
168
169
170
171
|
# File 'lib/rubocop/cop/cop.rb', line 165
def include_file?(file)
return true unless include_paths
include_paths.any? do |regex|
processed_source.buffer.name =~ /#{regex}/
end
end
|
#include_paths ⇒ Object
161
162
163
|
# File 'lib/rubocop/cop/cop.rb', line 161
def include_paths
cop_config && cop_config['Include']
end
|
#message(node = nil) ⇒ Object
114
115
116
|
# File 'lib/rubocop/cop/cop.rb', line 114
def message(node = nil)
self.class::MSG
end
|
#relevant_file?(file) ⇒ Boolean
185
186
187
|
# File 'lib/rubocop/cop/cop.rb', line 185
def relevant_file?(file)
include_file?(file) && !exclude_file?(file)
end
|
#support_autocorrect? ⇒ Boolean
118
119
120
|
# File 'lib/rubocop/cop/cop.rb', line 118
def support_autocorrect?
respond_to?(:autocorrect, true)
end
|