Skip to content

Commit

Permalink
[Fix rubocop#8469] Add cop Layout/SpaceAroundSingletonClassOperator
Browse files Browse the repository at this point in the history
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
jonas054 committed Jul 3, 2021
1 parent 33ed53a commit 8883ae5
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_layout_spacearoundsingletonclassoperator.md
@@ -0,0 +1 @@
* [#8469](https://github.com/rubocop/rubocop/issues/8469): Add cop `Layout/SpaceAroundSingletonClassOperator`. ([@jonas054][])
9 changes: 9 additions & 0 deletions config/default.yml
Expand Up @@ -1226,6 +1226,15 @@ Layout/SpaceAroundOperators:
- space
- no_space

Layout/SpaceAroundSingletonClassOperator:
Description: 'Use a single space around the singleton class operator.'
Enabled: pending
VersionAdded: '<<next>>'
EnforcedStyle: space
SupportedStyles:
- space
- no_space

Layout/SpaceBeforeBlockBraces:
Description: >-
Checks that the left block brace has or doesn't have space
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Expand Up @@ -238,6 +238,7 @@
require_relative 'rubocop/cop/layout/space_around_keyword'
require_relative 'rubocop/cop/layout/space_around_method_call_operator'
require_relative 'rubocop/cop/layout/space_around_operators'
require_relative 'rubocop/cop/layout/space_around_singleton_class_operator'
require_relative 'rubocop/cop/layout/space_before_block_braces'
require_relative 'rubocop/cop/layout/space_before_brackets'
require_relative 'rubocop/cop/layout/space_before_comma'
Expand Down
53 changes: 53 additions & 0 deletions lib/rubocop/cop/layout/space_around_singleton_class_operator.rb
@@ -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
@@ -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

0 comments on commit 8883ae5

Please sign in to comment.