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/EmptyConditionalBody
cop
- Loading branch information
Showing
7 changed files
with
254 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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for the presence of `if`, `elsif` and `unless` branches without a body. | ||
# @example | ||
# # bad | ||
# if condition | ||
# end | ||
# | ||
# # bad | ||
# unless condition | ||
# end | ||
# | ||
# # bad | ||
# if condition | ||
# do_something | ||
# elsif other_condition | ||
# end | ||
# | ||
# # good | ||
# if condition | ||
# do_something | ||
# end | ||
# | ||
# # good | ||
# unless condition | ||
# do_something | ||
# end | ||
# | ||
# # good | ||
# if condition | ||
# do_something | ||
# elsif other_condition | ||
# do_something_else | ||
# end | ||
# | ||
# @example AllowComments: true (default) | ||
# # good | ||
# if condition | ||
# do_something | ||
# elsif other_condition | ||
# # noop | ||
# end | ||
# | ||
# @example AllowComments: false | ||
# # bad | ||
# if condition | ||
# do_something | ||
# elsif other_condition | ||
# # noop | ||
# end | ||
# | ||
class EmptyConditionalBody < Base | ||
MSG = 'Avoid `%<keyword>s` branches without a body.' | ||
|
||
def on_if(node) | ||
return if node.body | ||
return if cop_config['AllowComments'] && comment_lines?(node) | ||
|
||
add_offense(node, message: format(MSG, keyword: node.keyword)) | ||
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,94 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::EmptyConditionalBody, :config do | ||
let(:cop_config) do | ||
{ 'AllowComments' => true } | ||
end | ||
|
||
it 'registers an offense for missing `if` body' do | ||
expect_offense(<<~RUBY) | ||
if condition | ||
^^^^^^^^^^^^ Avoid `if` branches without a body. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for missing `if` body with a comment' do | ||
expect_no_offenses(<<~RUBY) | ||
if condition | ||
# noop | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for missing `elsif` body' do | ||
expect_offense(<<~RUBY) | ||
if condition | ||
do_something | ||
elsif other_condition | ||
^^^^^^^^^^^^^^^^^^^^^ Avoid `elsif` branches without a body. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for missing `elsif` body with a comment' do | ||
expect_no_offenses(<<~RUBY) | ||
if condition | ||
do_something | ||
elsif other_condition | ||
# noop | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for missing `unless` body' do | ||
expect_offense(<<~RUBY) | ||
unless condition | ||
^^^^^^^^^^^^^^^^ Avoid `unless` branches without a body. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for missing `unless` body with a comment' do | ||
expect_no_offenses(<<~RUBY) | ||
unless condition | ||
# noop | ||
end | ||
RUBY | ||
end | ||
|
||
context 'when AllowComments is false' do | ||
let(:cop_config) do | ||
{ 'AllowComments' => false } | ||
end | ||
|
||
it 'registers an offense for missing `if` body with a comment' do | ||
expect_offense(<<~RUBY) | ||
if condition | ||
^^^^^^^^^^^^ Avoid `if` branches without a body. | ||
# noop | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for missing `elsif` body with a comment' do | ||
expect_offense(<<~RUBY) | ||
if condition | ||
do_something | ||
elsif other_condition | ||
^^^^^^^^^^^^^^^^^^^^^ Avoid `elsif` branches without a body. | ||
# noop | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for missing `unless` body with a comment' do | ||
expect_offense(<<~RUBY) | ||
unless condition | ||
^^^^^^^^^^^^^^^^ Avoid `unless` branches without a body. | ||
# noop | ||
end | ||
RUBY | ||
end | ||
end | ||
end |