lib/rubocop/cop/rspec/multiple_subjects.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/multiple_subjects.rb in rubocop-rspec-1.42.0
- old
+ new
@@ -32,51 +32,50 @@
#
# - If subjects are defined with `subject!` then we don't autocorrect.
# This is enough of an edge case that people can just move this to
# a `before` hook on their own
class MultipleSubjects < Cop
+ extend AutoCorrector
include RangeHelp
MSG = 'Do not set more than one subject per example group'
def on_block(node)
return unless example_group?(node)
subjects = RuboCop::RSpec::ExampleGroup.new(node).subjects
subjects[0...-1].each do |subject|
- add_offense(subject)
+ add_offense(subject) do |corrector|
+ autocorrect(corrector, subject)
+ end
end
end
- def autocorrect(node)
- return unless node.method_name.equal?(:subject) # Ignore `subject!`
+ private
- if named_subject?(node)
- rename_autocorrect(node)
+ def autocorrect(corrector, subject)
+ return unless subject.method_name.equal?(:subject) # Ignore `subject!`
+
+ if named_subject?(subject)
+ rename_autocorrect(corrector, subject)
else
- remove_autocorrect(node)
+ remove_autocorrect(corrector, subject)
end
end
- private
-
def named_subject?(node)
node.send_node.arguments?
end
- def rename_autocorrect(node)
- lambda do |corrector|
- corrector.replace(node.send_node.loc.selector, 'let')
- end
+ def rename_autocorrect(corrector, node)
+ corrector.replace(node.send_node.loc.selector, 'let')
end
- def remove_autocorrect(node)
- lambda do |corrector|
- range = range_by_whole_lines(node.source_range,
- include_final_newline: true)
- corrector.remove(range)
- end
+ def remove_autocorrect(corrector, node)
+ range = range_by_whole_lines(node.source_range,
+ include_final_newline: true)
+ corrector.remove(range)
end
end
end
end
end