Skip to content

Commit

Permalink
Make Style/RedundantSelf allow self.it
Browse files Browse the repository at this point in the history
This PR makes `Style/RedundantSelf` allow the `self.it` method in a block.

It respects following Ruby 3.3's warning:

```console
$ ruby -e '0.times { begin; it; end }'
-e:1: warning: `it` calls without arguments will refer to the first block param in Ruby 3.4; use it() or self.it
```

Related PR ... rubocop#12518
  • Loading branch information
koic committed Dec 11, 2023
1 parent 42a5d50 commit 70fdd80
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12523](https://github.com/rubocop/rubocop/pull/12523): Make `Style/RedundantSelf` allow the `self.it` method in a block. ([@koic][])
19 changes: 17 additions & 2 deletions lib/rubocop/cop/style/redundant_self.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module Style
# protected scope, you cannot send private messages this way.
#
# Note we allow uses of `self` with operators because it would be awkward
# otherwise.
# otherwise. Also allows the use of `self.it` without arguments in blocks,
# as in `0.times { self.it }`, following `Lint/ItWithoutArgumentsInBlock` cop.
#
# @example
#
Expand Down Expand Up @@ -107,8 +108,8 @@ def on_in_pattern(node)
def on_send(node)
return unless node.self_receiver? && regular_method_call?(node)
return if node.parent&.mlhs_type?

return if allowed_send_node?(node)
return if it_method_in_block?(node)

add_offense(node.receiver) do |corrector|
corrector.remove(node.receiver)
Expand Down Expand Up @@ -155,6 +156,20 @@ def allowed_send_node?(node)
KERNEL_METHODS.include?(node.method_name)
end

# Respects `Lint/ItWithoutArgumentsInBlock` cop and the following Ruby 3.3's warning:
#
# $ ruby -e '0.times { begin; it; end }'
# -e:1: warning: `it` calls without arguments will refer to the first block param in
# Ruby 3.4; use it() or self.it
#
def it_method_in_block?(node)
return false unless node.method?(:it)
return false unless (block_node = node.each_ancestor(:block).first)
return false unless block_node.arguments.empty_and_without_delimiters?

node.arguments.empty? && !node.block_literal?
end

def regular_method_call?(node)
!(node.operator_method? ||
KEYWORDS.include?(node.method_name) ||
Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/cop/style/redundant_self_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,51 @@ def do_something((a, b)) # This method expects Array that has 2 elements as argu
RUBY
end

it 'does not register an offense when using `self.it` in the single line block' do
# `Lint/ItWithoutArgumentsInBlock` respects for this syntax.
expect_no_offenses(<<~RUBY)
0.times { self.it }
RUBY
end

it 'does not register an offense when using `self.it` in the multiline block' do
# `Lint/ItWithoutArgumentsInBlock` respects for this syntax.
expect_no_offenses(<<~RUBY)
0.times do
self.it
it = 1
it
end
RUBY
end

it 'registers an offense when using `it` without arguments in `def` body' do
expect_offense(<<~RUBY)
def foo
self.it
^^^^ Redundant `self` detected.
end
RUBY
end

it 'registers an offense when using `it` without arguments in the block with empty block parameter' do
expect_offense(<<~RUBY)
0.times { ||
self.it
^^^^ Redundant `self` detected.
}
RUBY
end

it 'registers an offense when using `it` without arguments in the block with useless block parameter' do
expect_offense(<<~RUBY)
0.times { |_n|
self.it
^^^^ Redundant `self` detected.
}
RUBY
end

context 'with ruby >= 2.7', :ruby27 do
context 'with pattern matching' do
it 'accepts a self receiver on an `match-var`' do
Expand Down

0 comments on commit 70fdd80

Please sign in to comment.