From 2aaecd626f883d54545b4774d2a7940bffab742f Mon Sep 17 00:00:00 2001 From: Jonathan Calvert Date: Fri, 26 Aug 2022 14:53:54 -0500 Subject: [PATCH] [Fix rubocop#10969] Allow `Lint/AmbiguousBlockAssociation` `AllowedPattern` to match on `send_node.source` instead of `method_name`. --- ...ix_allow_ambigious_block_association_to_match_source.md | 1 + lib/rubocop/cop/lint/ambiguous_block_association.rb | 2 +- spec/rubocop/cop/lint/ambiguous_block_association_spec.rb | 7 +++++-- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_allow_ambigious_block_association_to_match_source.md diff --git a/changelog/fix_allow_ambigious_block_association_to_match_source.md b/changelog/fix_allow_ambigious_block_association_to_match_source.md new file mode 100644 index 00000000000..70cd36ac9a6 --- /dev/null +++ b/changelog/fix_allow_ambigious_block_association_to_match_source.md @@ -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][]) diff --git a/lib/rubocop/cop/lint/ambiguous_block_association.rb b/lib/rubocop/cop/lint/ambiguous_block_association.rb index 8ea895fb0ec..1f316e01c6f 100644 --- a/lib/rubocop/cop/lint/ambiguous_block_association.rb +++ b/lib/rubocop/cop/lint/ambiguous_block_association.rb @@ -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) || - matches_allowed_pattern?(node.last_argument.method_name) + matches_allowed_pattern?(node.last_argument.send_node.source) end def message(send_node) diff --git a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb index d77adcff4da..9c009b1da7c 100644 --- a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb +++ b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb @@ -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