-
-
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.
…le multiple bisected attrs in the same macro.
- Loading branch information
1 parent
211c017
commit 56d645d
Showing
4 changed files
with
245 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#9622](https://github.com/rubocop/rubocop/issues/9622): Fixed `Style/BisectedAttrAccessor` autocorrection to handle multiple bisected attrs in the same macro. ([@dvandersluis][]) |
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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
class BisectedAttrAccessor | ||
# Representation of an `attr_reader`, `attr_writer` or `attr` macro | ||
# for use by `Style/BisectedAttrAccessor`. | ||
# @api private | ||
class Macro | ||
include VisibilityHelp | ||
|
||
attr_reader :node, :attrs, :bisection | ||
|
||
def self.macro?(node) | ||
node.method?(:attr_reader) || node.method?(:attr_writer) || node.method?(:attr) | ||
end | ||
|
||
def initialize(node) | ||
@node = node | ||
@attrs = node.arguments.map do |attr| | ||
[attr.source, attr] | ||
end.to_h | ||
@bisection = [] | ||
end | ||
|
||
def bisect(*names) | ||
@bisection = attrs.slice(*names).values | ||
end | ||
|
||
def attr_names | ||
@attr_names ||= attrs.keys | ||
end | ||
|
||
def bisected_names | ||
bisection.map(&:source) | ||
end | ||
|
||
def visibility | ||
@visibility ||= node_visibility(node) | ||
end | ||
|
||
def reader? | ||
node.method?(:attr_reader) || node.method?(:attr) | ||
end | ||
|
||
def writer? | ||
node.method?(:attr_writer) | ||
end | ||
|
||
def all_bisected? | ||
rest.none? | ||
end | ||
|
||
def rest | ||
@rest ||= attr_names - bisected_names | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.