Class: RuboCop::Cop::Cop

Inherits:
Object
  • Object
show all
Extended by:
AST::Sexp, NodePattern::Macros
Includes:
AST::Sexp, AutocorrectLogic, IgnoredNode, Util
Defined in:
lib/rubocop/cop/cop.rb,
lib/rubocop/rspec/cop_helper.rb

Overview

Monkey-patch Cop for tests to provide easy access to messages and highlights.

Direct Known Subclasses

Bundler::DuplicatedGem, Bundler::GemComment, Bundler::InsecureProtocolSource, Bundler::OrderedGems, Gemspec::DuplicatedAssignment, Gemspec::OrderedDependencies, Gemspec::RequiredRubyVersion, Gemspec::RubyVersionGlobalsUsage, InternalAffairs::NodeDestructuring, InternalAffairs::NodeTypePredicate, InternalAffairs::OffenseLocationKeyword, InternalAffairs::RedundantLocationArgument, InternalAffairs::RedundantMessageArgument, InternalAffairs::UselessMessageAssertion, Layout::AccessModifierIndentation, Layout::AlignArguments, Layout::AlignArray, Layout::AlignHash, Layout::AlignParameters, Layout::BlockAlignment, Layout::BlockEndNewline, Layout::CaseIndentation, Layout::ClassStructure, Layout::ClosingHeredocIndentation, Layout::ClosingParenthesisIndentation, Layout::CommentIndentation, Layout::ConditionPosition, Layout::DefEndAlignment, Layout::DotPosition, Layout::ElseAlignment, Layout::EmptyComment, Layout::EmptyLineAfterGuardClause, Layout::EmptyLineAfterMagicComment, Layout::EmptyLineBetweenDefs, Layout::EmptyLines, Layout::EmptyLinesAroundAccessModifier, Layout::EmptyLinesAroundArguments, Layout::EmptyLinesAroundBeginBody, Layout::EmptyLinesAroundBlockBody, Layout::EmptyLinesAroundClassBody, Layout::EmptyLinesAroundExceptionHandlingKeywords, Layout::EmptyLinesAroundMethodBody, Layout::EmptyLinesAroundModuleBody, Layout::EndAlignment, Layout::EndOfLine, Layout::ExtraSpacing, Layout::FirstArrayElementLineBreak, Layout::FirstHashElementLineBreak, Layout::FirstMethodArgumentLineBreak, Layout::FirstMethodParameterLineBreak, Layout::HeredocArgumentClosingParenthesis, Layout::IndentAssignment, Layout::IndentFirstArgument, Layout::IndentFirstArrayElement, Layout::IndentFirstHashElement, Layout::IndentFirstParameter, Layout::IndentHeredoc, Layout::IndentationConsistency, Layout::IndentationWidth, Layout::InitialIndentation, Layout::LeadingBlankLines, Layout::LeadingCommentSpace, Layout::MultilineArrayBraceLayout, Layout::MultilineArrayLineBreaks, Layout::MultilineAssignmentLayout, Layout::MultilineBlockLayout, Layout::MultilineHashBraceLayout, Layout::MultilineHashKeyLineBreaks, Layout::MultilineMethodArgumentLineBreaks, Layout::MultilineMethodCallBraceLayout, Layout::MultilineMethodCallIndentation, Layout::MultilineMethodDefinitionBraceLayout, Layout::MultilineOperationIndentation, Layout::RescueEnsureAlignment, Layout::SpaceAfterColon, Layout::SpaceAfterComma, Layout::SpaceAfterMethodName, Layout::SpaceAfterNot, Layout::SpaceAfterSemicolon, Layout::SpaceAroundBlockParameters, Layout::SpaceAroundEqualsInParameterDefault, Layout::SpaceAroundKeyword, Layout::SpaceAroundOperators, Layout::SpaceBeforeBlockBraces, Layout::SpaceBeforeComma, Layout::SpaceBeforeComment, Layout::SpaceBeforeFirstArg, Layout::SpaceBeforeSemicolon, Layout::SpaceInLambdaLiteral, Layout::SpaceInsideArrayLiteralBrackets, Layout::SpaceInsideArrayPercentLiteral, Layout::SpaceInsideBlockBraces, Layout::SpaceInsideHashLiteralBraces, Layout::SpaceInsideParens, Layout::SpaceInsidePercentLiteralDelimiters, Layout::SpaceInsideRangeLiteral, Layout::SpaceInsideReferenceBrackets, Layout::SpaceInsideStringInterpolation, Layout::Tab, Layout::TrailingBlankLines, Layout::TrailingWhitespace, Lint::AmbiguousBlockAssociation, Lint::AmbiguousOperator, Lint::AmbiguousRegexpLiteral, Lint::AssignmentInCondition, Lint::BigDecimalNew, Lint::BooleanSymbol, Lint::CircularArgumentReference, Lint::Debugger, Lint::DeprecatedClassMethods, Lint::DisjunctiveAssignmentInConstructor, Lint::DuplicateCaseCondition, Lint::DuplicateMethods, Lint::DuplicatedKey, Lint::EachWithObjectArgument, Lint::ElseLayout, Lint::EmptyEnsure, Lint::EmptyExpression, Lint::EmptyInterpolation, Lint::EmptyWhen, Lint::EndInMethod, Lint::EnsureReturn, Lint::ErbNewArguments, Lint::FlipFlop, Lint::FloatOutOfRange, Lint::FormatParameterMismatch, Lint::HandleExceptions, Lint::HeredocMethodCallPosition, Lint::ImplicitStringConcatenation, Lint::IneffectiveAccessModifier, Lint::InheritException, Lint::InterpolationCheck, Lint::LiteralAsCondition, Lint::LiteralInInterpolation, Lint::Loop, Lint::MissingCopEnableDirective, Lint::MultipleCompare, Lint::NestedMethodDefinition, Lint::NestedPercentLiteral, Lint::NextWithoutAccumulator, Lint::NonLocalExitFromIterator, Lint::NumberConversion, Lint::OrderedMagicComments, Lint::ParenthesesAsGroupedExpression, Lint::PercentStringArray, Lint::PercentSymbolArray, Lint::RandOne, Lint::RedundantWithIndex, Lint::RedundantWithObject, Lint::RegexpAsCondition, Lint::RequireParentheses, Lint::RescueException, Lint::RescueType, Lint::ReturnInVoidContext, Lint::SafeNavigationChain, Lint::SafeNavigationConsistency, Lint::SafeNavigationWithEmpty, Lint::ScriptPermission, Lint::SendWithMixinArgument, Lint::ShadowedArgument, Lint::ShadowedException, Lint::ShadowingOuterLocalVariable, Lint::StringConversionInInterpolation, Lint::Syntax, Lint::ToJSON, Lint::UnderscorePrefixedVariableName, Lint::UnifiedInteger, Lint::UnneededCopDisableDirective, Lint::UnneededCopEnableDirective, Lint::UnneededRequireStatement, Lint::UnneededSplatExpansion, Lint::UnreachableCode, Lint::UnusedBlockArgument, Lint::UnusedMethodArgument, Lint::UriEscapeUnescape, Lint::UriRegexp, Lint::UselessAccessModifier, Lint::UselessAssignment, Lint::UselessComparison, Lint::UselessElseWithoutRescue, Lint::UselessSetterCall, Lint::Void, Metrics::AbcSize, Metrics::BlockLength, Metrics::BlockNesting, Metrics::ClassLength, Metrics::CyclomaticComplexity, Metrics::LineLength, Metrics::MethodLength, Metrics::ModuleLength, Metrics::ParameterLists, Metrics::PerceivedComplexity, Migration::DepartmentName, Naming::AccessorMethodName, Naming::AsciiIdentifiers, Naming::BinaryOperatorParameterName, Naming::ClassAndModuleCamelCase, Naming::ConstantName, Naming::FileName, Naming::HeredocDelimiterCase, Naming::HeredocDelimiterNaming, Naming::MemoizedInstanceVariableName, Naming::MethodName, Naming::PredicateName, Naming::RescuedExceptionsVariableName, Naming::UncommunicativeBlockParamName, Naming::UncommunicativeMethodParamName, Naming::VariableName, Naming::VariableNumber, Security::Eval, Security::JSONLoad, Security::MarshalLoad, Security::Open, Security::YAMLLoad, Style::AccessModifierDeclarations, Style::Alias, Style::AndOr, Style::ArrayJoin, Style::AsciiComments, Style::Attr, Style::AutoResourceCleanup, Style::BarePercentLiterals, Style::BeginBlock, Style::BlockComments, Style::BlockDelimiters, Style::BracesAroundHashParameters, Style::CaseEquality, Style::CharacterLiteral, Style::ClassAndModuleChildren, Style::ClassCheck, Style::ClassMethods, Style::ClassVars, Style::CollectionMethods, Style::ColonMethodCall, Style::ColonMethodDefinition, Style::CommandLiteral, Style::CommentAnnotation, Style::CommentedKeyword, Style::ConditionalAssignment, Style::ConstantVisibility, Style::Copyright, Style::DateTime, Style::DefWithParentheses, Style::Dir, Style::Documentation, Style::DocumentationMethod, Style::DoubleCopDisableDirective, Style::DoubleNegation, Style::EachForSimpleLoop, Style::EachWithObject, Style::EmptyBlockParameter, Style::EmptyCaseCondition, Style::EmptyElse, Style::EmptyLambdaParameter, Style::EmptyLiteral, Style::EmptyMethod, Style::Encoding, Style::EndBlock, Style::EvalWithLocation, Style::EvenOdd, Style::ExpandPathArguments, Style::FloatDivision, Style::For, Style::FormatString, Style::FormatStringToken, Style::FrozenStringLiteralComment, Style::GlobalVars, Style::GuardClause, Style::HashSyntax, Style::IdenticalConditionalBranches, Style::IfInsideElse, Style::IfUnlessModifier, Style::IfUnlessModifierOfIfUnless, Style::IfWithSemicolon, Style::ImplicitRuntimeError, Style::InfiniteLoop, Style::InlineComment, Style::InverseMethods, Style::IpAddresses, Style::Lambda, Style::LambdaCall, Style::LineEndConcatenation, Style::MethodCallWithArgsParentheses, Style::MethodCallWithoutArgsParentheses, Style::MethodCalledOnDoEndBlock, Style::MethodDefParentheses, Style::MethodMissingSuper, Style::MinMax, Style::MissingElse, Style::MissingRespondToMissing, Style::MixinGrouping, Style::MixinUsage, Style::ModuleFunction, Style::MultilineBlockChain, Style::MultilineIfModifier, Style::MultilineIfThen, Style::MultilineMemoization, Style::MultilineMethodSignature, Style::MultilineTernaryOperator, Style::MultilineWhenThen, Style::MultipleComparison, Style::MutableConstant, Style::NegatedIf, Style::NegatedUnless, Style::NegatedWhile, Style::NestedModifier, Style::NestedParenthesizedCalls, Style::NestedTernaryOperator, Style::Next, Style::NilComparison, Style::NonNilCheck, Style::Not, Style::NumericLiteralPrefix, Style::NumericLiterals, Style::NumericPredicate, Style::OneLineConditional, Style::OptionHash, Style::OptionalArguments, Style::OrAssignment, Style::ParallelAssignment, Style::ParenthesesAroundCondition, Style::PercentLiteralDelimiters, Style::PercentQLiterals, Style::PerlBackrefs, Style::PreferredHashMethods, Style::Proc, Style::RaiseArgs, Style::RandomWithOffset, Style::RedundantBegin, Style::RedundantConditional, Style::RedundantException, Style::RedundantFreeze, Style::RedundantParentheses, Style::RedundantReturn, Style::RedundantSelf, Style::RedundantSortBy, Style::RegexpLiteral, Style::RescueModifier, Style::RescueStandardError, Style::ReturnNil, Style::SafeNavigation, Style::Sample, Style::SelfAssignment, Style::Semicolon, Style::Send, Style::SignalException, Style::SingleLineBlockParams, Style::SingleLineMethods, Style::SpecialGlobalVars, Style::StabbyLambdaParentheses, Style::StderrPuts, Style::StringHashKeys, Style::StringLiterals, Style::StringLiteralsInInterpolation, Style::StringMethods, Style::Strip, Style::StructInheritance, Style::SymbolArray, Style::SymbolLiteral, Style::SymbolProc, Style::TernaryParentheses, Style::TrailingBodyOnClass, Style::TrailingBodyOnMethodDefinition, Style::TrailingBodyOnModule, Style::TrailingCommaInArguments, Style::TrailingCommaInArrayLiteral, Style::TrailingCommaInHashLiteral, Style::TrailingMethodEndStatement, Style::TrailingUnderscoreVariable, Style::TrivialAccessors, Style::UnlessElse, Style::UnneededCapitalW, Style::UnneededCondition, Style::UnneededInterpolation, Style::UnneededPercentQ, Style::UnneededSort, Style::UnpackFirst, Style::VariableInterpolation, Style::WhenThen, Style::WhileUntilDo, Style::WhileUntilModifier, Style::WordArray, Style::YodaCondition, Style::ZeroLengthPredicate

