Skip to content

Commit

Permalink
Don't spend CPU finding the same node twice
Browse files Browse the repository at this point in the history
  • Loading branch information
bquorning committed Jul 10, 2020
1 parent cd69228 commit 3845c0f
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/rubocop/cop/rspec/leading_subject.rb
Expand Up @@ -47,7 +47,7 @@ def check_previous_nodes(node)
if offending?(sibling)
msg = format(MSG, offending: sibling.method_name)
add_offense(node, message: msg) do |corrector|
autocorrect(corrector, node)
autocorrect(corrector, node, sibling)
end
end

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

private

def autocorrect(corrector, node)
first_node = find_first_offending_node(node)
def autocorrect(corrector, node, sibling)
RuboCop::RSpec::Corrector::MoveNode.new(
node, corrector, processed_source
).move_before(first_node)
).move_before(sibling)
end

def offending?(node)
let?(node) || hook?(node) || example?(node)
end

def find_first_offending_node(node)
node.parent.children.find { |sibling| offending?(sibling) }
end

def in_spec_block?(node)
node.each_ancestor(:block).any? do |ancestor|
example?(ancestor)
Expand Down

0 comments on commit 3845c0f

Please sign in to comment.