Module: RuboCop::Cop::Metrics::Utils::RepeatedAttributeDiscount Private

Extended by:
Macros
Includes:
AST::Sexp
Included in:
AbcSizeCalculator
Defined in:
lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Identifies repetitions `csend` calls with no arguments:

foo.bar
foo.bar # => repeated
foo.bar.baz.qux # => inner send repeated
foo.bar.baz.other # => both inner send repeated
foo.bar(2) # => not repeated

It also invalidates sequences if a receiver is reassigned:

xx.foo.bar
xx.foo.baz      # => inner send repeated
self.xx = any   # => invalidates everything so far
xx.foo.baz      # => no repetition
self.xx.foo.baz # => all repeated

Instance Method Summary collapse

Instance Method Details

#calculate_node(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


53
54
55
56
# 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_attributes?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

43
44
45
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 43

def discount_repeated_attributes?
  defined?(@known_attributes)
end

#evaluate_branch_nodes(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


47
48
49
50
51
# 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

#initialize(node, discount_repeated_attributes: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Plug into the calculator


30
31
32
33
34
35
36
37
38
39
40
41
# 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