-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
7 changed files
with
295 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#9001](https://github.com/rubocop-hq/rubocop/pull/9001): Add new `Lint/EmptyClass` cop. ([@fatkodima][]) |
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,93 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for classes and metaclasses without a body. | ||
# Such empty classes and metaclasses are typically an oversight or we should provide a comment | ||
# to be clearer what we're aiming for. | ||
# | ||
# @example | ||
# # bad | ||
# class Foo | ||
# end | ||
# | ||
# class Bar | ||
# class << self | ||
# end | ||
# end | ||
# | ||
# class << obj | ||
# end | ||
# | ||
# # good | ||
# class Foo | ||
# def do_something | ||
# # ... code | ||
# end | ||
# end | ||
# | ||
# class Bar | ||
# class << self | ||
# attr_reader :bar | ||
# end | ||
# end | ||
# | ||
# class << obj | ||
# attr_reader :bar | ||
# end | ||
# | ||
# @example AllowComments: false (default) | ||
# # bad | ||
# class Foo | ||
# # TODO: implement later | ||
# end | ||
# | ||
# class Bar | ||
# class << self | ||
# # TODO: implement later | ||
# end | ||
# end | ||
# | ||
# class << obj | ||
# # TODO: implement later | ||
# end | ||
# | ||
# @example AllowComments: true | ||
# # good | ||
# class Foo | ||
# # TODO: implement later | ||
# end | ||
# | ||
# class Bar | ||
# class << self | ||
# # TODO: implement later | ||
# end | ||
# end | ||
# | ||
# class << obj | ||
# # TODO: implement later | ||
# end | ||
# | ||
class EmptyClass < Base | ||
CLASS_MSG = 'Empty class detected.' | ||
METACLASS_MSG = 'Empty metaclass detected.' | ||
|
||
def on_class(node) | ||
add_offense(node, message: CLASS_MSG) unless body_or_allowed_comment_lines?(node) || | ||
node.parent_class | ||
end | ||
|
||
def on_sclass(node) | ||
add_offense(node, message: METACLASS_MSG) unless body_or_allowed_comment_lines?(node) | ||
end | ||
|
||
private | ||
|
||
def body_or_allowed_comment_lines?(node) | ||
node.body || (cop_config['AllowComments'] && comment_lines?(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,93 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::EmptyClass, :config do | ||
let(:cop_config) do | ||
{ 'AllowComments' => false } | ||
end | ||
|
||
it 'registers an offense for empty class' do | ||
expect_offense(<<~RUBY) | ||
class Foo | ||
^^^^^^^^^ Empty class detected. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for empty class metaclass' do | ||
expect_offense(<<~RUBY) | ||
class Foo | ||
class << self | ||
^^^^^^^^^^^^^ Empty metaclass detected. | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for empty object metaclass' do | ||
expect_offense(<<~RUBY) | ||
class << obj | ||
^^^^^^^^^^^^ Empty metaclass detected. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when empty metaclass contains only comments' do | ||
expect_offense(<<~RUBY) | ||
class Foo | ||
class << self | ||
^^^^^^^^^^^^^ Empty metaclass detected. | ||
# Comment. | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when class is not empty' do | ||
expect_no_offenses(<<~RUBY) | ||
class Foo | ||
attr_reader :bar | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when empty has a parent' do | ||
expect_no_offenses(<<~RUBY) | ||
class Child < Parent | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when metaclass is not empty' do | ||
expect_no_offenses(<<~RUBY) | ||
class Foo | ||
class << self | ||
attr_reader :bar | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
context 'when AllowComments is true' do | ||
let(:cop_config) do | ||
{ 'AllowComments' => true } | ||
end | ||
|
||
it 'does not register an offense when empty class contains only comments' do | ||
expect_no_offenses(<<~RUBY) | ||
class Foo | ||
# Comment. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when empty metaclass contains only comments' do | ||
expect_no_offenses(<<~RUBY) | ||
class Foo | ||
class << self | ||
# Comment. | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
end |