Defined Under Namespace

Classes: Correction

Constant Summary

Constants included from Util

Util::LITERAL_REGEX

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AST::Sexp

s

Methods included from NodePattern::Macros

def_node_matcher, def_node_search, node_search, node_search_all, node_search_body, node_search_first

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_offense, #disable_uncorrectable?, #support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

begins_its_line?, comment_line?, double_quotes_required?, escape_string, first_part_of_call_chain, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, tokens, trim_string_interporation_escape_character

Methods included from PathUtil

absolute?, chdir, hidden_dir?, hidden_file_in_not_hidden_dir?, match_path?, pwd, relative_path, reset_pwd, smart_path

Constructor Details

#initialize(config = nil, options = nil) ⇒ Cop

Returns a new instance of Cop.



100
101
102
103
104
105
106
107
108
109
# File 'lib/rubocop/cop/cop.rb', line 100

def initialize(config = nil, options = nil)
  @config = config || Config.new
  @options = options || { debug: false }

  @offenses = []
  @corrections = []
  @corrected_nodes = {}
  @corrected_nodes.compare_by_identity
  @processed_source = nil
end

Class Attribute Details

.registryObject (readonly)

Returns the value of attribute registry.



50
51
52
# File 'lib/rubocop/cop/cop.rb', line 50

