Skip to content

Commit

Permalink
[Fix #8469] Add singleton class operator inspection
Browse files Browse the repository at this point in the history
We classify the << operator used for the singleton class
construct `class << self` as an operator like any other.
This means that not inspecting it was a bug, and this
change fixes it.
  • Loading branch information
jonas054 committed Jul 4, 2021
1 parent 33ed53a commit abb9032
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_space_around_singleton_class_operator.md
@@ -0,0 +1 @@
* [#8469](https://github.com/rubocop/rubocop/issues/8469): Add inspection of `class <<` to `Layout/SpaceAroundOperators`. ([@jonas054][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/layout/space_around_operators.rb
Expand Up @@ -63,6 +63,10 @@ def self.autocorrect_incompatible_with
[Style::SelfAssignment]
end

def on_sclass(node)
check_operator(:sclass, node.loc.operator, node.source_range)
end

def on_pair(node)
return unless node.hash_rocket?

Expand Down
12 changes: 9 additions & 3 deletions spec/rubocop/cop/layout/space_around_operators_spec.rb
Expand Up @@ -278,9 +278,15 @@ def init(name=nil)
RUBY
end

it 'accepts the construct class <<self with no space after <<' do
expect_no_offenses(<<~RUBY)
class <<self
it 'registers an offense and corrects singleton class operator`' do
expect_offense(<<~RUBY)
class<<self
^^ Surrounding space missing for operator `<<`.
end
RUBY

expect_correction(<<~RUBY)
class << self
end
RUBY
end
Expand Down

0 comments on commit abb9032

Please sign in to comment.