Skip to content

Commit

Permalink
Tweak offense range for Lint/RedundantSafeNavigation
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Feb 22, 2024
1 parent 1666278 commit 1d43035
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
12 changes: 5 additions & 7 deletions lib/rubocop/cop/lint/redundant_safe_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ module Lint
#
class RedundantSafeNavigation < Base
include AllowedMethods
include RangeHelp
extend AutoCorrector

MSG = 'Redundant safe navigation detected.'
MSG = 'Redundant safe navigation detected, use `.` instead.'
MSG_LITERAL = 'Redundant safe navigation with default literal detected.'

NIL_SPECIFIC_METHODS = (nil.methods - Object.new.methods).to_set.freeze
Expand Down Expand Up @@ -111,19 +110,18 @@ def on_csend(node)
return if respond_to_nil_specific_method?(node)
end

range = range_between(node.loc.dot.begin_pos, node.source_range.end_pos)
add_offense(range) { |corrector| corrector.replace(node.loc.dot, '.') }
range = node.loc.dot
add_offense(range) { |corrector| corrector.replace(range, '.') }
end

def on_or(node)
conversion_with_default?(node) do |send_node|
range = range_between(send_node.loc.dot.begin_pos, node.source_range.end_pos)
range = send_node.loc.dot.begin.join(node.source_range.end)

add_offense(range, message: MSG_LITERAL) do |corrector|
corrector.replace(send_node.loc.dot, '.')

range_with_default = range_between(node.lhs.source_range.end.begin_pos,
node.source_range.end.end_pos)
range_with_default = node.lhs.source_range.end.begin.join(node.source_range.end)
corrector.remove(range_with_default)
end
end
Expand Down
28 changes: 14 additions & 14 deletions spec/rubocop/cop/lint/redundant_safe_navigation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
it 'registers an offense and corrects when `&.` is used for camel case const receiver' do
expect_offense(<<~RUBY)
Const&.do_something
^^^^^^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
ConstName&.do_something
^^^^^^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
Const_name&.do_something # It is treated as camel case, similar to the `Naming/ConstantName` cop.
^^^^^^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -30,10 +30,10 @@
it 'registers an offense and corrects when `&.` is used inside `if` condition' do
expect_offense(<<~RUBY)
if foo&.respond_to?(:bar)
^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
do_something
elsif foo&.respond_to?(:baz)
^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
do_something_else
end
RUBY
Expand All @@ -50,7 +50,7 @@
it 'registers an offense and corrects when `&.` is used inside `unless` condition' do
expect_offense(<<~RUBY)
do_something unless foo&.respond_to?(:bar)
^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -61,7 +61,7 @@
it 'registers an offense and corrects when `&.` is used for string literals' do
expect_offense(<<~RUBY)
'2012-03-02 16:05:37'&.to_time
^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -72,7 +72,7 @@
it 'registers an offense and corrects when `&.` is used for integer literals' do
expect_offense(<<~RUBY)
42&.minutes
^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -83,7 +83,7 @@
it 'registers an offense and corrects when `&.` is used for array literals' do
expect_offense(<<~RUBY)
[1, 2, 3]&.join(', ')
^^^^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -94,7 +94,7 @@
it 'registers an offense and corrects when `&.` is used for hash literals' do
expect_offense(<<~RUBY)
{k: :v}&.count
^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -112,14 +112,14 @@
it 'registers an offense and corrects when `&.` is used inside `#{loop_type}` condition' do
expect_offense(<<~RUBY, loop_type: loop_type)
%{loop_type} foo&.respond_to?(:bar)
_{loop_type} ^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected.
_{loop_type} ^^ Redundant safe navigation detected, use `.` instead.
do_something
end
begin
do_something
end %{loop_type} foo&.respond_to?(:bar)
_{loop_type} ^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected.
_{loop_type} ^^ Redundant safe navigation detected, use `.` instead.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -137,8 +137,8 @@
it 'registers an offense and corrects when `&.` is used inside complex condition' do
expect_offense(<<~RUBY)
do_something if foo&.respond_to?(:bar) && !foo&.respond_to?(:baz)
^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected.
^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected.
^^ Redundant safe navigation detected, use `.` instead.
^^ Redundant safe navigation detected, use `.` instead.
RUBY

expect_correction(<<~RUBY)
Expand Down

0 comments on commit 1d43035

Please sign in to comment.