Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #9304] Do not register offense for Style/ExplicitBlockArgument when the yield arguments are not an exact match with the block arguments. #9310

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_do_not_register_offense_for.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9304](https://github.com/rubocop-hq/rubocop/issues/9304): Do not register an offense for `Style/ExplicitBlockArgument` when the `yield` arguments are not an exact match with the block arguments. ([@dvandersluis][])
3 changes: 1 addition & 2 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3271,9 +3271,8 @@ Style/ExplicitBlockArgument:
that just passes its arguments to another block.
StyleGuide: '#block-argument'
Enabled: true
# May change the yielding arity.
Safe: false
VersionAdded: '0.89'
VersionChanged: <<next>>

Style/ExponentialNotation:
Description: 'When using exponential notation, favor a mantissa between 1 (inclusive) and 10 (exclusive).'
Expand Down
10 changes: 10 additions & 0 deletions lib/rubocop/cop/style/explicit_block_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module Style
# This cop enforces the use of explicit block argument to avoid writing
# block literal that just passes its arguments to another block.
#
# NOTE: This cop only registers an offense if the block args match the
dvandersluis marked this conversation as resolved.
Show resolved Hide resolved
# yield args exactly.
#
# @example
# # bad
# def with_tmp_dir
Expand Down Expand Up @@ -75,7 +78,14 @@ def on_yield(node)
private

def yielding_arguments?(block_args, yield_args)
yield_args = yield_args.dup.fill(
nil,
yield_args.length, block_args.length - yield_args.length
)

yield_args.zip(block_args).all? do |yield_arg, block_arg|
next false unless yield_arg && block_arg

block_arg && yield_arg.children.first == block_arg.children.first
end
end
Expand Down
28 changes: 22 additions & 6 deletions spec/rubocop/cop/style/explicit_block_argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@ def m(&block)
RUBY
end

it 'registers an offense and corrects when block yields several first its arguments' do
it 'registers an offense and corrects when multiple arguments are yielded' do
expect_offense(<<~RUBY)
def m
items.something { |i, j| yield i }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Consider using explicit block argument in the surrounding method's signature over `yield`.
items.something(first_arg) { |i, j| yield i, j }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Consider using explicit block argument in the surrounding method's signature over `yield`.
end
RUBY

expect_correction(<<~RUBY)
def m(&block)
items.something(&block)
items.something(first_arg, &block)
end
RUBY
end

it 'does not register an offense when arguments are yielded in a different order' do
expect_no_offenses(<<~RUBY)
def m
items.something(first_arg) { |i, j| yield j, i }
end
RUBY
end
Expand Down Expand Up @@ -92,8 +100,8 @@ def m
3.times { yield }
^^^^^^^^^^^^^^^^^ Consider using explicit block argument in the surrounding method's signature over `yield`.
else
other_items.something { |i, j| yield i }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Consider using explicit block argument in the surrounding method's signature over `yield`.
other_items.something { |i, j| yield i, j }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Consider using explicit block argument in the surrounding method's signature over `yield`.
end
end
RUBY
Expand Down Expand Up @@ -155,6 +163,14 @@ def m
RUBY
end

it 'does not register an offense when there is more than one block argument and not all are yielded' do
expect_no_offenses(<<~RUBY)
def m
items.something { |i, j| yield i }
end
RUBY
end

it 'does not register an offense when code is called outside of a method' do
expect_no_offenses(<<~RUBY)
render("partial") do
Expand Down