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 #9684] Support IgnoredMethods option for Lint/AmbiguousBlockAssociation #9685

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 @@
* [#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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I had forgotten the mixing was named like this. You can escape from legacy. :D


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