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

Lint/AmbiguousBlockAssociation Pattern Matching Regression #10969

Closed
jcalvert opened this issue Aug 26, 2022 · 6 comments · Fixed by #10970
Closed

Lint/AmbiguousBlockAssociation Pattern Matching Regression #10969

jcalvert opened this issue Aug 26, 2022 · 6 comments · Fixed by #10970

Comments

@jcalvert
Copy link
Contributor

I discovered what I believe to be a regression in the behavior of the Lint/AmbiguousBlockAssociation cop from v1.33.0 on. For a large code base, when upgrading RuboCop from 1.32 to the latest revision, I saw previously matching lines now fail.

When the commit was made deprecating IgnoredMethods for AllowedMethods and AllowedPatterns, the argument to ignored_method? was changed from node.last_argument.send_node.source to node.last_argument.method_name for matches_allowed_pattern?. This means that the string passed to the regular expression changed, so regular expressions that worked prior to 1.33.0 may not match. The output of the cop message still uses send_node.source for interpolation as well. This makes the message instructions difficult to mark for allowance because the output will include the whole method chain but only match on the last call.

Given the language of the output message it's possible the intention would be for this cop to only want the last method call in the chain to appear. The most common usage for the Allowed methods and patterns on this cop is for the rspec style - because of that I would provide color commentary that I think providing the whole method chain to match against the regular expression is more useful. Idiomatic rspec usage is typically in conjunction with other method calls like receive and with which help differentiate specs from legitimate uses where we would want the cop to trigger.


Expected behavior

for AllowedPatterns of [/receive\(.*?\)\.twice/]

expect the string

expect(order).to receive(:complete).twice { OrderCount.update! }

to have no offenses

Actual behavior

 expect(order).to receive(:complete).twice { OrderCount.update! }
       +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `receive(:complete).twice { OrderCount.update! }` to make sure that the block will be associated with the `receive(:complete).twice` method call.

Steps to reproduce the problem

Included above

RuboCop version

1.35.1 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 2.7.5 x86_64-linux)
  - rubocop-rails 2.15.2
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 26, 2022

@ydah, please look into this.

@ydah
Copy link
Member

ydah commented Aug 26, 2022

@jcalvert You are right, this problem is happening. I had missed it. Thank you.
Thanks also for sending us the PR. I will check that PR.

@ydah
Copy link
Member

ydah commented Aug 26, 2022

As I mentioned in #10970 (comment) , I think this is the same problem with AllowedMethods.

@koic
Copy link
Member

koic commented Aug 27, 2022

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

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 27, 2022

Agreed. AllowMethods should be doing only exact name matches.

@ydah
Copy link
Member

ydah commented Aug 27, 2022

IgnoredMethods may differ from the previous behavior strictly because if Regex is not specified, the comparison will be by equivalence operators like AllowedMethods is now. However, if the difference in behavior does not seem to be a problem, it seems to be a good.

jcalvert added a commit to jcalvert/rubocop that referenced this issue Aug 29, 2022
…ttern` to match on

`send_node.source` instead of `method_name`.
koic added a commit that referenced this issue Aug 29, 2022
[Fix #10969] Allow `Lint/AmbiguousBlockAssociation` `AllowedPa…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants