Skip to content

Improve PredicateMatcher recommendations #2068

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix false positive in `RSpec/Pending`, where it would mark the default block `it` as an offense. ([@bquorning])
- Fix issue when `Style/ContextWording` is configured with a Prefix being interpreted as a boolean, like `on`. ([@sakuro])
- Add new `RSpec/IncludeExamples` cop to enforce using `it_behaves_like` over `include_examples`. ([@dvandersluis])
- Fix `RSpec/PredicateMatcher` sometimes suggesting the `be_key` matcher instead of `have_key`. ([@bquorning])

## 3.5.0 (2025-02-16)

Expand Down
55 changes: 33 additions & 22 deletions lib/rubocop/cop/rspec/predicate_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,22 @@ def message_inflected(predicate)
matcher_name: to_predicate_matcher(predicate.method_name))
end

TO_PREDICATE_MATCHER_MAP = {
exist?: 'exist',
exists?: 'exist',
include?: 'include',
instance_of?: 'be_an_instance_of',
is_a?: 'be_a',
key?: 'have_key',
respond_to?: 'respond_to'
}.freeze
private_constant :TO_PREDICATE_MATCHER_MAP

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/...

when 'is_a?'
'be_a'
when 'instance_of?'
'be_an_instance_of'
when 'include?', 'respond_to?'
name[0..-2]
when 'exist?', 'exists?'
'exist'
when /\Ahas_/
name.sub('has_', 'have_')[0..-2]
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?

"be_#{name[0..-2]}"
end
Expand Down Expand Up @@ -241,18 +245,25 @@ def move_predicate(corrector, actual, matcher, block_child)
corrector.insert_after(actual, ".#{predicate}" + args + block)
end

TO_PREDICATE_METHOD_MAP = {
a_kind_of: 'is_a?',
an_instance_of: 'instance_of?',
be_a: 'is_a?',
be_a_kind_of: 'is_a?',
be_an: 'is_a?',
be_an_instance_of: 'instance_of?',
be_instance_of: 'instance_of?',
be_kind_of: 'is_a?',
include: 'include?',
respond_to: 'respond_to?'
}.freeze
private_constant :TO_PREDICATE_METHOD_MAP

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.

'is_a?'
when 'be_an_instance_of', 'be_instance_of', 'an_instance_of'
'instance_of?'
when 'include'
'include?'
when 'respond_to'
'respond_to?'
when /\Ahave_(.+)/
"has_#{Regexp.last_match(1)}?"
if TO_PREDICATE_METHOD_MAP.key?(matcher)
TO_PREDICATE_METHOD_MAP.fetch(matcher)
elsif (subject = matcher[/\Ahave_(.+)/, 1])
"has_#{subject}?"
else
"#{matcher[/\Abe_(.+)/, 1]}?"
end
Expand Down
3 changes: 3 additions & 0 deletions spec/rubocop/cop/rspec/predicate_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.

expect(foo.is_a?(Array)).to be_truthy
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_a` matcher over `is_a?`.
expect(foo.instance_of?(Array)).to be_truthy
Expand All @@ -103,6 +105,7 @@
expect(foo).to be_something('foo', 'bar')
expect(foo).to be_something 1, 2
expect(foo).to have_key('foo')
expect(foo).to have_key('foo')
expect(foo).to be_a(Array)
expect(foo).to be_an_instance_of(Array)
RUBY
Expand Down