From 5658046f33ba4a12feba741bd8ec403c1bebb73a Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Mon, 1 Apr 2019 19:06:46 -0400 Subject: [PATCH 1/2] Mention block form of Struct.new in Style/StructInheritance As the documentation for `Struct` recommends using the block form over inheriting, I think it is worth mentioning it. > If a block is given it will be evaluated in the context of StructClass, passing the created class as a parameter > This is the recommended way to customize a struct. Adjust examples in the documentation and add a test which uses the block form. --- lib/rubocop/cop/style/struct_inheritance.rb | 12 ++++++++++-- manual/cops_style.md | 9 ++++++++- spec/rubocop/cop/style/struct_inheritance_spec.rb | 14 ++++++++++++-- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/rubocop/cop/style/struct_inheritance.rb b/lib/rubocop/cop/style/struct_inheritance.rb index 9ff05a9ecea..71520b61efa 100644 --- a/lib/rubocop/cop/style/struct_inheritance.rb +++ b/lib/rubocop/cop/style/struct_inheritance.rb @@ -8,12 +8,20 @@ module Style # @example # # bad # class Person < Struct.new(:first_name, :last_name) + # def age + # 42 + # end # end # # # good - # Person = Struct.new(:first_name, :last_name) + # Person = Struct.new(:first_name, :last_name) do + # def age + # 42 + # end + # end class StructInheritance < Cop - MSG = "Don't extend an instance initialized by `Struct.new`.".freeze + MSG = "Don't extend an instance initialized by `Struct.new`. " \ + 'Use a block to customize the struct.'.freeze def on_class(node) _name, superclass, _body = *node diff --git a/manual/cops_style.md b/manual/cops_style.md index ce476392615..3eb55349374 100644 --- a/manual/cops_style.md +++ b/manual/cops_style.md @@ -5954,10 +5954,17 @@ This cop checks for inheritance from Struct.new. ```ruby # bad class Person < Struct.new(:first_name, :last_name) + def age + 42 + end end # good -Person = Struct.new(:first_name, :last_name) +Person = Struct.new(:first_name, :last_name) do + def age + 42 + end +end ``` ### References diff --git a/spec/rubocop/cop/style/struct_inheritance_spec.rb b/spec/rubocop/cop/style/struct_inheritance_spec.rb index e1451a6e10d..592203c6c45 100644 --- a/spec/rubocop/cop/style/struct_inheritance_spec.rb +++ b/spec/rubocop/cop/style/struct_inheritance_spec.rb @@ -6,7 +6,7 @@ it 'registers an offense when extending instance of Struct' do expect_offense(<<-RUBY.strip_indent) class Person < Struct.new(:first_name, :last_name) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct. end RUBY end @@ -14,7 +14,7 @@ class Person < Struct.new(:first_name, :last_name) it 'registers an offense when extending instance of Struct with do ... end' do expect_offense(<<-RUBY.strip_indent) class Person < Struct.new(:first_name, :last_name) do end - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct. end RUBY end @@ -36,4 +36,14 @@ class Person < DelegateClass(Animal) it 'accepts assignment to Struct.new' do expect_no_offenses('Person = Struct.new(:first_name, :last_name)') end + + it 'accepts assignment to block form of Struct.new' do + expect_no_offenses(<<-RUBY.strip_indent) + Person = Struct.new(:first_name, :last_name) do + def age + 42 + end + end + RUBY + end end From 8cddeb1265f81ce53f4316097312d889b8208d5c Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Mon, 1 Apr 2019 19:11:09 -0400 Subject: [PATCH 2/2] Add a changelog entry for #6875 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9be78ffb62..79c267f7f57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * [#5977](https://github.com/rubocop-hq/rubocop/issues/5977): Warn for Performance Cops. ([@koic][]) * [#6637](https://github.com/rubocop-hq/rubocop/issues/6637): Move `LstripRstrip` from `Performance` to `Style` department and rename it to `Strip`. ([@anuja-joshi][]) +* [#6875](https://github.com/rubocop-hq/rubocop/pull/6875): Mention block form of `Struct.new` in ` Style/StructInheritance`. ([@XrXr][]) ## 0.66.0 (2019-03-18) @@ -3894,3 +3895,4 @@ [@ericsullivan]: https://github.com/ericsullivan [@aeroastro]: https://github.com/aeroastro [@anuja-joshi]: https://github.com/anuja-joshi +[@XrXr]: https://github.com/XrXr