From 8883ae5f9a3246b20310c928dfe31a11a394705d Mon Sep 17 00:00:00 2001 From: Jonas Arvidsson Date: Sat, 3 Jul 2021 12:41:22 +0200 Subject: [PATCH] [Fix #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. --- ...ayout_spacearoundsingletonclassoperator.md | 1 + config/default.yml | 9 +++ lib/rubocop.rb | 1 + .../space_around_singleton_class_operator.rb | 53 +++++++++++++++ ...ce_around_singleton_class_operator_spec.rb | 65 +++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 changelog/new_layout_spacearoundsingletonclassoperator.md create mode 100644 lib/rubocop/cop/layout/space_around_singleton_class_operator.rb create mode 100644 spec/rubocop/cop/layout/space_around_singleton_class_operator_spec.rb 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 <