From 3c51fb318bc34082f0dca84a7e32abb822304c26 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 3 Aug 2020 18:58:16 +0900 Subject: [PATCH] Fix an incorrect auto-correct for `Style/StructInheritance` This PR fixes the following incorrect auto-correct for `Style/StructInheritance` incorrect auto-correct for `Style/StructInheritance` when there is a comment before class declaration. ```console % cat example.rb # comment class Foo < Struct.new(:foo) end % bundle exec rubocop --only Style/StructInheritance -a (snip) Offenses: example.rb:2:13: C: [Corrected] Style/StructInheritance: Don't extend an instance initialized by Struct.new. Use a block to customize the struct. class Foo < Struct.new(:foo) ^^^^^^^^^^^^^^^^ 1 file inspected, 1 offense detected, 1 offense corrected % cat example.rb # commentFoo = Struct.new(:foo) do end ``` This issue has been reported on rubocop-jp. (Japanese) https://github.com/rubocop-hq/rubocop-jp/issues/61 --- CHANGELOG.md | 1 + lib/rubocop/cop/style/struct_inheritance.rb | 2 +- .../rubocop/cop/style/struct_inheritance_spec.rb | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b16da72a39..736d05fffee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ * [#8006](https://github.com/rubocop-hq/rubocop/issues/8006): Fix line length calculation for `Style/IfUnlessModifier` to correctly take into account code before the if condition when considering conversation to a single-line form. ([@dsavochkin][]) * [#8283](https://github.com/rubocop-hq/rubocop/issues/8283): Fix line length calculation for `Style/IfUnlessModifier` to correctly take into account a comment on the first line when considering conversation to a single-line form. ([@dsavochkin][]) * [#8226](https://github.com/rubocop-hq/rubocop/issues/8226): Fix `Style/IfUnlessModifier` to add parentheses when converting if-end condition inside an array or a hash to a single-line form. ([@dsavochkin][]) +* [#8443](https://github.com/rubocop-hq/rubocop/pull/8443): Fix an incorrect auto-correct for `Style/StructInheritance` when there is a comment before class declaration. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/style/struct_inheritance.rb b/lib/rubocop/cop/style/struct_inheritance.rb index d4fac0b41eb..30c5609e6bd 100644 --- a/lib/rubocop/cop/style/struct_inheritance.rb +++ b/lib/rubocop/cop/style/struct_inheritance.rb @@ -33,7 +33,7 @@ def on_class(node) def autocorrect(node) lambda do |corrector| - corrector.remove(range_with_surrounding_space(range: node.loc.keyword)) + corrector.remove(range_with_surrounding_space(range: node.loc.keyword, newlines: false)) corrector.replace(node.loc.operator, '=') correct_parent(node.parent_class, corrector) diff --git a/spec/rubocop/cop/style/struct_inheritance_spec.rb b/spec/rubocop/cop/style/struct_inheritance_spec.rb index e1f355cda4c..6a2cbe30616 100644 --- a/spec/rubocop/cop/style/struct_inheritance_spec.rb +++ b/spec/rubocop/cop/style/struct_inheritance_spec.rb @@ -60,6 +60,22 @@ class Person < ::Struct.new(:first_name, :last_name) do end RUBY end + it 'registers an offense when extending instance of `Struct` when there is a comment ' \ + 'before class declaration' do + expect_offense(<<~RUBY) + # comment + 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) + # comment + Person = Struct.new(:first_name, :last_name) do + end + RUBY + end + it 'accepts plain class' do expect_no_offenses(<<~RUBY) class Person