Skip to content

Commit

Permalink
Merge pull request #2033 from rubocop/focus-cop-coverage
Browse files Browse the repository at this point in the history
Refactor RSpec/Focus, increase branch coverage
  • Loading branch information
bquorning authored Feb 10, 2025
2 parents 3d46503 + a78f6c7 commit 2e3cca0
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/rubocop/cop/rspec/focus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,27 @@ class Focus < Base
def on_send(node)
return if node.chained? || node.each_ancestor(:def, :defs).any?

focus_metadata(node) do |focus|
add_offense(focus) do |corrector|
if focus.pair_type? || focus.str_type? || focus.sym_type?
corrector.remove(with_surrounding(focus))
elsif focus.send_type?
correct_send(corrector, focus)
end
if focused_block?(node)
on_focused_block(node)
else
metadata(node) do |focus|
on_metadata(focus)
end
end
end

private

def focus_metadata(node, &block)
yield(node) if focused_block?(node)
def on_focused_block(node)
add_offense(node) do |corrector|
correct_send(corrector, node)
end
end

metadata(node, &block)
def on_metadata(node)
add_offense(node) do |corrector|
corrector.remove(with_surrounding(node))
end
end

def with_surrounding(focus)
Expand Down

0 comments on commit 2e3cca0

Please sign in to comment.