-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1239 from rubocop/add-be-nil-cop
Add new `RSpec/BeNil` cop
- Loading branch information
Showing
7 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |