Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Lint/DuplicateElsifCondition
cop
- Loading branch information
Showing
7 changed files
with
134 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
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks that there are no repeated conditions used in if 'elsif'. | ||
# | ||
# @example | ||
# # bad | ||
# if x == 1 | ||
# do_something | ||
# elsif x == 1 | ||
# do_something_else | ||
# end | ||
# | ||
# # good | ||
# if x == 1 | ||
# do_something | ||
# elsif x == 2 | ||
# do_something_else | ||
# end | ||
# | ||
class DuplicateElsifCondition < Base | ||
MSG = 'Duplicate `elsif` condition detected.' | ||
|
||
def on_if(node) | ||
previous = [] | ||
while node.if? || node.elsif? | ||
condition = node.condition | ||
add_offense(condition) if previous.include?(condition) | ||
previous << condition | ||
node = node.else_branch | ||
break unless node&.if_type? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::DuplicateElsifCondition do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense for repeated elsif conditions' do | ||
expect_offense(<<~RUBY) | ||
if x == 1 | ||
elsif x == 2 | ||
elsif x == 1 | ||
^^^^^^ Duplicate `elsif` condition detected. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for subsequent repeated elsif conditions' do | ||
expect_offense(<<~RUBY) | ||
if x == 1 | ||
elsif x == 2 | ||
elsif x == 2 | ||
^^^^^^ Duplicate `elsif` condition detected. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers multiple offenses for multiple repeated elsif conditions' do | ||
expect_offense(<<~RUBY) | ||
if x == 1 | ||
elsif x == 2 | ||
elsif x == 1 | ||
^^^^^^ Duplicate `elsif` condition detected. | ||
elsif x == 2 | ||
^^^^^^ Duplicate `elsif` condition detected. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for non-repeated elsif conditions' do | ||
expect_no_offenses(<<~RUBY) | ||
if x == 1 | ||
elsif x == 2 | ||
else | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for partially repeated elsif conditions' do | ||
expect_no_offenses(<<~RUBY) | ||
if x == 1 | ||
elsif x == 1 && x == 2 | ||
end | ||
RUBY | ||
end | ||
end |