Skip to content

Commit

Permalink
Support autocorrection for Style/RedundantInitialize
Browse files Browse the repository at this point in the history
This PR supports autocorrection for `Style/RedundantInitialize`.
  • Loading branch information
koic authored and bbatsov committed Apr 21, 2022
1 parent e491166 commit 7fcb272
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10552](https://github.com/rubocop/rubocop/pull/10552): Support autocorrection for `Style/RedundantInitialize`. ([@koic][])
12 changes: 10 additions & 2 deletions lib/rubocop/cop/style/redundant_initialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ module Style
#
class RedundantInitialize < Base
include CommentsHelp
include RangeHelp
extend AutoCorrector

MSG = 'Remove unnecessary `initialize` method.'
MSG_EMPTY = 'Remove unnecessary empty `initialize` method.'
Expand All @@ -109,20 +111,26 @@ def on_def(node)
return if acceptable?(node)

if node.body.nil?
add_offense(node, message: MSG_EMPTY)
register_offense(node, MSG_EMPTY)
else
return if node.body.begin_type?

if (args, super_node = initialize_forwards?(node))
return unless same_args?(super_node, args)

add_offense(node)
register_offense(node, MSG)
end
end
end

private

def register_offense(node, message)
add_offense(node, message: message) do |corrector|
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
end
end

def acceptable?(node)
!node.method?(:initialize) || forwards?(node) || allow_comments?(node)
end
Expand Down
24 changes: 18 additions & 6 deletions spec/rubocop/cop/style/redundant_initialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ def do_something
RUBY
end

it 'registers an offense for an empty `initialize` method' do
it 'registers and corrects an offense for an empty `initialize` method' do
expect_offense(<<~RUBY)
def initialize
^^^^^^^^^^^^^^ Remove unnecessary empty `initialize` method.
end
RUBY

expect_correction('')
end

it 'does not register an offense for an `initialize` method with only a comment' do
Expand All @@ -34,31 +36,37 @@ def initialize
RUBY
end

it 'registers an offense for an `initialize` method that only calls `super`' do
it 'registers and corrects an offense for an `initialize` method that only calls `super`' do
expect_offense(<<~RUBY)
def initialize
^^^^^^^^^^^^^^ Remove unnecessary `initialize` method.
super
end
RUBY

expect_correction('')
end

it 'registers an offense for an `initialize` method with arguments that only calls `super`' do
it 'registers and corrects an offense for an `initialize` method with arguments that only calls `super`' do
expect_offense(<<~RUBY)
def initialize(a, b)
^^^^^^^^^^^^^^^^^^^^ Remove unnecessary `initialize` method.
super
end
RUBY

expect_correction('')
end

it 'registers an offense for an `initialize` method with arguments that only calls `super` with explicit args' do
it 'registers and corrects an offense for an `initialize` method with arguments that only calls `super` with explicit args' do
expect_offense(<<~RUBY)
def initialize(a, b)
^^^^^^^^^^^^^^^^^^^^ Remove unnecessary `initialize` method.
super(a, b)
end
RUBY

expect_correction('')
end

it 'does not register an offense for an `initialize` method that calls another method' do
Expand Down Expand Up @@ -103,13 +111,15 @@ def initialize(a, b)
RUBY
end

it 'registers an offense for an `initialize` method with no arguments that calls `super` with no arguments' do
it 'registers and corrects an offense for an `initialize` method with no arguments that calls `super` with no arguments' do
expect_offense(<<~RUBY)
def initialize()
^^^^^^^^^^^^^^^^ Remove unnecessary `initialize` method.
super()
end
RUBY

expect_correction('')
end

it 'does not register an offense for an `initialize` method with a default argument that calls `super`' do
Expand Down Expand Up @@ -176,13 +186,15 @@ def initialize(...)
context 'when `AllowComments: false`' do
let(:cop_config) { { 'AllowComments' => false } }

it 'registers an offense for an `initialize` method with only a comment' do
it 'registers and corrects an offense for an `initialize` method with only a comment' do
expect_offense(<<~RUBY)
def initialize
^^^^^^^^^^^^^^ Remove unnecessary empty `initialize` method.
# initializer
end
RUBY

expect_correction('')
end
end
end

0 comments on commit 7fcb272

Please sign in to comment.