Class: Rubocop::Cop::Cop
Overview
A scaffold for concrete cops.
The Cop class is meant to be extended.
Cops track offenses 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::AmbiguousOperator, Lint::AmbiguousRegexpLiteral, Lint::AssignmentInCondition, Lint::BlockAlignment, Lint::ConditionPosition, Lint::Debugger, Lint::DeprecatedClassMethods, Lint::ElseLayout, Lint::EmptyEnsure, Lint::EmptyInterpolation, Lint::EndAlignment, Lint::EndInMethod, Lint::EnsureReturn, Lint::Eval, Lint::HandleExceptions, Lint::InvalidCharacterLiteral, Lint::LiteralInCondition, Lint::LiteralInInterpolation, Lint::Loop, Lint::ParenthesesAsGroupedExpression, Lint::RequireParentheses, Lint::RescueException, Lint::ShadowingOuterLocalVariable, Lint::SpaceBeforeFirstArg, Lint::StringConversionInInterpolation, Lint::UnreachableCode, Lint::UselessAccessModifier, Lint::UselessAssignment, Lint::UselessComparison, Lint::UselessElseWithoutRescue, Lint::UselessSetterCall, Lint::Void, Rails::ActionFilter, Rails::DefaultScope, Rails::HasAndBelongsToMany, Rails::Output, Rails::ReadWriteAttribute, Rails::ScopeArgs, Rails::Validation, Style::AccessModifierIndentation, Style::AccessorMethodName, Style::Alias, Style::AlignArray, Style::AlignHash, Style::AlignParameters, Style::AndOr, Style::ArrayJoin, 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::ClassAndModuleChildren, Style::ClassLength, Style::ClassMethods, Style::ClassVars, Style::CollectionMethods, Style::ColonMethodCall, Style::CommentAnnotation, Style::ConstantName, Style::CyclomaticComplexity, Style::DefWithParentheses, Style::DeprecatedHashMethods, Style::Documentation, Style::DotPosition, Style::DoubleNegation, Style::EmptyLineBetweenDefs, Style::EmptyLines, Style::EmptyLinesAroundAccessModifier, Style::EmptyLinesAroundBody, Style::EmptyLiteral, Style::Encoding, Style::EndBlock, Style::EndOfLine, Style::EvenOdd, Style::FileName, Style::FinalNewline, Style::FlipFlop, Style::For, Style::FormatString, Style::GlobalVars, Style::GuardClause, Style::HashSyntax, Style::IfUnlessModifier, Style::IfWithSemicolon, Style::IndentArray, Style::IndentHash, Style::IndentationConsistency, Style::IndentationWidth, Style::Lambda, Style::LambdaCall, Style::LeadingCommentSpace, Style::LineEndConcatenation, Style::LineLength, Style::MethodCallParentheses, Style::MethodCalledOnDoEndBlock, Style::MethodDefParentheses, Style::MethodLength, Style::MethodName, Style::ModuleFunction, Style::MultilineBlockChain, Style::MultilineIfThen, Style::MultilineTernaryOperator, Style::NegatedIf, Style::NegatedWhile, Style::NestedTernaryOperator, Style::NilComparison, Style::NonNilCheck, Style::Not, Style::NumericLiterals, Style::OneLineConditional, Style::OpMethod, Style::ParameterLists, Style::ParenthesesAroundCondition, Style::PercentLiteralDelimiters, Style::PerlBackrefs, Style::PredicateName, Style::Proc, Style::RaiseArgs, Style::RedundantBegin, Style::RedundantException, Style::RedundantReturn, Style::RedundantSelf, Style::RegexpLiteral, Style::RescueModifier, Style::SelfAssignment, Style::Semicolon, Style::SignalException, Style::SingleLineBlockParams, Style::SingleLineMethods, Style::SingleSpaceBeforeFirstArg, Style::SpaceAfterColon, Style::SpaceAfterComma, Style::SpaceAfterControlKeyword, Style::SpaceAfterMethodName, Style::SpaceAfterNot, Style::SpaceAfterSemicolon, Style::SpaceAroundEqualsInParameterDefault, Style::SpaceAroundOperators, Style::SpaceBeforeBlockBraces, Style::SpaceBeforeModifierKeyword, Style::SpaceInsideBlockBraces, Style::SpaceInsideBrackets, Style::SpaceInsideHashLiteralBraces, Style::SpaceInsideParens, Style::SpecialGlobalVars, Style::StringLiterals, Style::SymbolArray, Style::Tab, Style::TrailingBlankLines, Style::TrailingComma, Style::TrailingWhitespace, Style::TrivialAccessors, Style::UnlessElse, Style::VariableInterpolation, Style::VariableName, Style::WhenThen, Style::WhileUntilDo, Style::WhileUntilModifier, Style::WordArray
Constant Summary
Constants included
from Util
Util::ASGN_NODES, Util::EQUALS_ASGN_NODES, Util::OPERATOR_METHODS, Util::PROC_NEW_NODE, Util::SHORTHAND_ASGN_NODES
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
block_length, command?, comment_line?, const_name, first_part_of_call_chain, lambda?, lambda_or_proc?, line_range, on_node, operator?, parentheses?, proc?, range_with_surrounding_space, source_range, strip_quotes
Methods included from PathUtil
match_path?, relative_path
Constructor Details
#initialize(config = nil, options = nil) ⇒ Cop
Returns a new instance of Cop.
81
82
83
84
85
86
87
|
# File 'lib/rubocop/cop/cop.rb', line 81
def initialize(config = nil, options = nil)
@config = config || Config.new
@options = options || { auto_correct: false, debug: false }
@offenses = []
@corrections = []
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
48
49
50
|
# File 'lib/rubocop/cop/cop.rb', line 48
def config
@config
end
|
#corrections ⇒ Object
Returns the value of attribute corrections.
48
49
50
|
# File 'lib/rubocop/cop/cop.rb', line 48
def corrections
@corrections
end
|
#offenses ⇒ Object
Returns the value of attribute offenses.
48
49
50
|
# File 'lib/rubocop/cop/cop.rb', line 48
def offenses
@offenses
end
|
#processed_source ⇒ Object
49
50
51
|
# File 'lib/rubocop/cop/cop.rb', line 49
def processed_source
@processed_source
end
|
Class Method Details
.all ⇒ Object
53
54
55
|
# File 'lib/rubocop/cop/cop.rb', line 53
def self.all
@all.clone
end
|
.cop_name ⇒ Object
65
66
67
|
# File 'lib/rubocop/cop/cop.rb', line 65
def self.cop_name
name.to_s.split('::').last
end
|
.cop_type ⇒ Object
69
70
71
|
# File 'lib/rubocop/cop/cop.rb', line 69
def self.cop_type
name.to_s.split('::')[-2].downcase.to_sym
end
|
.inherited(subclass) ⇒ Object
61
62
63
|
# File 'lib/rubocop/cop/cop.rb', line 61
def self.inherited(subclass)
@all << subclass
end
|
.lint? ⇒ Boolean
73
74
75
|
# File 'lib/rubocop/cop/cop.rb', line 73
def self.lint?
cop_type == :lint
end
|
.non_rails ⇒ Object
57
58
59
|
# File 'lib/rubocop/cop/cop.rb', line 57
def self.non_rails
@all.without_type(:rails)
end
|
.rails? ⇒ Boolean
77
78
79
|
# File 'lib/rubocop/cop/cop.rb', line 77
def self.rails?
cop_type == :rails
end
|
Instance Method Details
#add_offense(node, loc, message = nil, severity = nil) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/rubocop/cop/cop.rb', line 113
def add_offense(node, loc, message = nil, severity = nil)
location = loc.is_a?(Symbol) ? node.loc.send(loc) : loc
return unless enabled_line?(location.line)
return if @offenses.find { |o| o.location == location }
severity = custom_severity || severity || default_severity
message ||= message(node)
message = display_cop_names? ? "#{name}: #{message}" : message
autocorrect(node) if autocorrect?
@offenses << Offense.new(severity, location, message, name,
autocorrect?)
yield if block_given?
end
|
#autocorrect? ⇒ Boolean
93
94
95
|
# File 'lib/rubocop/cop/cop.rb', line 93
def autocorrect?
@options[:auto_correct] && support_autocorrect?
end
|
#config_to_allow_offenses ⇒ Object
#config_to_allow_offenses=(hash) ⇒ Object
#cop_config ⇒ Object
89
90
91
|
# File 'lib/rubocop/cop/cop.rb', line 89
def cop_config
@config.for_cop(self)
end
|
#cop_name ⇒ Object
Also known as:
name
141
142
143
|
# File 'lib/rubocop/cop/cop.rb', line 141
def cop_name
self.class.cop_name
end
|
#debug? ⇒ Boolean
97
98
99
|
# File 'lib/rubocop/cop/cop.rb', line 97
def debug?
@options[:debug]
end
|
#display_cop_names? ⇒ Boolean
101
102
103
|
# File 'lib/rubocop/cop/cop.rb', line 101
def display_cop_names?
debug? || @options[:display_cop_names]
end
|
#exclude_file?(file) ⇒ Boolean
151
152
153
|
# File 'lib/rubocop/cop/cop.rb', line 151
def exclude_file?(file)
file_name_matches_any?(file, 'Exclude', false)
end
|
#include_file?(file) ⇒ Boolean
147
148
149
|
# File 'lib/rubocop/cop/cop.rb', line 147
def include_file?(file)
file_name_matches_any?(file, 'Include', true)
end
|
#message(node = nil) ⇒ Object
105
106
107
|
# File 'lib/rubocop/cop/cop.rb', line 105
def message(node = nil)
self.class::MSG
end
|
#relevant_file?(file) ⇒ Boolean
155
156
157
|
# File 'lib/rubocop/cop/cop.rb', line 155
def relevant_file?(file)
include_file?(file) && !exclude_file?(file)
end
|
#support_autocorrect? ⇒ Boolean
109
110
111
|
# File 'lib/rubocop/cop/cop.rb', line 109
def support_autocorrect?
respond_to?(:autocorrect, true)
end
|