Skip to content

Commit

Permalink
Fix an incorrect auto-correct for Style/ClassMethodsDefinitions
Browse files Browse the repository at this point in the history
This PR fixes the following incorrect auto-correct for `Style/ClassMethodsDefinitions`
when defining class methods with `class << self` and there is no blank line between
method definition and attribute accessor.

```console
% cat example.rb
class A
  class << self
    def three
    end
    attr_reader :two
  end
end
```

```console
% bundle exec rubocop --only Style/ClassMethodsDefinitions -a
(snip)

Inspecting 1 file
C

Offenses:

example.rb:2:3: C: [Corrected] Style/ClassMethodsDefinitions: Do not
define public methods within class << self.
class << self ...
^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
```

## Before

The syntax is broken as follows.

```console
% cat example.rb
class A
  class << self    attr_reader :two
  end

  def self.three
  end
end

% ruby -c example.rb
example.rb:2: syntax error, unexpected local variable or method,
expecting ';' or '\n'
class << self    attr_reader :two
example.rb:7: syntax error, unexpected `end', expecting end-of-input
```

## After

Safe auto-correction is done.

```console
% cat example.rb
class A
  class << self
  attr_reader :two
  end

  def self.three
  end
end
```

NOTE: This change may leave unwanted whitespace, which can be resolved with
`Layout/EmptyLinesAroundClassBody`. On the other hand, cannot auto-correction
for syntax error, so this PR fixes the syntax error first priority.
  • Loading branch information
koic authored and bbatsov committed Jan 28, 2021
1 parent ad85a55 commit 160e5df
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9424](https://github.com/rubocop-hq/rubocop/pull/9424): Fix an incorrect auto-correct for `Style/ClassMethodsDefinitions` when defining class methods with `class << self` and there is no blank line between method definition and attribute accessor. ([@koic][])
1 change: 0 additions & 1 deletion lib/rubocop/cop/mixin/comments_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module CommentsHelp
def source_range_with_comment(node)
begin_pos = begin_pos_with_comment(node)
end_pos = end_position_for(node)
end_pos += 1 if node.def_type?

Parser::Source::Range.new(buffer, begin_pos, end_pos)
end
Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cop/style/class_methods_definitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ def three
end
RUBY

expect_correction(<<~RUBY)
class A
class << self
attr_reader :two
end
def self.three
end
end
RUBY
end

it 'registers an offense and corrects when defining class methods with `class << self` and ' \
'there is no blank line between method definition and attribute accessor' do
expect_offense(<<~RUBY)
class A
class << self
^^^^^^^^^^^^^ Do not define public methods within class << self.
def three
end
attr_reader :two
end
end
RUBY

expect_correction(<<~RUBY)
class A
class << self
Expand Down Expand Up @@ -50,6 +76,7 @@ def two
class A
class << self
attr_reader :one
end
# Multiline
Expand Down Expand Up @@ -139,6 +166,7 @@ class A
class << self
def self.one
end
end
def self.two
Expand Down

0 comments on commit 160e5df

Please sign in to comment.