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

Constant Summary collapse

VAR_SETTER_TO_GETTER =

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

{
  lvasgn: :lvar,
  ivasgn: :ivar,
  cvasgn: :cvar,
  gvasgn: :gvar
}.freeze

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.



60
61
62
63
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 60

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)


50
51
52
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 50

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.



54
55
56
57
58
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 54

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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb', line 37

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