Class: Rubocop::Cop::Cop
- Inherits:
-
Parser::Rewriter
- Object
- Parser::Rewriter
- Rubocop::Cop::Cop
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.
Direct Known Subclasses
Lint::AssignmentInCondition, Lint::EndAlignment, Lint::EndInMethod, Lint::EnsureReturn, Lint::Eval, Lint::HandleExceptions, Lint::LiteralInCondition, Lint::Loop, Lint::RescueException, Lint::ShadowingOuterLocalVariable, Lint::UnreachableCode, Lint::UnusedLocalVariable, 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::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::MultilineIfThen, Style::MultilineTernaryOperator, Style::NestedTernaryOperator, Style::Not, Style::NumericLiterals, Style::OneLineConditional, Style::OpMethod, Style::ParameterLists, Style::ParenthesesAroundCondition, Style::Proc, Style::ReduceArguments, Style::RegexpLiteral, Style::RescueModifier, Style::Semicolon, 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
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Cop
Returns a new instance of Cop.
63
64
65
66
67
|
# File 'lib/rubocop/cop/cop.rb', line 63
def initialize
@offences = []
@debug = false
@autocorrect = false
end
|
Class Attribute Details
.all ⇒ Object
Returns the value of attribute all.
35
36
37
|
# File 'lib/rubocop/cop/cop.rb', line 35
def all
@all
end
|
.config ⇒ Object
Returns the value of attribute config.
36
37
38
|
# File 'lib/rubocop/cop/cop.rb', line 36
def config
@config
end
|
Instance Attribute Details
#autocorrect ⇒ Object
Returns the value of attribute autocorrect.
28
29
30
|
# File 'lib/rubocop/cop/cop.rb', line 28
def autocorrect
@autocorrect
end
|
#debug ⇒ Object
Returns the value of attribute debug.
27
28
29
|
# File 'lib/rubocop/cop/cop.rb', line 27
def debug
@debug
end
|
#disabled_lines=(value) ⇒ Object
Sets the attribute disabled_lines
29
30
31
|
# File 'lib/rubocop/cop/cop.rb', line 29
def disabled_lines=(value)
@disabled_lines = value
end
|
#offences ⇒ Object
Returns the value of attribute offences.
26
27
28
|
# File 'lib/rubocop/cop/cop.rb', line 26
def offences
@offences
end
|
Class Method Details
.cop_name ⇒ Object
43
44
45
|
# File 'lib/rubocop/cop/cop.rb', line 43
def self.cop_name
name.to_s.split('::').last
end
|
.cop_type ⇒ Object
47
48
49
|
# File 'lib/rubocop/cop/cop.rb', line 47
def self.cop_type
name.to_s.split('::')[-2].downcase.to_sym
end
|
.inherited(subclass) ⇒ Object
39
40
41
|
# File 'lib/rubocop/cop/cop.rb', line 39
def self.inherited(subclass)
all << subclass
end
|
.lint? ⇒ Boolean
55
56
57
|
# File 'lib/rubocop/cop/cop.rb', line 55
def self.lint?
cop_type == :lint
end
|
.rails? ⇒ Boolean
59
60
61
|
# File 'lib/rubocop/cop/cop.rb', line 59
def self.rails?
cop_type == :rails
end
|
.style? ⇒ Boolean
51
52
53
|
# File 'lib/rubocop/cop/cop.rb', line 51
def self.style?
cop_type == :style
end
|
Instance Method Details
#add_offence(severity, location, message) ⇒ Object
93
94
95
96
97
98
|
# File 'lib/rubocop/cop/cop.rb', line 93
def add_offence(severity, location, message)
unless @disabled_lines && @disabled_lines.include?(location.line)
message = debug ? "#{name}: #{message}" : message
@offences << Offence.new(severity, location, message, name)
end
end
|
#autocorrect_action(node) ⇒ Object
87
88
|
# File 'lib/rubocop/cop/cop.rb', line 87
def autocorrect_action(node)
end
|
#do_autocorrect(node) ⇒ Object
83
84
85
|
# File 'lib/rubocop/cop/cop.rb', line 83
def do_autocorrect(node)
autocorrect_action(node) if autocorrect
end
|
#ignore_node(node) ⇒ Object
90
91
|
# File 'lib/rubocop/cop/cop.rb', line 90
def ignore_node(node)
end
|
#inspect(source_buffer, source, tokens, ast, comments) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/rubocop/cop/cop.rb', line 69
def inspect(source_buffer, source, tokens, ast, )
if autocorrect
filename = source_buffer.instance_variable_get(:@name)
new_source = rewrite(source_buffer, ast)
unless new_source == source_buffer.source
File.open(filename, 'w') { |f| f.write(new_source) }
source_buffer.instance_variable_set(:@source, nil)
source_buffer.read
end
else
process(ast)
end
end
|
#name ⇒ Object
100
101
102
|
# File 'lib/rubocop/cop/cop.rb', line 100
def name
self.class.cop_name
end
|