Skip to content

Commit

Permalink
[Fix rubocop#9684] Support IgnoredMethods option for `Lint/Ambiguou…
Browse files Browse the repository at this point in the history
…sBlockAssociation`
  • Loading branch information
gPrado committed Apr 9, 2021
1 parent 1e55b1a commit 7e838aa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
@@ -0,0 +1 @@
* [#9684](https://github.com/rubocop/rubocop/issues/9684): Support `IgnoredMethods` option for `Lint/AmbguousBlockAssociation`. ([@gprado][])
2 changes: 2 additions & 0 deletions config/default.yml
Expand Up @@ -1387,6 +1387,8 @@ Lint/AmbiguousBlockAssociation:
StyleGuide: '#syntax'
Enabled: true
VersionAdded: '0.48'
VersionChanged: '1.13'
IgnoredMethods: []

Lint/AmbiguousOperator:
Description: >-
Expand Down
12 changes: 11 additions & 1 deletion docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -40,7 +40,7 @@ x != y # or x = !y
| Yes
| No
| 0.48
| -
| 1.13
|===

This cop checks for ambiguous block association with method
Expand Down Expand Up @@ -71,6 +71,16 @@ foo == bar { |b| b.baz }
foo = ->(bar) { bar.baz }
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| IgnoredMethods
| `[]`
| Array
|===

=== References

* https://rubystyle.guide#syntax
Expand Down
12 changes: 11 additions & 1 deletion lib/rubocop/cop/lint/ambiguous_block_association.rb
Expand Up @@ -6,6 +6,8 @@ module Lint
# This cop checks for ambiguous block association with method
# when param passed without parentheses.
#
# This cop can customize ignored methods with `IgnoredMethods`.
#
# @example
#
# # bad
Expand All @@ -26,7 +28,14 @@ module Lint
# # good
# # Lambda arguments require no disambiguation
# foo = ->(bar) { bar.baz }
#
# @example IgnoredMethods: [change]
#
# # good
# expect { do_something }.to change { object.attribute }
class AmbiguousBlockAssociation < Base
include IgnoredMethods

MSG = 'Parenthesize the param `%<param>s` to make sure that the ' \
'block will be associated with the `%<method>s` method ' \
'call.'
Expand All @@ -50,7 +59,8 @@ def ambiguous_block_association?(send_node)
end

def allowed_method?(node)
node.assignment? || node.operator_method? || node.method?(:[])
node.assignment? || node.operator_method? || node.method?(:[]) ||
ignored_method?(node.last_argument.send_node.source)
end

def message(send_node)
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/lint/ambiguous_block_association_spec.rb
Expand Up @@ -83,4 +83,21 @@
end
end
end

context 'IgnoredMethods' do
let(:cop_config) { { 'IgnoredMethods' => %w[change] } }

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

it 'registers an offense for other methods' do
expect_offense(<<~RUBY)
expect { order.expire }.to update { order.events }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `update { order.events }` to make sure that the block will be associated with the `update` method call.
RUBY
end
end
end

0 comments on commit 7e838aa

Please sign in to comment.