lib/rubocop/cop/rspec/implicit_subject.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/implicit_subject.rb in rubocop-rspec-1.42.0
- old
+ new
@@ -25,10 +25,11 @@
#
# # good
# it { expect(subject).to be_truthy }
#
class ImplicitSubject < Cop
+ extend AutoCorrector
include ConfigurableEnforcedStyle
MSG = "Don't use implicit subject."
def_node_matcher :implicit_subject?, <<-PATTERN
@@ -37,24 +38,26 @@
def on_send(node)
return unless implicit_subject?(node)
return if valid_usage?(node)
- add_offense(node)
+ add_offense(node) do |corrector|
+ autocorrect(corrector, node)
+ end
end
- def autocorrect(node)
+ private
+
+ def autocorrect(corrector, node)
replacement = 'expect(subject)'
if node.method_name == :should
replacement += '.to'
elsif node.method_name == :should_not
replacement += '.not_to'
end
- ->(corrector) { corrector.replace(node.loc.selector, replacement) }
+ corrector.replace(node.loc.selector, replacement)
end
-
- private
def valid_usage?(node)
example = node.ancestors.find { |parent| example?(parent) }
return false if example.nil?