Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
EnforcedStyle
for Style/DoubleNegation
cop
This PR adds `EnforcedStyle` option for `Style/DoubleNegation` cop, and `EnforcedStyle: allowed_in_boolean_context` by default based on #7833 survey. ```ruby # @example EnforcedStyle: allowed_in_boolean_context (default) # # good # def foo? # !!return_value # end # # @example EnforcedStyle: always # # bad # def foo? # !!return_value # end ``` Ruby will return the last evaluated value. This PR only supports simple contexts, such as the end of line value of method, and when using `return`. A complex contexts should also be supported, such as when branching with `if` and others in the future.
- Loading branch information
Showing
5 changed files
with
157 additions
and
20 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 |
---|---|---|
@@ -1,20 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::DoubleNegation do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense for !!' do | ||
expect_offense(<<~RUBY) | ||
!!test.something | ||
^ Avoid the use of double negation (`!!`). | ||
RUBY | ||
RSpec.describe RuboCop::Cop::Style::DoubleNegation, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:cop_config) do | ||
{ 'EnforcedStyle' => enforced_style } | ||
end | ||
|
||
shared_examples 'common' do | ||
it 'does not register an offense for `!!` when not a return location' do | ||
expect_offense(<<~RUBY) | ||
def foo? | ||
foo | ||
!!test.something | ||
^ Avoid the use of double negation (`!!`). | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for `!!`' do | ||
expect_offense(<<~RUBY) | ||
!!test.something | ||
^ Avoid the use of double negation (`!!`). | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for !' do | ||
expect_no_offenses('!test.something') | ||
end | ||
|
||
it 'does not register an offense for `not not`' do | ||
expect_no_offenses('not not test.something') | ||
end | ||
end | ||
|
||
it 'does not register an offense for !' do | ||
expect_no_offenses('!test.something') | ||
context 'when `EnforcedStyle: allowed_in_returns`' do | ||
let(:enforced_style) { 'allowed_in_returns' } | ||
|
||
include_examples 'common' | ||
|
||
it 'does not register an offense for `!!` when return location' do | ||
expect_no_offenses(<<~RUBY) | ||
def foo? | ||
bar | ||
!!baz.do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for `!!` when using `return` keyword' do | ||
expect_no_offenses(<<~RUBY) | ||
def foo? | ||
return !!bar.do_something if condition | ||
baz | ||
!!qux | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense for not not' do | ||
expect_no_offenses('not not test.something') | ||
context 'when `EnforcedStyle: forbidden`' do | ||
let(:enforced_style) { 'forbidden' } | ||
|
||
include_examples 'common' | ||
|
||
it 'registers an offense for `!!` when return location' do | ||
expect_offense(<<~RUBY) | ||
def foo? | ||
bar | ||
!!baz.do_something | ||
^ Avoid the use of double negation (`!!`). | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for `!!` when using `return` keyword' do | ||
expect_offense(<<~RUBY) | ||
def foo? | ||
return !!bar.do_something if condition | ||
^ Avoid the use of double negation (`!!`). | ||
baz | ||
!!bar | ||
^ Avoid the use of double negation (`!!`). | ||
end | ||
RUBY | ||
end | ||
end | ||
end |