diff --git a/changelog/new_layout_spacearoundsingletonclassoperator.md b/changelog/new_layout_spacearoundsingletonclassoperator.md new file mode 100644 index 00000000000..742534ab757 --- /dev/null +++ b/changelog/new_layout_spacearoundsingletonclassoperator.md @@ -0,0 +1 @@ +* [#8469](https://github.com/rubocop/rubocop/issues/8469): Add cop `Layout/SpaceAroundSingletonClassOperator`. ([@jonas054][]) diff --git a/config/default.yml b/config/default.yml index 11af268c520..147efbe700a 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1226,6 +1226,15 @@ Layout/SpaceAroundOperators: - space - no_space +Layout/SpaceAroundSingletonClassOperator: + Description: 'Use a single space around the singleton class operator.' + Enabled: pending + VersionAdded: '<>' + EnforcedStyle: space + SupportedStyles: + - space + - no_space + Layout/SpaceBeforeBlockBraces: Description: >- Checks that the left block brace has or doesn't have space diff --git a/lib/rubocop.rb b/lib/rubocop.rb index c9a937ea142..cfdb5222761 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -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' diff --git a/lib/rubocop/cop/layout/space_around_singleton_class_operator.rb b/lib/rubocop/cop/layout/space_around_singleton_class_operator.rb new file mode 100644 index 00000000000..f9966ececd7 --- /dev/null +++ b/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< '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 < 'no_space' } } + + it 'registers an offense for space to the left`' do + expect_offense(<<~RUBY) + class <