Skip to content

Commit

Permalink
Support AllowComments option for Style/EmptyElse
Browse files Browse the repository at this point in the history
This PR is support `AllowComments` option for `Style/EmptyElse`.
The default for this option is false.

In the case of complex conditional statements,
we may want to leave `else` empty or nil and
write only the explanation of the `else` case in the comments.

`AllowComments: true` will not treat when `else` as offense
if the `else` with an intentional comment.

### @example AllowComments: false (default)

```ruby
# bad
if condition
  statement
else
  # something comment
  nil
end

# bad
if condition
  statement
else
  # something comment
end
```

### @example AllowComments: true

```ruby
# good
if condition
  statement
else
  # something comment
  nil
end

# good
if condition
  statement
else
  # something comment
end
```
  • Loading branch information
ydah authored and bbatsov committed Jul 11, 2022
1 parent d8b4235 commit 4c4b91e
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 0 deletions.
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

0 comments on commit 4c4b91e

Please sign in to comment.