def registry
  @registry
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



44
45
46
# File 'lib/rubocop/cop/cop.rb', line 44

def config
  @config
end

#correctionsObject (readonly)

Returns the value of attribute corrections.



44
45
46
# File 'lib/rubocop/cop/cop.rb', line 44

def corrections
  @corrections
end

#offensesObject (readonly)

Returns the value of attribute offenses.



44
45
46
# File 'lib/rubocop/cop/cop.rb', line 44

def offenses
  @offenses
end

#processed_sourceObject

TODO: Bad design.



45
46
47
# File 'lib/rubocop/cop/cop.rb', line 45

def processed_source
  @processed_source
end

Class Method Details

.allObject



53
54
55
# File 'lib/rubocop/cop/cop.rb', line 53

def self.all
  registry.without_department(:Test).cops
end

.autocorrect_incompatible_withArray<RuboCop::Cop::Cop>

List of cops that should not try to autocorrect at the same time as this cop

Returns:



96
97
98
# File 'lib/rubocop/cop/cop.rb', line 96

def self.autocorrect_incompatible_with
  []
end

.badgeObject



65
66
67
# File 'lib/rubocop/cop/cop.rb', line 65

def self.badge
  @badge ||= Badge.for(name)
end

.cop_nameObject



69
70
71
# File 'lib/rubocop/cop/cop.rb', line 69

