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 #4189] Lint/AmbiguousBlockAssociation cites unambiguous lambda p… #4237

Conversation

smakagon
Copy link
Contributor

@smakagon smakagon commented Apr 3, 2017

Fixes problem reported in #4189.
With this fix the following code is ok for AmbiguousBlockAssociation cop:

scope :technical, -> { where(technical: true) }

Now AmbiguousBlockAssociation cop treats lambda properly.

@ivanovaleksey
Copy link
Contributor

ivanovaleksey commented Apr 3, 2017

One more thing...

For the following code
expect(described_class.new.by_coordinates).to eq {}
I receive

Parenthesize the param eq {} to make sure that the block will be associated with the eq method call.

Is it expected behavior?

@smakagon
Copy link
Contributor Author

smakagon commented Apr 3, 2017

@ivanovaleksey yes, i think that's expected behavior because we call method .to and pass two params eq and {}. Parser treats {} as a block.

@smakagon
Copy link
Contributor Author

smakagon commented Apr 3, 2017

@ivanovaleksey It's expected behavior that cop adds offense, but I'm not sure about error message though.

@ivanovaleksey
Copy link
Contributor

ivanovaleksey commented Apr 3, 2017

Yes, that is the case.

Parenthesize the param eq {} ...

Does it mean to write expect(described_class.new.by_coordinates).to (eq {})?

@smakagon
Copy link
Contributor Author

smakagon commented Apr 3, 2017

@ivanovaleksey I've just checked your example and it returns the following message:

Parenthesize the param eq {} to make sure that the block will be associated with the eq method call.

Which is good. It means that your should add parentheses for (eq {}) to make sure that block {} will be associated with eq method, not with to.

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 4, 2017

This needs a changelog entry.

@ivanovaleksey
Copy link
Contributor

@smakagon actually it should be
expect(described_class.new.by_coordinates).to eq({})

This way it doesn't produce offense.
But the question in the same: is it okay that one receives a message like this?

@Drenmi
Copy link
Collaborator

Drenmi commented Apr 4, 2017

@ivanovaleksey: This cop is meaningless to run on RSpec files, because the DSL deliberately uses the constructs this cop is checking for. 🙂

@ivanovaleksey
Copy link
Contributor

ivanovaleksey commented Apr 4, 2017

@Drenmi thanks, I see.
I just wanted to clarify that everything is fine with this fix.

@smakagon smakagon force-pushed the 4189_ambiguous_block_association_cites_unambiguous_lambda_param branch from 6729cac to 1a8ce30 Compare April 4, 2017 12:18
@smakagon
Copy link
Contributor Author

smakagon commented Apr 4, 2017

@bbatsov updated changelog. Thanks.

@shayani
Copy link

shayani commented May 5, 2017

I updated my gem to 0.48.1 but I'm still having the same warning for my scope:
scope :by_local_assembly_id, ->(id) { where(local_assembly_id: id) }

@giangneolab
Copy link

@shayani me too

@janraasch
Copy link
Contributor

The problem is present in v0.48.1 as can be seen from this comment. So I suppose the fix will be part of the next rubocop release.

@jonas054
Copy link
Collaborator

jonas054 commented Aug 2, 2017

I want to add something for clarification.

@ivanovaleksey wrote:

Does it mean to write expect(described_class.new.by_coordinates).to (eq {})?

Yes, that what RuboCop wants you to write. It assumes that passing a block and not an empty hash is what you wanted. The fact that eq takes one parameter and no block is something that RuboCop doesn't know. A check for this could be implemented in rubocop-rspec, but OTOH rspec will tell you ArgumentError: wrong number of arguments (0 for 1) when you try to run the code.

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 this pull request may close these issues.

8 participants