lib/rubocop/cop/base.rb in rubocop-1.41.1 vs lib/rubocop/cop/base.rb in rubocop-1.42.0
- old
+ new
@@ -66,10 +66,68 @@
# @api public
def self.documentation_url
Documentation.url_for(self) if builtin?
end
+ def self.inherited(subclass)
+ super
+ Registry.global.enlist(subclass)
+ end
+
+ # Call for abstract Cop classes
+ def self.exclude_from_registry
+ Registry.global.dismiss(self)
+ end
+
+ # Returns if class supports autocorrect.
+ # It is recommended to extend AutoCorrector instead of overriding
+ def self.support_autocorrect?
+ false
+ end
+
+ ### Naming
+
+ def self.badge
+ @badge ||= Badge.for(name)
+ end
+
+ def self.cop_name
+ badge.to_s
+ end
+
+ def self.department
+ badge.department
+ end
+
+ def self.lint?
+ department == :Lint
+ end
+
+ # Returns true if the cop name or the cop namespace matches any of the
+ # given names.
+ def self.match?(given_names)
+ return false unless given_names
+
+ given_names.include?(cop_name) || given_names.include?(badge.department_name)
+ end
+
+ # Override and return the Force class(es) you need to join
+ def self.joining_forces; end
+
+ ### Persistence
+
+ # Override if your cop should be called repeatedly for multiple investigations
+ # Between calls to `on_new_investigation` and `on_investigation_end`,
+ # the result of `processed_source` will remain constant.
+ # You should invalidate any caches that depend on the current `processed_source`
+ # in the `on_new_investigation` callback.
+ # If your cop does autocorrections, be aware that your instance may be called
+ # multiple times with the same `processed_source.path` but different content.
+ def self.support_multiple_source?
+ false
+ end
+
def initialize(config = nil, options = nil)
@config = config || Config.new
@options = options || { debug: false }
reset_investigation
end
@@ -90,13 +148,10 @@
# When refining this method, always call `super`
def on_other_file
# Typically do nothing here
end
- # Override and return the Force class(es) you need to join
- def self.joining_forces; end
-
# Gets called if no message is specified when calling `add_offense` or
# `add_global_offense`
# Cops are discouraged to override this; instead pass your message directly
def message(_range = nil)
self.class::MSG
@@ -145,52 +200,10 @@
# ie when the ResultCache should be invalidated.
def external_dependency_checksum
nil
end
- def self.inherited(subclass)
- super
- Registry.global.enlist(subclass)
- end
-
- # Call for abstract Cop classes
- def self.exclude_from_registry
- Registry.global.dismiss(self)
- end
-
- # Returns if class supports autocorrect.
- # It is recommended to extend AutoCorrector instead of overriding
- def self.support_autocorrect?
- false
- end
-
- ### Naming
-
- def self.badge
- @badge ||= Badge.for(name)
- end
-
- def self.cop_name
- badge.to_s
- end
-
- def self.department
- badge.department
- end
-
- def self.lint?
- department == :Lint
- end
-
- # Returns true if the cop name or the cop namespace matches any of the
- # given names.
- def self.match?(given_names)
- return false unless given_names
-
- given_names.include?(cop_name) || given_names.include?(badge.department_name)
- end
-
def cop_name
@cop_name ||= self.class.cop_name
end
alias name cop_name
@@ -238,23 +251,10 @@
# There should be very limited reasons for a Cop to do it's own parsing
def parse(source, path = nil)
ProcessedSource.new(source, target_ruby_version, path)
end
- ### Persistence
-
- # Override if your cop should be called repeatedly for multiple investigations
- # Between calls to `on_new_investigation` and `on_investigation_end`,
- # the result of `processed_source` will remain constant.
- # You should invalidate any caches that depend on the current `processed_source`
- # in the `on_new_investigation` callback.
- # If your cop does autocorrections, be aware that your instance may be called
- # multiple times with the same `processed_source.path` but different content.
- def self.support_multiple_source?
- false
- end
-
# @api private
# Called between investigations
def ready
return self if self.class.support_multiple_source?
@@ -269,10 +269,11 @@
'they are returned as the result of the investigation'
end
### Reserved for Commissioner
+ # rubocop:disable Layout/ClassStructure
# @api private
def callbacks_needed
self.class.callbacks_needed
end
@@ -281,10 +282,11 @@
@callbacks_needed ||= public_instance_methods.select do |m|
m.match?(/^on_|^after_/) &&
!Base.method_defined?(m) # exclude standard "callbacks" like 'on_begin_investigation'
end
end
+ # rubocop:enable Layout/ClassStructure
private
### Reserved for Cop::Cop
@@ -325,15 +327,12 @@
@currently_disabled_lines = nil
@processed_source = processed_source
@current_corrector = nil
end
- # rubocop:disable Layout/ClassStructure
EMPTY_OFFENSES = [].freeze
private_constant :EMPTY_OFFENSES
- # rubocop:enable Layout/ClassStructure
-
# Called to complete an investigation
def complete_investigation
InvestigationReport.new(
self, processed_source, @current_offenses || EMPTY_OFFENSES, @current_corrector
)
@@ -341,16 +340,18 @@
reset_investigation
end
### Actually private methods
+ # rubocop:disable Layout/ClassStructure
def self.builtin?
return false unless (m = instance_methods(false).first) # any custom method will do
path, _line = instance_method(m).source_location
path.start_with?(__dir__)
end
private_class_method :builtin?
+ # rubocop:enable Layout/ClassStructure
def reset_investigation
@currently_disabled_lines = @current_offenses = @processed_source = @current_corrector = nil
end