def self.cop_name
  badge.to_s
end

.departmentObject



73
74
75
# File 'lib/rubocop/cop/cop.rb', line 73

def self.department
  badge.department
end

.inherited(subclass) ⇒ Object



61
62
63
# File 'lib/rubocop/cop/cop.rb', line 61

def self.inherited(subclass)
  registry.enlist(subclass)
end

.lint?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rubocop/cop/cop.rb', line 77

def self.lint?
  department == :Lint
end

.match?(given_names) ⇒ Boolean

Returns true if the cop name or the cop namespace matches any of the given names.

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/rubocop/cop/cop.rb', line 83

def self.match?(given_names)
  return false unless given_names

  given_names.include?(cop_name) ||
    given_names.include?(department.to_s)
end

.qualified_cop_name(name, origin) ⇒ Object



57
58
59
# File 'lib/rubocop/cop/cop.rb', line 57

def self.qualified_cop_name(name, origin)
  registry.qualified_cop_name(name, origin)
end

Instance Method Details

#add_offense(node, location: :expression, message: nil, severity: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rubocop/cop/cop.rb', line 127

def add_offense(node, location: :expression, message: nil, severity: nil)
  loc = find_location(node, location)

  return if duplicate_location?(loc)

  severity = custom_severity || severity || default_severity

  message ||= message(node)
  message = annotate(message)

  status = enabled_line?(loc.line) ? correct(node) : :disabled

  @offenses << Offense.new(severity, loc, message, name, status)
  yield if block_given? && status != :disabled
