Module: RuboCop::Cop::Metrics::Utils::RepeatedAttributeDiscount Private
Do not use. This module is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Macros
|
|
Instance Chain:
self,
RuboCop::AST::Sexp
|
|
Defined in: | lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb |
Overview
Identifies repetitions {c}send
calls with no arguments:
foo.
foo. # => repeated
foo. .baz.qux # => inner send repeated
foo. .baz.other # => both inner send repeated
foo. (2) # => not repeated
It also invalidates sequences if a receiver is reassigned:
xx.foo.
xx.foo.baz # => inner send repeated
self.xx = any # => invalidates everything so far
xx.foo.baz # => no repetition
self.xx.foo.baz # => all repeated
Constant Summary
-
VAR_SETTER_TO_GETTER =
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 117{ lvasgn: :lvar, ivasgn: :ivar, cvasgn: :cvar, gvasgn: :gvar }.freeze
Instance Attribute Summary
- #discount_repeated_attributes? ⇒ Boolean readonly Internal use only
Instance Method Summary
- #calculate_node(node) Internal use only
- #evaluate_branch_nodes(node) Internal use only
-
#initialize(node, discount_repeated_attributes: false)
Internal use only
Plug into the calculator.
- #attribute_call?(node) private
- #discount_repeated_attribute?(send_node) ⇒ Boolean private Internal use only
-
#find_attributes(node, &block)
private
Internal use only
Returns the "known_attributes" for the
node
by walking the receiver tree If at any step the subdirectory does not exist, it is yielded with the associated key (method_name) If the node is not a series of(c)send
calls with no arguments, thennil
is yielded. - #root_node?(node) private
-
#setter_to_getter(node)
private
Internal use only
or
nil
if it is not a setter. - #update_repeated_attribute(node) private Internal use only
Instance Attribute Details
#discount_repeated_attributes? ⇒ Boolean
(readonly)
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 43
def discount_repeated_attributes? defined?(@known_attributes) end
Instance Method Details
#attribute_call?(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 61
def_node_matcher :attribute_call?, <<~PATTERN (call _receiver _method # and no parameters ) PATTERN
#calculate_node(node)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 53
def calculate_node(node) update_repeated_attribute(node) if discount_repeated_attributes? super end
#discount_repeated_attribute?(send_node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 66
def discount_repeated_attribute?(send_node) return false unless attribute_call?(send_node) repeated = true find_attributes(send_node) do |hash, lookup| return false if hash.nil? repeated = false hash[lookup] = {} end repeated end
#evaluate_branch_nodes(node)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 47
def evaluate_branch_nodes(node) return if discount_repeated_attributes? && discount_repeated_attribute?(node) super end
#find_attributes(node, &block) (private)
Returns the "known_attributes" for the node
by walking the receiver tree
If at any step the subdirectory does not exist, it is yielded with the
associated key (method_name)
If the node is not a series of (c)send
calls with no arguments,
then nil
is yielded
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 103
def find_attributes(node, &block) if attribute_call?(node) calls = find_attributes(node.receiver, &block) value = node.method_name elsif root_node?(node) calls = @known_attributes value = node else return yield nil end calls.fetch(value) { yield [calls, value] } end
#initialize(node, discount_repeated_attributes: false)
Plug into the calculator
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 30
def initialize(node, discount_repeated_attributes: false) super(node) return unless discount_repeated_attributes self_attributes = {} # Share hash for `(send nil? :foo)` and `(send (self) :foo)` @known_attributes = { s(:self) => self_attributes, nil => self_attributes } # example after running `obj = foo.bar; obj.baz.qux` # { nil => {foo: {bar: {}}}, # s(self) => same hash ^, # s(:lvar, :obj) => {baz: {qux: {}}} # } end
#root_node?(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 92
def_node_matcher :root_node?, <<~PATTERN { nil? | self # e.g. receiver of `my_method` or `self.my_attr` | lvar | ivar | cvar | gvar # e.g. receiver of `var.my_method` | const } # e.g. receiver of `MyConst.foo.bar` PATTERN
#setter_to_getter(node) (private)
or nil
if it is not a setter.
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 126
def setter_to_getter(node) if (type = VAR_SETTER_TO_GETTER[node.type]) # (lvasgn :my_var (int 42)) => [(lvar my_var), nil] [s(type, node.children.first), nil] elsif node.shorthand_asgn? # (or-asgn (send _receiver :foo) _value) # (or-asgn (send _receiver :foo) _value) => [_receiver, :foo] node.children.first.children elsif node.respond_to?(:setter_method?) && node.setter_method? # (send _receiver :foo= (int 42) ) => [_receiver, :foo] method_name = node.method_name[0...-1].to_sym [node.receiver, method_name] end end
#update_repeated_attribute(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 80
def update_repeated_attribute(node) return unless (receiver, method = setter_to_getter(node)) calls = find_attributes(receiver) { return } if method # e.g. `self.foo = 42` calls.delete(method) else # e.g. `var = 42` calls.clear end end