lib/rubocop/cop/rspec/leading_subject.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/leading_subject.rb in rubocop-rspec-1.42.0
- old
+ new
@@ -30,10 +30,12 @@
# subject { described_class.new(params) }
# it { expect_something }
# it { expect_something_else }
#
class LeadingSubject < Cop
+ extend AutoCorrector
+
MSG = 'Declare `subject` above any other `%<offending>s` declarations.'
def on_block(node)
return unless subject?(node) && !in_spec_block?(node)
@@ -41,29 +43,27 @@
end
def check_previous_nodes(node)
node.parent.each_child_node do |sibling|
if offending?(sibling)
- add_offense(
- node,
- message: format(MSG, offending: sibling.method_name)
- )
+ msg = format(MSG, offending: sibling.method_name)
+ add_offense(node, message: msg) do |corrector|
+ autocorrect(corrector, node)
+ end
end
break if offending?(sibling) || sibling.equal?(node)
end
end
- def autocorrect(node)
- lambda do |corrector|
- first_node = find_first_offending_node(node)
- RuboCop::RSpec::Corrector::MoveNode.new(
- node, corrector, processed_source
- ).move_before(first_node)
- end
- end
-
private
+
+ def autocorrect(corrector, node)
+ first_node = find_first_offending_node(node)
+ RuboCop::RSpec::Corrector::MoveNode.new(
+ node, corrector, processed_source
+ ).move_before(first_node)
+ end
def offending?(node)
let?(node) || hook?(node) || example?(node)
end