lib/rubocop/cop/base.rb in rubocop-1.40.0 vs lib/rubocop/cop/base.rb in rubocop-1.41.0
- old
+ new
@@ -105,12 +105,11 @@
# Adds an offense that has no particular location.
# No correction can be applied to global offenses
def add_global_offense(message = nil, severity: nil)
severity = find_severity(nil, severity)
message = find_message(nil, message)
- @current_offenses <<
- Offense.new(severity, Offense::NO_LOCATION, message, name, :unsupported)
+ current_offenses << Offense.new(severity, Offense::NO_LOCATION, message, name, :unsupported)
end
# 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.
@@ -124,11 +123,11 @@
severity = find_severity(range_to_pass, severity)
message = find_message(range_to_pass, message)
status, corrector = enabled_line?(range.line) ? correct(range, &block) : :disabled
- @current_offenses << Offense.new(severity, range, message, name, status, corrector)
+ current_offenses << Offense.new(severity, range, message, name, status, corrector)
end
# This method should be overridden when a cop's behavior depends
# on state that lives outside of these locations:
#
@@ -185,11 +184,11 @@
# 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?(department.to_s)
+ given_names.include?(cop_name) || given_names.include?(badge.department_name)
end
def cop_name
@cop_name ||= self.class.cop_name
end
@@ -223,10 +222,12 @@
def active_support_extensions_enabled?
@config.active_support_extensions_enabled?
end
def relevant_file?(file)
+ return true unless @config.clusivity_config_for_badge?(self.class.badge)
+
file == RuboCop::AST::ProcessedSource::STRING_SOURCE_NAME ||
(file_name_matches_any?(file, 'Include', true) &&
!file_name_matches_any?(file, 'Exclude', false))
end
@@ -290,11 +291,11 @@
def callback_argument(range)
range
end
def apply_correction(corrector)
- @current_corrector&.merge!(corrector) if corrector
+ current_corrector&.merge!(corrector) if corrector
end
### Reserved for Commissioner:
def current_offense_locations
@@ -303,26 +304,41 @@
def currently_disabled_lines
@currently_disabled_lines ||= Set.new
end
+ def current_corrector
+ @current_corrector ||= Corrector.new(@processed_source) if @processed_source.valid_syntax?
+ end
+
+ def current_offenses
+ @current_offenses ||= []
+ end
+
private_class_method def self.restrict_on_send
@restrict_on_send ||= self::RESTRICT_ON_SEND.to_a.freeze
end
# Called before any investigation
def begin_investigation(processed_source)
- @current_offenses = []
+ @current_offenses = nil
@current_offense_locations = nil
@currently_disabled_lines = nil
@processed_source = processed_source
- @current_corrector = Corrector.new(@processed_source) if @processed_source.valid_syntax?
+ @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, @current_corrector)
+ InvestigationReport.new(
+ self, processed_source, @current_offenses || EMPTY_OFFENSES, @current_corrector
+ )
ensure
reset_investigation
end
### Actually private methods
@@ -410,10 +426,10 @@
def file_name_matches_any?(file, parameter, default_result)
patterns = cop_config[parameter]
return default_result unless patterns
- patterns = OptimizedPatterns.from(patterns)
+ patterns = FilePatterns.from(patterns)
patterns.match?(config.path_relative_to_config(file)) || patterns.match?(file)
end
def enabled_line?(line_number)
return true if @options[:ignore_disable_comments] || !@processed_source