lib/rubocop/cop/rspec/hook_argument.rb in rubocop-rspec-1.15.1 vs lib/rubocop/cop/rspec/hook_argument.rb in rubocop-rspec-1.16.0
- old
+ new
@@ -64,14 +64,14 @@
EXPLICIT_MSG = 'Use `%p` for RSpec hooks.'.freeze
HOOKS = Hooks::ALL.node_pattern_union.freeze
def_node_matcher :scoped_hook, <<-PATTERN
- (block $(send nil #{HOOKS} (sym ${:each :example})) ...)
+ (block $(send _ #{HOOKS} (sym ${:each :example})) ...)
PATTERN
- def_node_matcher :unscoped_hook, "(block $(send nil #{HOOKS}) ...)"
+ def_node_matcher :unscoped_hook, "(block $(send _ #{HOOKS}) ...)"
def on_block(node)
hook(node) do |method_send, scope_name|
return correct_style_detected if scope_name.equal?(style)
return check_implicit(method_send) unless scope_name
@@ -80,15 +80,14 @@
add_offense(method_send, :expression, explicit_message(scope_name))
end
end
def autocorrect(node)
- scope = "(#{style.inspect})" unless implicit_style?
- hook = "#{node.method_name}#{scope}"
+ scope = implicit_style? ? '' : "(#{style.inspect})"
lambda do |corrector|
- corrector.replace(node.loc.expression, hook)
+ corrector.replace(argument_range(node), scope)
end
end
private
@@ -111,9 +110,16 @@
style.equal?(:implicit)
end
def hook(node, &block)
scoped_hook(node, &block) || unscoped_hook(node, &block)
+ end
+
+ def argument_range(send_node)
+ range_between(
+ send_node.loc.selector.end_pos,
+ send_node.loc.expression.end_pos
+ )
end
end
end
end
end