Class: RuboCop::Cop::Cop Deprecated
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Base ,
::RuboCop::ExcludeLimit ,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
Instance Chain:
|
|
Inherits: |
RuboCop::Cop::Base
|
Defined in: | lib/rubocop/cop/cop.rb, lib/rubocop/rspec/cop_helper.rb |
Overview
Use Cop::Base instead
Legacy scaffold for Cops. See https://docs.rubocop.org/rubocop/v1_upgrade_notes.html
Constant Summary
Base
- Inherited
Class Attribute Summary
- .support_autocorrect? ⇒ Boolean readonly
Base
- Inherited
.gem_requirements, .lint?, | |
.support_autocorrect? | Returns if class supports autocorrect. |
.support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to #on_new_investigation and #on_investigation_end, the result of |
Class Method Summary
-
.all
deprecated
Deprecated.
Use Registry.all
- .inherited(_subclass)
- .joining_forces
-
.qualified_cop_name(name, origin)
deprecated
Deprecated.
Use Registry.qualified_cop_name
-
.registry
deprecated
Deprecated.
Use Registry.global
Base
- Inherited
.autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
.badge | Naming. |
.callbacks_needed, .cop_name, .department, | |
.documentation_url | Returns a url to view this cops documentation online. |
.exclude_from_registry | Call for abstract Cop classes. |
.inherited, | |
.joining_forces | Override and return the Force class(es) you need to join. |
.match? | Returns true if the cop name or the cop namespace matches any of the given names. |
.new, | |
.requires_gem | Register a version requirement for the given gem name. |
.restrict_on_send |
::RuboCop::ExcludeLimit
- Extended
exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
transform |
Instance Attribute Summary
- #offenses readonly
-
#support_autocorrect? ⇒ Boolean
readonly
deprecated
Deprecated.
Use class method
Base
- Inherited
AutocorrectLogic
- Included
Instance Method Summary
- #add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
-
#begin_investigation(processed_source, offset: 0, original: processed_source)
Internal use only
Internal use only
Called before any investigation.
- #corrections deprecated Deprecated.
- #find_location(node, loc)
- #highlights
- #messages
-
#on_investigation_end
Called after all on_…
-
#on_new_investigation
Called before all on_…
- #apply_correction(corrector) private
-
#callback_argument(_range)
private
Override Base.
- #correction_lambda private
- #dedupe_on_node(node) private
-
#emulate_v0_callsequence(corrector) {|corrector| ... }
private
Just for legacy.
- #range_for_original(range) private
- #suppress_clobbering private
Base
- Inherited
#add_global_offense | Adds an offense that has no particular location. |
#add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
#begin_investigation | Called before any investigation. |
#callbacks_needed, | |
#cop_config | Configuration Helpers. |
#cop_name, #excluded_file?, | |
#external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
#inspect, | |
#message | Gets called if no message is specified when calling #add_offense or |
#name | Alias for Base#cop_name. |
#offenses, | |
#on_investigation_end | Called after all on_… |
#on_new_investigation | Called before all on_… |
#on_other_file | Called instead of all on_… |
#parse | There should be very limited reasons for a Cop to do it’s own parsing. |
#parser_engine, | |
#ready | Called between investigations. |
#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
#callback_argument | Reserved for Cop::Cop. |
#complete_investigation | Called to complete an investigation. |
#correct, #current_corrector, | |
#current_offense_locations | Reserved for Commissioner: |
#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range, | |
#reset_investigation | Actually private methods. |
#use_corrector |
AutocorrectLogic
- Included
#disable_offense, #disable_offense_at_end_of_line, #disable_offense_before_and_after, #disable_offense_with_eol_or_surround_comment, #max_line_length, | |
#range_by_lines | Expand the given range to include all of any lines it covers. |
#range_of_first_line, #range_overlaps_offense?, #string_continuation, #string_continuation?, #surrounding_heredoc, #surrounding_percent_array |
IgnoredNode
- Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Class Attribute Details
.support_autocorrect? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/rubocop/cop/cop.rb', line 33
def self.support_autocorrect? method_defined?(:autocorrect) end
Class Method Details
.all
Use Registry.all
.inherited(_subclass)
[ GitHub ]# File 'lib/rubocop/cop/cop.rb', line 25
def self.inherited(_subclass) super warn Rainbow(<<~WARNING).yellow, uplevel: 1 Inheriting from `RuboCop::Cop::Cop` is deprecated. Use `RuboCop::Cop::Base` instead. For more information, see https://docs.rubocop.org/rubocop/v1_upgrade_notes.html. WARNING end
.joining_forces
[ GitHub ].qualified_cop_name(name, origin)
Use Registry.qualified_cop_name
# File 'lib/rubocop/cop/cop.rb', line 65
def self.qualified_cop_name(name, origin) warn Rainbow(<<~WARNING).yellow, uplevel: 1 `Cop.qualified_cop_name` is deprecated. Use `Registry.qualified_cop_name` instead. WARNING Registry.qualified_cop_name(name, origin) end
.registry
Use Registry.global
Instance Attribute Details
#offenses (readonly)
[ GitHub ]# File 'lib/rubocop/cop/cop.rb', line 12
attr_reader :offenses
#support_autocorrect? ⇒ Boolean
(readonly)
Use class method
# File 'lib/rubocop/cop/cop.rb', line 96
def support_autocorrect? warn Rainbow(<<~WARNING).yellow, uplevel: 1 `support_autocorrect?` is deprecated. Use `cop.class.support_autocorrect?`. WARNING self.class.support_autocorrect? end
Instance Method Details
#add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
[ GitHub ]# File 'lib/rubocop/cop/cop.rb', line 73
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) # Since this range may be generated from Ruby code embedded in some # template file, we convert it to location info in the original file. range = range_for_original(range) if block.nil? && !self.class.support_autocorrect? super(range, message: , severity: severity) else super(range, message: , severity: severity) do |corrector| emulate_v0_callsequence(corrector, &block) end end end
#apply_correction(corrector) (private)
[ GitHub ]# File 'lib/rubocop/cop/cop.rb', line 147
def apply_correction(corrector) suppress_clobbering { super } end
#begin_investigation(processed_source, offset: 0, original: processed_source)
Called before any investigation
# File 'lib/rubocop/cop/cop.rb', line 129
def begin_investigation(processed_source, offset: 0, original: processed_source) super @offenses = current_offenses @last_corrector = @current_corrector # We need to keep track of the original source and offset, # because `processed_source` here may be an embedded code in it. @current_offset = offset @current_original = original end
#callback_argument(_range) (private)
Override Base
# File 'lib/rubocop/cop/cop.rb', line 143
def callback_argument(_range) @v0_argument end
#correction_lambda (private)
[ GitHub ]# File 'lib/rubocop/cop/cop.rb', line 164
def correction_lambda return unless self.class.support_autocorrect? dedupe_on_node(@v0_argument) { autocorrect(@v0_argument) } end
#corrections
# File 'lib/rubocop/cop/cop.rb', line 105
def corrections warn Rainbow(<<~WARNING).yellow, uplevel: 1 `Cop#corrections` is deprecated. WARNING return [] unless @last_corrector Legacy::CorrectionsProxy.new(@last_corrector) end
#dedupe_on_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/cop.rb', line 170
def dedupe_on_node(node) @corrected_nodes ||= {}.compare_by_identity yield unless @corrected_nodes.key?(node) ensure @corrected_nodes[node] = true end
#emulate_v0_callsequence(corrector) {|corrector| ... } (private)
Just for legacy
# File 'lib/rubocop/cop/cop.rb', line 152
def emulate_v0_callsequence(corrector) lambda = correction_lambda yield corrector if block_given? unless corrector.empty? raise 'Your cop must inherit from Cop::Base and extend AutoCorrector' end return unless lambda suppress_clobbering { lambda.call(corrector) } end
#find_location(node, loc)
[ GitHub ]# File 'lib/rubocop/cop/cop.rb', line 90
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
[ GitHub ]# File 'lib/rubocop/rspec/cop_helper.rb', line 94
def highlights offenses.sort.map { |o| o.location.source } end
#messages
[ GitHub ]# File 'lib/rubocop/rspec/cop_helper.rb', line 90
def offenses.sort.map(&: ) end
#on_investigation_end
Called after all on_… have been called
# File 'lib/rubocop/cop/cop.rb', line 122
def on_investigation_end investigate_post_walk(processed_source) if respond_to?(:investigate_post_walk) super end
#on_new_investigation
Called before all on_… have been called
# File 'lib/rubocop/cop/cop.rb', line 116
def on_new_investigation investigate(processed_source) if respond_to?(:investigate) super end
#range_for_original(range) (private)
[ GitHub ]# File 'lib/rubocop/cop/cop.rb', line 183
def range_for_original(range) ::Parser::Source::Range.new( @current_original.buffer, range.begin_pos + @current_offset, range.end_pos + @current_offset ) end
#suppress_clobbering (private)
[ GitHub ]# File 'lib/rubocop/cop/cop.rb', line 177
def suppress_clobbering yield rescue ::Parser::ClobberingError # ignore Clobbering errors end