Skip to content

Commit

Permalink
Add auto-correct for Style/StructInheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane authored and bbatsov committed Jun 8, 2020
1 parent 9e25b30 commit fd2a24f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#8111](https://github.com/rubocop-hq/rubocop/pull/8111): Add auto-correct for `Style/StructInheritance`. ([@tejasbubane][])

## 0.85.1 (2020-06-07)

### Bug fixes
Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -3875,6 +3875,7 @@ Style/StructInheritance:
StyleGuide: '#no-extend-struct-new'
Enabled: true
VersionAdded: '0.29'
VersionChanged: '0.86'

Style/SymbolArray:
Description: 'Use %i or %I for arrays of symbols.'
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -8693,9 +8693,9 @@ This cop identifies places where `lstrip.rstrip` can be replaced by

| Enabled
| Yes
| No
| Yes
| 0.29
| -
| 0.86
|===

This cop checks for inheritance from Struct.new.
Expand Down
21 changes: 21 additions & 0 deletions lib/rubocop/cop/style/struct_inheritance.rb
Expand Up @@ -20,6 +20,8 @@ module Style
# end
# end
class StructInheritance < Cop
include RangeHelp

MSG = "Don't extend an instance initialized by `Struct.new`. " \
'Use a block to customize the struct.'

Expand All @@ -29,10 +31,29 @@ def on_class(node)
add_offense(node, location: node.parent_class.source_range)
end

def autocorrect(node)
lambda do |corrector|
corrector.remove(range_with_surrounding_space(range: node.loc.keyword))
corrector.replace(node.loc.operator, '=')

correct_parent(node.parent_class, corrector)
end
end

def_node_matcher :struct_constructor?, <<~PATTERN
{(send (const nil? :Struct) :new ...)
(block (send (const nil? :Struct) :new ...) ...)}
PATTERN

private

def correct_parent(parent, corrector)
if parent.block_type?
corrector.remove(range_with_surrounding_space(range: parent.loc.end, newlines: false))
else
corrector.insert_after(parent.loc.expression, ' do')
end
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/struct_inheritance_spec.rb
Expand Up @@ -7,6 +7,13 @@
expect_offense(<<~RUBY)
class Person < Struct.new(:first_name, :last_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct.
def foo; end
end
RUBY

expect_correction(<<~RUBY)
Person = Struct.new(:first_name, :last_name) do
def foo; end
end
RUBY
end
Expand All @@ -17,6 +24,11 @@ class Person < Struct.new(:first_name, :last_name) do end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct.
end
RUBY

expect_correction(<<~RUBY)
Person = Struct.new(:first_name, :last_name) do
end
RUBY
end

it 'accepts plain class' do
Expand Down

0 comments on commit fd2a24f

Please sign in to comment.