lib/rubocop/cop/rspec/described_class.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/described_class.rb in rubocop-rspec-1.42.0
- old
+ new
@@ -53,10 +53,11 @@
# include MyConcern
# end
# end
#
class DescribedClass < Cop
+ extend AutoCorrector
include ConfigurableEnforcedStyle
DESCRIBED_CLASS = 'described_class'
MSG = 'Use `%<replacement>s` instead of `%<src>s`.'
@@ -83,25 +84,27 @@
@described_class, body = described_constant(node)
return unless body
find_usage(body) do |match|
- add_offense(match, message: message(match.const_name))
+ msg = message(match.const_name)
+ add_offense(match, message: msg) do |corrector|
+ autocorrect(corrector, match)
+ end
end
end
- def autocorrect(node)
+ private
+
+ def autocorrect(corrector, match)
replacement = if style == :described_class
DESCRIBED_CLASS
else
@described_class.const_name
end
- lambda do |corrector|
- corrector.replace(node.loc.expression, replacement)
- end
- end
- private
+ corrector.replace(match, replacement)
+ end
def find_usage(node, &block)
yield(node) if offensive?(node)
return if scope_change?(node) || node.const_type?