Skip to content

Commit

Permalink
Merge pull request #2036 from rubocop/fix-2010
Browse files Browse the repository at this point in the history
Fix a bug in RSpec/PredicateMatcher
  • Loading branch information
bquorning authored Feb 9, 2025
2 parents 226f332 + ae4aadd commit 27e37d5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Don't let `RSpec/PredicateMatcher` replace `respond_to?` with two arguments with the RSpec `respond_to` matcher. ([@bquorning])

## 3.4.0 (2025-01-20)

- Fix `RSpec/SortMetadata` cop to limit sorting to trailing metadata arguments. ([@cbliard])
Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/cop/rspec/predicate_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module InflectedHelper

def check_inflected(node)
predicate_in_actual?(node) do |predicate, to, matcher|
next if cannot_replace_predicate?(predicate)

msg = message_inflected(predicate)
add_offense(node, message: msg) do |corrector|
remove_predicate(corrector, predicate)
Expand All @@ -35,6 +37,10 @@ def check_inflected(node)
$#boolean_matcher? ...)
PATTERN

def cannot_replace_predicate?(send_node)
send_node.method?(:respond_to?) && send_node.arguments.length > 1
end

# @!method be_bool?(node)
def_node_matcher :be_bool?, <<~PATTERN
(send nil? {:be :eq :eql :equal} {true false})
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rspec/predicate_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@
RUBY
end

it 'accepts respond_to? with a second argument' do
expect_no_offenses(<<~RUBY)
expect(foo.respond_to?(:bar, true)).to be_truthy
RUBY
end

it 'registers an offense for a predicate method with argument' do
expect_offense(<<~RUBY)
expect(foo.something?('foo')).to be_truthy
Expand Down

0 comments on commit 27e37d5

Please sign in to comment.