Skip to content

Commit 887a06d

Browse files
committed
Don't spend CPU finding the same node twice
1 parent b948759 commit 887a06d

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

lib/rubocop/cop/rspec/leading_subject.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def check_previous_nodes(node)
4747
if offending?(sibling)
4848
msg = format(MSG, offending: sibling.method_name)
4949
add_offense(node, message: msg) do |corrector|
50-
autocorrect(corrector, node)
50+
autocorrect(corrector, node, sibling)
5151
end
5252
end
5353

@@ -57,21 +57,16 @@ def check_previous_nodes(node)
5757

5858
private
5959

60-
def autocorrect(corrector, node)
61-
first_node = find_first_offending_node(node)
60+
def autocorrect(corrector, node, sibling)
6261
RuboCop::RSpec::Corrector::MoveNode.new(
6362
node, corrector, processed_source
64-
).move_before(first_node)
63+
).move_before(sibling)
6564
end
6665

6766
def offending?(node)
6867
let?(node) || hook?(node) || example?(node)
6968
end
7069

71-
def find_first_offending_node(node)
72-
node.parent.children.find { |sibling| offending?(sibling) }
73-
end
74-
7570
def in_spec_block?(node)
7671
node.each_ancestor(:block).any? do |ancestor|
7772
example?(ancestor)

0 commit comments

Comments
 (0)