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 rubocop#10969] Allow Lint/AmbiguousBlockAssociation `AllowedPa… #10970

Merged
merged 1 commit into from Aug 29, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#10969](https://github.com/rubocop/rubocop/issues/10969): Fix a false negative for `AllowedPatterns` of `Lint/AmbiguousBlockAssociation` when using a method chain. ([@jcalvert][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/ambiguous_block_association.rb
Expand Up @@ -81,7 +81,7 @@ def ambiguous_block_association?(send_node)
def allowed_method_pattern?(node)
node.assignment? || node.operator_method? || node.method?(:[]) ||
allowed_method?(node.last_argument.method_name) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the same problem with AllowedMethods?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there it's fine to match on the name. Generally, it's a bit unclear which patterns would need to match on the entire send node's source.

Copy link
Member

@koic koic Aug 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AllowedMethods and AllowedPatterns have different uses, so AllowedMethods seems fine.

matches_allowed_pattern?(node.last_argument.method_name)
matches_allowed_pattern?(node.last_argument.send_node.source)
end

def message(send_node)
Expand Down
7 changes: 5 additions & 2 deletions spec/rubocop/cop/lint/ambiguous_block_association_spec.rb
Expand Up @@ -104,13 +104,16 @@
end

context 'when AllowedPatterns is enabled' do
let(:cop_config) { { 'AllowedPatterns' => [/change/] } }
let(:cop_config) { { 'AllowedPatterns' => [/change/, /receive\(.*?\)\.twice/] } }

it 'does not register an offense for an allowed method' do
expect_no_offenses(<<~RUBY)
expect { order.expire }.to change { order.events }
expect { order.expire }.to not_change { order.events }
RUBY

expect_no_offenses(<<~RUBY)
expect(order).to receive(:complete).twice { OrderCount.update! }
RUBY
end

it 'registers an offense for other methods' do
Expand Down