Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AllowComments option for Style/EmptyElse #10790

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_support_allow_comments_for_empty_else.md
@@ -0,0 +1 @@
* [#10790](https://github.com/rubocop/rubocop/pull/10790): Support `AllowComments` option for `Style/EmptyElse`. ([@ydah][])
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -3512,6 +3512,7 @@ Style/EmptyElse:
- empty
- nil
- both
AllowComments: false

Style/EmptyLambdaParameter:
Description: 'Omit parens for empty lambda parameters.'
Expand Down
37 changes: 37 additions & 0 deletions lib/rubocop/cop/style/empty_else.rb
Expand Up @@ -89,6 +89,41 @@ module Style
# if condition
# statement
# end
#
# @example AllowComments: false (default)
#
# # bad
# if condition
# statement
# else
# # something comment
# nil
# end
#
# # bad
# if condition
# statement
# else
# # something comment
# end
#
# @example AllowComments: true
#
# # good
# if condition
# statement
# else
# # something comment
# nil
# end
#
# # good
# if condition
# statement
# else
# # something comment
# end
#
class EmptyElse < Base
include OnNormalIfUnless
include ConfigurableEnforcedStyle
Expand All @@ -108,6 +143,8 @@ def on_case(node)
private

def check(node)
return if cop_config['AllowComments'] && comment_in_else?(node.loc)

empty_check(node) if empty_style?
nil_check(node) if nil_style?
end
Expand Down
168 changes: 168 additions & 0 deletions spec/rubocop/cop/style/empty_else_spec.rb
Expand Up @@ -530,6 +530,174 @@
end
end

context 'when `AllowComments: true`' do
let(:config) do
RuboCop::Config.new('Style/EmptyElse' => {
'AllowComments' => true,
'EnforcedStyle' => 'both',
'SupportedStyles' => %w[empty nil both]
},
'Style/MissingElse' => missing_else_config)
end

context 'given an if-statement' do
context 'with not comment and empty else-clause' do
it 'registers an offense' do
expect_offense(<<~RUBY)
if condition
statement
else
^^^^ Redundant `else`-clause.
end
RUBY
end
end

context 'with not comment and nil else-clause' do
it 'registers an offense' do
expect_offense(<<~RUBY)
if condition
statement
else
^^^^ Redundant `else`-clause.
nil
end
RUBY
end
end

context 'with comment and empty else-clause' do
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
if condition
statement
else
# some comment
end
RUBY
end
end

context 'with comment and nil else-clause' do
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
if condition
statement
else
nil # some comment
end
RUBY
end
end
end

context 'given an unless-statement' do
context 'with not comment and empty else-clause' do
it 'registers an offense' do
expect_offense(<<~RUBY)
unless condition
statement
else
^^^^ Redundant `else`-clause.
end
RUBY
end
end

context 'with not comment and nil else-clause' do
it 'registers an offense' do
expect_offense(<<~RUBY)
unless condition
statement
else
^^^^ Redundant `else`-clause.
nil
end
RUBY
end
end

context 'with comment and empty else-clause' do
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
unless condition
statement
else
# some comment
end
RUBY
end
end

context 'with comment and nil else-clause' do
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
unless condition
statement
else
nil # some comment
end
RUBY
end
end
end

context 'given a case statement' do
context 'with not comment and empty else-clause' do
it 'registers an offense' do
expect_offense(<<~RUBY)
case a
when condition
statement
else
^^^^ Redundant `else`-clause.
end
RUBY
end
end

context 'with not comment and nil else-clause' do
it 'registers an offense' do
expect_offense(<<~RUBY)
case a
when condition
statement
else
^^^^ Redundant `else`-clause.
nil
end
RUBY
end
end

context 'with comment and empty else-clause' do
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
case a
when condition
statement
else
# some comment
end
RUBY
end
end

context 'with comment and nil else-clause' do
it "doesn't register an offense" do
expect_no_offenses(<<~RUBY)
case a
when condition
statement
else
nil # some comment
end
RUBY
end
end
end
end

context 'with nested if and case statement' do
let(:config) do
RuboCop::Config.new('Style/EmptyElse' => {
Expand Down