Skip to content

Commit

Permalink
Increase branch coverage of RSpec/Focus
Browse files Browse the repository at this point in the history
The corrector logic for pair_type/str_type/sym_type seems like a
"generic" implementation, so I move things around to let that be a
"default" case in the if/else block.

Also, inlining `#focus_metadata` into `#on_send` means we don't even
need to check the type.

Co-authored-by: Phil Pirozhkov <[email protected]>
  • Loading branch information
bquorning and pirj committed Feb 10, 2025
1 parent 3d46503 commit d181805
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/rubocop/cop/rspec/focus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,26 @@ class Focus < Base
(send #rspec? {#ExampleGroups.focused #Examples.focused} ...)
PATTERN

# rubocop:disable Metrics/MethodLength
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?
if focused_block?(node)
add_offense(node) do |corrector|
correct_send(corrector, node)
end
else
metadata(node) do |focus|
add_offense(focus) do |corrector|
corrector.remove(with_surrounding(focus))
elsif focus.send_type?
correct_send(corrector, focus)
end
end
end
end
# rubocop:enable Metrics/MethodLength

private

def focus_metadata(node, &block)
yield(node) if focused_block?(node)

metadata(node, &block)
end

def with_surrounding(focus)
range_with_space =
range_with_surrounding_space(focus.source_range, side: :left)
Expand Down

0 comments on commit d181805

Please sign in to comment.