Skip to content

Commit

Permalink
Merge pull request #1239 from rubocop/add-be-nil-cop
Browse files Browse the repository at this point in the history
Add new `RSpec/BeNil` cop
  • Loading branch information
pirj authored Feb 24, 2022
2 parents 766bc1b + d636fc7 commit 0a1a7b9
Show file tree
Hide file tree
Showing 7 changed files with 112 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)

* Add new `RSpec/BeNil` cop. ([@bquorning][])

## 2.8.0 (2022-01-24)

* Fix `RSpec/FactoryBot/SyntaxMethods` and `RSpec/Capybara/FeatureMethods` to inspect shared groups. ([@pirj][])
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ RSpec/BeEql:
VersionAdded: '1.7'
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeEql

RSpec/BeNil:
Description: Check that `be_nil` is used instead of `be(nil)`.
Enabled: pending
VersionAdded: 2.9.0
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeNil

RSpec/BeforeAfterAll:
Description: Check that before/after(:all) isn't being used.
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* xref:cops_rspec.adoc#rspecaroundblock[RSpec/AroundBlock]
* xref:cops_rspec.adoc#rspecbe[RSpec/Be]
* xref:cops_rspec.adoc#rspecbeeql[RSpec/BeEql]
* xref:cops_rspec.adoc#rspecbenil[RSpec/BeNil]
* xref:cops_rspec.adoc#rspecbeforeafterall[RSpec/BeforeAfterAll]
* xref:cops_rspec.adoc#rspeccontextmethod[RSpec/ContextMethod]
* xref:cops_rspec.adoc#rspeccontextwording[RSpec/ContextWording]
Expand Down
32 changes: 32 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,38 @@ expect(foo).to be(nil)

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeEql

== RSpec/BeNil

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Yes
| 2.9.0
| -
|===

Check that `be_nil` is used instead of `be(nil)`.

RSpec has a built-in `be_nil` matcher specifically for expecting `nil`.
For consistent specs, we recommend using that instead of `be(nil).

=== Examples

[source,ruby]
----
# bad
expect(foo).to be(nil)
# good
expect(foo).to be_nil
----

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeNil

== RSpec/BeforeAfterAll

|===
Expand Down
40 changes: 40 additions & 0 deletions lib/rubocop/cop/rspec/be_nil.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
# Check that `be_nil` is used instead of `be(nil)`.
#
# RSpec has a built-in `be_nil` matcher specifically for expecting `nil`.
# For consistent specs, we recommend using that instead of `be(nil).
#
# @example
#
# # bad
# expect(foo).to be(nil)
#
# # good
# expect(foo).to be_nil
#
class BeNil < Base
extend AutoCorrector

MSG = 'Prefer `be_nil` over `be(nil)`.'
RESTRICT_ON_SEND = %i[be].freeze

# @!method nil_value_expectation?(node)
def_node_matcher :nil_value_expectation?, <<-PATTERN
(send nil? :be nil)
PATTERN

def on_send(node)
return unless nil_value_expectation?(node)

add_offense(node) do |corrector|
corrector.replace(node.loc.expression, 'be_nil')
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require_relative 'rspec/around_block'
require_relative 'rspec/be'
require_relative 'rspec/be_eql'
require_relative 'rspec/be_nil'
require_relative 'rspec/before_after_all'
require_relative 'rspec/context_method'
require_relative 'rspec/context_wording'
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cop/rspec/be_nil_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::BeNil do
it 'registers an offense when using `#be` for `nil` value' do
expect_offense(<<~RUBY)
expect(foo).to be(nil)
^^^^^^^ Prefer `be_nil` over `be(nil)`.
RUBY

expect_correction(<<~RUBY)
expect(foo).to be_nil
RUBY
end

it 'does not register an offense when using `#be_nil`' do
expect_no_offenses(<<~RUBY)
expect(foo).to be_nil
RUBY
end

it 'does not register an offense when using `#be` with other values' do
expect_no_offenses(<<~RUBY)
expect(foo).to be(true)
expect(foo).to be(false)
expect(foo).to be(1)
expect(foo).to be("yes")
expect(foo).to be(Class.new)
RUBY
end
end

0 comments on commit 0a1a7b9

Please sign in to comment.