end

#config_to_allow_offensesObject



187
188
189
190
# File 'lib/rubocop/cop/cop.rb', line 187

def config_to_allow_offenses
  Formatter::DisabledConfigFormatter
    .config_to_allow_offenses[cop_name] ||= {}
end

#config_to_allow_offenses=(hash) ⇒ Object



192
193
194
195
# File 'lib/rubocop/cop/cop.rb', line 192

def config_to_allow_offenses=(hash)
  Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] =
    hash
end

#cop_configObject



115
116
117
118
119
120
# File 'lib/rubocop/cop/cop.rb', line 115

def cop_config
  # Use department configuration as basis, but let individual cop
  # configuration override.
  @cop_config ||= @config.for_cop(self.class.department.to_s)
                         .merge(@config.for_cop(self))
end

#cop_nameObject Also known as: name



209
210
211
# File 'lib/rubocop/cop/cop.rb', line 209

def cop_name
  @cop_name ||= self.class.cop_name
end

#correct(node) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rubocop/cop/cop.rb', line 153

def correct(node)
  reason = reason_to_not_correct(node)
  return reason if reason

  @corrected_nodes[node] = true
  if support_autocorrect?
    correction = autocorrect(node)
    return :uncorrected unless correction

    @corrections << Correction.new(correction, node, self)
    :corrected
  elsif disable_uncorrectable?
    disable_uncorrectable(node)
    :corrected_with_todo
  end
end

#disable_uncorrectable(node) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/rubocop/cop/cop.rb', line 178

def disable_uncorrectable(node)
  @disabled_lines ||= {}
  line = node.location.line
  return if @disabled_lines.key?(line)

  @disabled_lines[line] = true
  @corrections << Correction.new(disable_offense(node), node, self)
end

#duplicate_location?(location) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/rubocop/cop/cop.rb', line 149

def duplicate_location?(location)
  @offenses.any? { |o| o.location == location }
end

#excluded_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/rubocop/cop/cop.rb', line 220

def excluded_file?(file)
  !relevant_file?(file)
end

#find_location(node, loc) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity



144
145
146
147
# File 'lib/rubocop/cop/cop.rb', line 144

def find_location(node, loc)
  # Location can be provided as a symbol, e.g.: `:keyword`
  loc.is_a?(Symbol) ? node.loc.public_send(loc) : loc
end

#highlightsObject



89
90
91
# File 'lib/rubocop/rspec/cop_helper.rb', line 89

def highlights
  offenses.sort.map { |o| o.location.source }
end

#join_force?(_force_class) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/rubocop/cop/cop.rb', line 111

def join_force?(_force_class)
  false
end

#message(_node = nil) ⇒ Object



122
123
124
# File 'lib/rubocop/cop/cop.rb', line 122

def message(_node = nil)
  self.class::MSG
end

#messagesObject



85
86
87
# File 'lib/rubocop/rspec/cop_helper.rb', line 85

def messages
  offenses.sort.map(&:message)
end

#parse(source, path = nil) ⇒ Object



205
206
207
# File 'lib/rubocop/cop/cop.rb', line 205

def parse(source, path = nil)
  ProcessedSource.new(source, target_ruby_version, path)
end

#reason_to_not_correct(node) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/rubocop/cop/cop.rb', line 170

def reason_to_not_correct(node)
  return :unsupported unless correctable?
  return :uncorrected unless autocorrect?
  return :already_corrected if @corrected_nodes.key?(node)

  nil
end

#relevant_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
218
# File 'lib/rubocop/cop/cop.rb', line 215

def relevant_file?(file)
  file_name_matches_any?(file, 'Include', true) &&
    !file_name_matches_any?(file, 'Exclude', false)
end

#target_rails_versionObject



201
202
203
# File 'lib/rubocop/cop/cop.rb', line 201

def target_rails_version
  @config.target_rails_version
end

#target_ruby_versionObject



197
198
199
# File 'lib/rubocop/cop/cop.rb', line 197

def target_ruby_version
  @config.target_ruby_version
end