Class: RuboCop::Cop::Cop
- 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
Defined Under Namespace
Classes: Correction
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#offenses ⇒ Object
readonly
Returns the value of attribute offenses.
Attributes inherited from Base
Class Method Summary collapse
-
.all ⇒ Object
deprecated
Deprecated.
Use Registry.all
- .joining_forces ⇒ Object
-
.qualified_cop_name(name, origin) ⇒ Object
deprecated
Deprecated.
Use Registry.qualified_cop_name
-
.registry ⇒ Object
deprecated
Deprecated.
Use Registry.global
- .support_autocorrect? ⇒ Boolean
Instance Method Summary collapse
- #add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block) ⇒ Object
- #corrections ⇒ Object deprecated Deprecated.
- #find_location(node, loc) ⇒ Object
- #highlights ⇒ Object
- #messages ⇒ Object
-
#on_investigation_end ⇒ Object
Called after all on_…
-
#on_new_investigation ⇒ Object
Called before all on_…
- #parse(source, path = nil) ⇒ Object deprecated Deprecated.
-
#support_autocorrect? ⇒ Boolean
deprecated
Deprecated.
Use class method
Methods inherited from Base
#add_global_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, lint?, match?, #message, #on_other_file, #ready, #relevant_file?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Attribute Details
#offenses ⇒ Object (readonly)
Returns the value of attribute offenses
12 13 14 |
# File 'lib/rubocop/cop/cop.rb', line 12 def offenses @offenses end |
Class Method Details
.all ⇒ Object
Use Registry.all
91 92 93 |
# File 'lib/rubocop/cop/cop.rb', line 91 def self.all Registry.all end |
.joining_forces ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/rubocop/cop/cop.rb', line 54 def self.joining_forces return unless method_defined?(:join_force?) cop = new Force.all.select do |force_class| cop.join_force?(force_class) end end |
.qualified_cop_name(name, origin) ⇒ Object
Use Registry.qualified_cop_name
96 97 98 |
# File 'lib/rubocop/cop/cop.rb', line 96 def self.qualified_cop_name(name, origin) Registry.qualified_cop_name(name, origin) end |
.registry ⇒ Object
Use Registry.global
86 87 88 |
# File 'lib/rubocop/cop/cop.rb', line 86 def self.registry Registry.global end |
.support_autocorrect? ⇒ Boolean
50 51 52 |
# File 'lib/rubocop/cop/cop.rb', line 50 def self.support_autocorrect? method_defined?(:autocorrect) end |
Instance Method Details
#add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rubocop/cop/cop.rb', line 27 def add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block) @v0_argument = node_or_range range = find_location(node_or_range, location) if block.nil? && !support_autocorrect? super(range, message: , severity: severity) else super(range, message: , severity: severity) do |corrector| emulate_v0_callsequence(corrector, &block) end end end |
#corrections ⇒ Object
64 65 66 67 68 69 |
# File 'lib/rubocop/cop/cop.rb', line 64 def corrections # warn 'Cop#corrections is deprecated' TODO return [] unless @last_corrector Legacy::CorrectionsProxy.new(@last_corrector) end |
#find_location(node, loc) ⇒ Object
39 40 41 42 |
# File 'lib/rubocop/cop/cop.rb', line 39 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 |
#highlights ⇒ Object
65 66 67 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 65 def highlights offenses.sort.map { |o| o.location.source } end |
#messages ⇒ Object
61 62 63 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 61 def offenses.sort.map(&:message) end |
#on_investigation_end ⇒ Object
Called after all on_… have been called
78 79 80 81 |
# File 'lib/rubocop/cop/cop.rb', line 78 def on_investigation_end investigate_post_walk(processed_source) if respond_to?(:investigate_post_walk) super end |
#on_new_investigation ⇒ Object
Called before all on_… have been called
72 73 74 75 |
# File 'lib/rubocop/cop/cop.rb', line 72 def on_new_investigation investigate(processed_source) if respond_to?(:investigate) super end |
#parse(source, path = nil) ⇒ Object
Open issue if there's a valid use case to include this in Base
102 103 104 |
# File 'lib/rubocop/cop/cop.rb', line 102 def parse(source, path = nil) ProcessedSource.new(source, target_ruby_version, path) end |
#support_autocorrect? ⇒ Boolean
Use class method
45 46 47 48 |
# File 'lib/rubocop/cop/cop.rb', line 45 def support_autocorrect? # warn 'deprecated, use cop.class.support_autocorrect?' TODO self.class.support_autocorrect? end |