lib/rubocop/cop/cop.rb in rubocop-0.50.0 vs lib/rubocop/cop/cop.rb in rubocop-0.51.0

- old
+ new

@@ -103,34 +103,27 @@ def join_force?(_force_class) false end def cop_config - @cop_config ||= @config.for_cop(self) + # Use department configuration as basis, but let individual cop + # configuration override. + @cop_config ||= @config.for_cop(self.class.department.to_s) + .merge(@config.for_cop(self)) end def message(_node = nil) self.class::MSG end - # rubocop:disable Metrics/CyclomaticComplexity - def add_offense(node, loc = :expression, message = nil, severity = nil) - location = find_location(node, loc) - - return if duplicate_location?(location) - - severity = custom_severity || severity || default_severity - - message ||= message(node) - message = annotate(message) - - status = enabled_line?(location.line) ? correct(node) : :disabled - - @offenses << Offense.new(severity, location, message, name, status) - yield if block_given? && status != :disabled + def add_offense(node, *args, **kwargs, &block) + if args.any? + add_offense_deprecated(node, *args, &block) + else + add_offense_common(node, **kwargs, &block) + end end - # rubocop:enable Metrics/CyclomaticComplexity 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 @@ -227,8 +220,48 @@ message = "Warning: Invalid severity '#{severity}'. " \ "Valid severities are #{Severity::NAMES.join(', ')}." warn(Rainbow(message).red) end end + + def add_offense_deprecated(node, loc = :expression, message = nil, + severity = nil, &block) + + caller = caller_locations(2..2).first + path = "#{caller.path}:#{caller.lineno}" + warn_message = <<-RUBY.strip_indent + #{path} + Warning: The usage of positional location, message, and severity + parameters to Cop#add_offense is deprecated. + Please use keyword arguments instead. + + The positional arguments version of Cop#add_offense will be removed in + RuboCop 0.52 + RUBY + + warn(Rainbow(warn_message).red) + + add_offense_common(node, location: loc, message: message, + severity: severity, &block) + end + + # rubocop:disable Metrics/CyclomaticComplexity + def add_offense_common(node, location: :expression, message: nil, + severity: nil) + loc = find_location(node, location) + + return if duplicate_location?(loc) + + severity = custom_severity || severity || default_severity + + message ||= message(node) + message = annotate(message) + + status = enabled_line?(loc.line) ? correct(node) : :disabled + + @offenses << Offense.new(severity, loc, message, name, status) + yield if block_given? && status != :disabled + end + # rubocop:enable Metrics/CyclomaticComplexity end end end