Skip to content

Commit

Permalink
Style/AccessorGrouping: Fix detection of Sorbet sig {} blocks
Browse files Browse the repository at this point in the history
- Sorbet `sig { returns(Integer) }` annotations above `attr_reader`
  methods should be ignored after
  9921cdc, per the docs, but they
  weren't being.
- Sorbet `sig {}` constructs are blocks (hence the braces), not
  `send_type?` directly.
- Hence, we now check if the previous expression is a block and if so,
  descend into its child nodes until we find a `send_type`, then use
  that to determine if the accessor is groupable.
- Relates to rubocop#11596.
  • Loading branch information
issyl0 authored and bbatsov committed Mar 3, 2023
1 parent 0dafb08 commit 008506d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11650](https://github.com/rubocop/rubocop/pull/11650): `Style/AccessorGrouping`: Fix detection of Sorbet `sig {}` blocks. ([@issyl0][])
14 changes: 13 additions & 1 deletion lib/rubocop/cop/style/accessor_grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ module Style
# # bad
# class Foo
# attr_reader :bar
# attr_reader :bax
# attr_reader :baz
# end
#
# # good
# class Foo
# attr_reader :bar, :baz
# attr_reader :bar, :bax, :baz
# end
#
# # good
# class Foo
# # may be intended comment for bar.
# attr_reader :bar
#
# sig { returns(String) }
# attr_reader :bax
#
# may_be_intended_annotation :baz
# attr_reader :baz
# end
Expand Down Expand Up @@ -90,6 +94,14 @@ def previous_line_comment?(node)

def groupable_accessor?(node)
return true unless (previous_expression = node.left_siblings.last)

# Accessors with Sorbet `sig { ... }` blocks shouldn't be groupable.
if previous_expression.block_type?
previous_expression.child_nodes.each do |child_node|
break previous_expression = child_node if child_node.send_type?
end
end

return true unless previous_expression.send_type?

previous_expression.attribute_accessor? || previous_expression.access_modifier?
Expand Down
5 changes: 5 additions & 0 deletions spec/rubocop/cop/style/accessor_grouping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,16 @@ class Foo
it 'does not register an offense for accessors with other methods' do
expect_no_offenses(<<~RUBY)
class Foo
extend T::Sig
annotation_method :one
attr_reader :one
annotation_method :two
attr_reader :two
sig { returns(Integer) }
attr_reader :three
end
RUBY
end
Expand Down

0 comments on commit 008506d

Please sign in to comment.