Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
184 additions
and
2 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
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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for blocks without a body. | ||
# Such empty blocks are typically an oversight or we should provide a comment | ||
# be clearer what we're aiming for. | ||
# | ||
# @example | ||
# # bad | ||
# items.each { |item| } | ||
# | ||
# # good | ||
# items.each { |item| puts item } | ||
# | ||
# @example AllowComments: true (default) | ||
# # good | ||
# items.each do |item| | ||
# # TODO: implement later (inner comment) | ||
# end | ||
# | ||
# items.each { |item| } # TODO: implement later (inline comment) | ||
# | ||
# @example AllowComments: false | ||
# # bad | ||
# items.each do |item| | ||
# # TODO: implement later (inner comment) | ||
# end | ||
# | ||
# items.each { |item| } # TODO: implement later (inline comment) | ||
# | ||
class EmptyBlock < Base | ||
MSG = 'Empty block detected.' | ||
|
||
def on_block(node) | ||
return if node.body | ||
return if cop_config['AllowComments'] && | ||
processed_source.contains_comment?(node.source_range) | ||
|
||
add_offense(node) | ||
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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::EmptyBlock, :config do | ||
let(:cop_config) do | ||
{ 'AllowComments' => true } | ||
end | ||
|
||
it 'registers an offense for empty block within method call' do | ||
expect_offense(<<~RUBY) | ||
items.each { |item| } | ||
^^^^^^^^^^^^^^^^^^^^^ Empty block detected. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for empty block within lambda' do | ||
expect_offense(<<~RUBY) | ||
lambda { |item| } | ||
^^^^^^^^^^^^^^^^^ Empty block detected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for empty block with inner comments' do | ||
expect_no_offenses(<<~RUBY) | ||
items.each do |item| | ||
# TODO: implement later | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for empty block with inline comments' do | ||
expect_no_offenses(<<~RUBY) | ||
items.each { |item| } # TODO: implement later | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when block is not empty' do | ||
expect_no_offenses(<<~RUBY) | ||
items.each { |item| puts item } | ||
RUBY | ||
end | ||
|
||
context 'when AllowComments is false' do | ||
let(:cop_config) do | ||
{ 'AllowComments' => false } | ||
end | ||
|
||
it 'registers an offense for empty block with inner comments' do | ||
expect_offense(<<~RUBY) | ||
items.each do |item| | ||
^^^^^^^^^^^^^^^^^^^^ Empty block detected. | ||
# TODO: implement later | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for empty block with inline comments' do | ||
expect_offense(<<~RUBY) | ||
items.each { |item| } # TODO: implement later | ||
^^^^^^^^^^^^^^^^^^^^^ Empty block detected. | ||
RUBY | ||
end | ||
end | ||
end |