-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix false positives for
Style/AccessorGrouping
when accessors have …
…different access modifiers
- Loading branch information
Showing
6 changed files
with
220 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Help methods for determining node visibility. | ||
module VisibilityHelp | ||
extend NodePattern::Macros | ||
|
||
VISIBILITY_SCOPES = %i[private protected public].freeze | ||
|
||
private | ||
|
||
def node_visibility(node) | ||
scope = find_visibility_start(node) | ||
scope&.method_name || :public | ||
end | ||
|
||
def find_visibility_start(node) | ||
left_siblings_of(node) | ||
.reverse | ||
.find(&method(:visibility_block?)) | ||
end | ||
|
||
# Navigate to find the last protected method | ||
def find_visibility_end(node) | ||
possible_visibilities = VISIBILITY_SCOPES - [node_visibility(node)] | ||
right = right_siblings_of(node) | ||
right.find do |child_node| | ||
possible_visibilities.include?(node_visibility(child_node)) | ||
end || right.last | ||
end | ||
|
||
def left_siblings_of(node) | ||
siblings_of(node)[0, node.sibling_index] | ||
end | ||
|
||
def right_siblings_of(node) | ||
siblings_of(node)[node.sibling_index..-1] | ||
end | ||
|
||
def siblings_of(node) | ||
node.parent.children | ||
end | ||
|
||
def_node_matcher :visibility_block?, <<~PATTERN | ||
(send nil? { :private :protected :public }) | ||
PATTERN | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters