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

Improve PredicateMatcher recommendations #2068

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bquorning
Copy link
Collaborator

PredicateMatcher should not recommend changing #key? to be_key, but will now instead suggest using have_key.

Fixes #2067


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

This prepares for adding more mathcer options.
PredicateMatcher should not recommend changing `#key?` to `be_key`, but
will now instead suggest using `have_key`.
@bquorning bquorning marked this pull request as ready for review March 29, 2025 10:41
@bquorning bquorning requested a review from a team as a code owner March 29, 2025 10:41
def to_predicate_method(matcher)
case matcher = matcher.to_s
when 'be_a', 'be_an', 'be_a_kind_of', 'a_kind_of', 'be_kind_of'
Copy link
Member

Choose a reason for hiding this comment

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

Subjectively, this reads better than multiple hash items

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I changed it to a hash lookup because one of the Metric cops said the method became too long. But I agree with you, and I will change it back when/if I get back to this PR.

@@ -91,6 +91,8 @@
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_something` matcher over `something?`.
expect(foo.has_key?('foo')).to be_truthy
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `have_key` matcher over `has_key?`.
expect(foo.key?('foo')).to be_truthy
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `have_key` matcher over `key?`.
Copy link
Member

Choose a reason for hiding this comment

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

Does this imply that the receiver is an instance of a Hash?
If it’s e.g. an SqlColumn, have_key? won’t fit, and it should be is_key? instead

Copy link
Member

Choose a reason for hiding this comment

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

Can we make a distinction at least if there’s an argument?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Proposal:
Only auto-correct expect(foo.key?(foo)) with a single argument.
Only raise an offence with zero or multiple args or a block

Copy link
Collaborator Author

@bquorning bquorning Mar 29, 2025

Choose a reason for hiding this comment

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

To be honest, I don't really like this cop (or others) that make broad assumptions about types which can not safely be deduced with static analysis. There will always be false positives.

Copy link
Collaborator Author

@bquorning bquorning Mar 29, 2025

Choose a reason for hiding this comment

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

TIL key? is aliased as has_key?

And I thought one of them had been deprecated, but apparently that's not the case.

I also think this method is aliased as member? and include? btw.

def to_predicate_matcher(name)
case name = name.to_s
Copy link
Member

Choose a reason for hiding this comment

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

Side note, unrelated to this PR. This list is incomplete, since RSpec has support for eg start_with/end_with. They work with Arrays, too. Also cover/exist/...

if TO_PREDICATE_MATCHER_MAP.key?(name)
TO_PREDICATE_MATCHER_MAP.fetch(name)
elsif name.start_with?('has_')
name.to_s.sub('has_', 'have_')[0..-2]
else
Copy link
Member

Choose a reason for hiding this comment

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

How do you feel if we raise an offence, but don't autocorrect the "else" cases?

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.

PredicateMatcher recommends changing #key? to matcher be_key
2 participants