lib/rubocop/cop/rspec/hook_argument.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/hook_argument.rb in rubocop-rspec-1.42.0
- old
+ new
@@ -56,10 +56,11 @@
# # good
# before(:example) do
# # ...
# end
class HookArgument < Cop
+ extend AutoCorrector
include ConfigurableEnforcedStyle
IMPLICIT_MSG = 'Omit the default `%<scope>p` ' \
'argument for RSpec hooks.'
EXPLICIT_MSG = 'Use `%<scope>p` for RSpec hooks.'
@@ -76,35 +77,28 @@
hook(node) do |method_send, scope_name|
return correct_style_detected if scope_name.equal?(style)
return check_implicit(method_send) unless scope_name
style_detected(scope_name)
- add_offense(
- method_send,
- message: explicit_message(scope_name)
- )
+ msg = explicit_message(scope_name)
+ add_offense(method_send, message: msg) do |corrector|
+ scope = implicit_style? ? '' : "(#{style.inspect})"
+ corrector.replace(argument_range(method_send), scope)
+ end
end
end
- def autocorrect(node)
- scope = implicit_style? ? '' : "(#{style.inspect})"
-
- lambda do |corrector|
- corrector.replace(argument_range(node), scope)
- end
- end
-
private
def check_implicit(method_send)
style_detected(:implicit)
return if implicit_style?
- add_offense(
- method_send,
- location: :selector,
- message: format(EXPLICIT_MSG, scope: style)
- )
+ msg = explicit_message(nil)
+ add_offense(method_send.loc.selector, message: msg) do |corrector|
+ scope = "(#{style.inspect})"
+ corrector.replace(argument_range(method_send), scope)
+ end
end
def explicit_message(scope)
if implicit_style?
format(IMPLICIT_MSG, scope: scope)