lib/rubocop/cop/cop.rb in rubocop-0.34.2 vs lib/rubocop/cop/cop.rb in rubocop-0.35.0
- old
+ new
@@ -47,11 +47,11 @@
# A scaffold for concrete cops.
#
# The Cop class is meant to be extended.
#
- # Cops track offenses and can autocorrect them of the fly.
+ # Cops track offenses and can autocorrect them on the fly.
#
# A commissioner object is responsible for traversing the AST and invoking
# the specific callbacks on each cop.
# If a cop needs to do its own processing of the AST or depends on
# something else, it should define the `#investigate` method and do
@@ -64,10 +64,11 @@
# # Do custom processing
# end
# end
class Cop
extend AST::Sexp
+ extend NodePattern::Macros
include Util
include IgnoredNode
include AutocorrectLogic
attr_reader :config, :offenses, :corrections
@@ -136,10 +137,15 @@
(style_guide_url || reference_url) &&
(@options[:display_style_guide] ||
config['AllCops'] && config['AllCops']['DisplayStyleGuide'])
end
+ def extra_details?
+ @options[:extra_details] ||
+ config['AllCops'] && config['AllCops']['ExtraDetails']
+ 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
@@ -177,11 +183,12 @@
@corrections << correction
:corrected
end
def config_to_allow_offenses
- Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name]
+ Formatter::DisabledConfigFormatter
+ .config_to_allow_offenses[cop_name] ||= {}
end
def config_to_allow_offenses=(hash)
Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] =
hash
@@ -201,32 +208,38 @@
def excluded_file?(file)
!relevant_file?(file)
end
def style_guide_url
- url = cop_config && cop_config['StyleGuide']
+ url = cop_config['StyleGuide']
(url.nil? || url.empty?) ? nil : url
end
def reference_url
- url = cop_config && cop_config['Reference']
+ url = cop_config['Reference']
(url.nil? || url.empty?) ? nil : url
end
+ def details
+ details = cop_config && cop_config['Details']
+ (details.nil? || details.empty?) ? nil : details
+ end
+
private
def annotate_message(message)
message = "#{name}: #{message}" if display_cop_names?
+ message += " #{details}" if extra_details?
if display_style_guide?
links = [style_guide_url, reference_url].compact.join(', ')
message = "#{message} (#{links})"
end
message
end
def file_name_matches_any?(file, parameter, default_result)
- patterns = cop_config && cop_config[parameter]
+ patterns = cop_config[parameter]
return default_result unless patterns
path = nil
patterns.any? do |pattern|
# Try to match the absolute path, as Exclude properties are absolute.
next true if match_path?(pattern, file, config.loaded_path)
@@ -245,10 +258,10 @@
def default_severity
self.class.lint? ? :warning : :convention
end
def custom_severity
- severity = cop_config && cop_config['Severity']
+ severity = cop_config['Severity']
return unless severity
if Severity::NAMES.include?(severity.to_sym)
severity.to_sym
else