forked from rubocop/rubocop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix rubocop#8469] Add cop Layout/SpaceAroundSingletonClassOperator
I chose the name SpaceAroundSingletonClassOperator rather than SpaceAroundSingletonInheritence, which was suggested in the feature request. Although I'm not sure that << is actually an operator, I am pretty sure that inheritance is not involved. The operator (for lack of a better term) opens up the singleton class, or eigenclass, so methods can be added to it.
- Loading branch information
Showing
5 changed files
with
129 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 @@ | ||
* [#8469](https://github.com/rubocop/rubocop/issues/8469): Add cop `Layout/SpaceAroundSingletonClassOperator`. ([@jonas054][]) |
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
53 changes: 53 additions & 0 deletions
53
lib/rubocop/cop/layout/space_around_singleton_class_operator.rb
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,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Layout | ||
# Checks if spaces are used around the singleton class operator <<. | ||
# | ||
# @example EnforcedStyle: space (default) | ||
# # bad | ||
# class SomeClass | ||
# class<<self | ||
# end | ||
# end | ||
# | ||
# # good | ||
# class SomeClass | ||
# class << self | ||
# end | ||
# end | ||
# | ||
# @example EnforcedStyle: no_space | ||
# # bad | ||
# class SomeClass | ||
# class << self | ||
# end | ||
# end | ||
# | ||
# # good | ||
# class SomeClass | ||
# class<<self | ||
# end | ||
# end | ||
# | ||
class SpaceAroundSingletonClassOperator < Base | ||
include RangeHelp | ||
include ConfigurableEnforcedStyle | ||
|
||
MSG_SPACE = 'Use a single space around the singleton class operator.' | ||
MSG_NO_SPACE = 'Use no space around the singleton class operator.' | ||
|
||
def on_sclass(node) | ||
range = range_with_surrounding_space(range: node.loc.operator) | ||
case style | ||
when :space | ||
add_offense(range, message: MSG_SPACE) if range.source != ' << ' | ||
else | ||
add_offense(range, message: MSG_NO_SPACE) if range.source != '<<' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
65 changes: 65 additions & 0 deletions
65
spec/rubocop/cop/layout/space_around_singleton_class_operator_spec.rb
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,65 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Layout::SpaceAroundSingletonClassOperator, :config do | ||
context 'when EnforcedStyle is space' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'space' } } | ||
|
||
it 'registers an offense for no space to the left`' do | ||
expect_offense(<<~RUBY) | ||
class<< self | ||
^^^ Use a single space around the singleton class operator. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for no space to the right`' do | ||
expect_offense(<<~RUBY) | ||
class <<Other | ||
^^^ Use a single space around the singleton class operator. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for no space at all`' do | ||
expect_offense(<<~RUBY) | ||
class<<self | ||
^^ Use a single space around the singleton class operator. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for << surrounded by single spaces' do | ||
expect_no_offenses(<<~RUBY) | ||
class << self | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when EnforcedStyle is no_space' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'no_space' } } | ||
|
||
it 'registers an offense for space to the left`' do | ||
expect_offense(<<~RUBY) | ||
class <<self | ||
^^^ Use no space around the singleton class operator. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for space to the right`' do | ||
expect_offense(<<~RUBY) | ||
class<< self | ||
^^^ Use no space around the singleton class operator. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for << surrounded by no space' do | ||
expect_no_offenses(<<~RUBY) | ||
class<<self | ||
end | ||
RUBY | ||
end | ||
end | ||
end |