-
-
Notifications
You must be signed in to change notification settings - Fork 266
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 #133 from gyfis/th/safe-nagivation-with-blank
Add Rails/SafeNavigationWithBlank cop
- Loading branch information
Showing
7 changed files
with
132 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks to make sure safe navigation isn't used with `blank?` in | ||
# a conditional. | ||
# | ||
# While the safe navigation operator is generally a good idea, when | ||
# checking `foo&.blank?` in a conditional, `foo` being `nil` will actually | ||
# do the opposite of what the author intends. | ||
# | ||
# @example | ||
# # bad | ||
# do_something if foo&.blank? | ||
# do_something unless foo&.blank? | ||
# | ||
# # good | ||
# do_something if foo.blank? | ||
# do_something unless foo.blank? | ||
# | ||
class SafeNavigationWithBlank < Cop | ||
MSG = | ||
'Avoid calling `blank?` with the safe navigation operator ' \ | ||
'in conditionals.' | ||
|
||
def_node_matcher :safe_navigation_blank_in_conditional?, <<-PATTERN | ||
(if $(csend ... :blank?) ...) | ||
PATTERN | ||
|
||
def on_if(node) | ||
return unless safe_navigation_blank_in_conditional?(node) | ||
|
||
add_offense(node) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
corrector.replace( | ||
safe_navigation_blank_in_conditional?(node).location.dot, | ||
'.' | ||
) | ||
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
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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::SafeNavigationWithBlank do | ||
subject(:cop) { described_class.new } | ||
|
||
context 'in a conditional' do | ||
it 'registers an offense on a single conditional' do | ||
expect_offense(<<~RUBY) | ||
do_something unless foo&.blank? | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid calling `blank?` with the safe navigation operator in conditionals. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
do_something unless foo.blank? | ||
RUBY | ||
end | ||
|
||
it 'registers an offense on chained conditionals' do | ||
expect_offense(<<~RUBY) | ||
do_something unless foo&.bar&.blank? | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid calling `blank?` with the safe navigation operator in conditionals. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
do_something unless foo&.bar.blank? | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense on `.blank?`' do | ||
expect_no_offenses(<<~RUBY) | ||
return if foo.blank? | ||
RUBY | ||
end | ||
end | ||
|
||
context 'outside a conditional' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
bar = foo&.blank? | ||
RUBY | ||
end | ||
end | ||
end |