class RuboCop::Cop::Naming::MemoizedInstanceVariableName

end
@_foo ||= calculate_expensive_thing
def _foo
# good
end
@_foo ||= calculate_expensive_thing
def foo
# good
end
@foo ||= calculate_expensive_thing
def foo
# good
end
@something ||= calculate_expensive_thing
def foo
# bad
@example EnforcedStyleForLeadingUnderscores :optional
end
@_foo ||= calculate_expensive_thing
def _foo
# good
end
@_foo ||= calculate_expensive_thing
def foo
# good
end
@foo ||= calculate_expensive_thing
def foo
# bad
end
@something ||= calculate_expensive_thing
def foo
# bad
@example EnforcedStyleForLeadingUnderscores: required
end
@foo ||= calculate_expensive_thing(helper_variable)
helper_variable = something_we_need_to_calculate_foo
def foo
# good
end
end
calculate_expensive_thing
@foo ||= begin
def foo
# good
end
@foo ||= calculate_expensive_thing
def foo
# good
end
@foo ||= calculate_expensive_thing
def _foo
# good
end
@something ||= calculate_expensive_thing
def foo
# not ‘@foo`. This can cause confusion and bugs.
# Method foo is memoized using an instance variable that is
# bad
@example EnforcedStyleForLeadingUnderscores: disallowed (default)
be set or referencd outside of the memoization method.
convention that is used to implicitly indicate that an ivar should not
prefixed with an underscore. Prefixing ivars with an underscore is a
directive. It can be configured to allow for memoized instance variables
This cop can be configured with the EnforcedStyleForLeadingUnderscores
does not match the method name.
This cop checks for memoized methods whose instance variable name

def self.node_pattern

def self.node_pattern
  memo_assign = '(or_asgn $(ivasgn _) _)'
  memoized_at_end_of_method = "(begin ... #{memo_assign})"
  instance_method =
    "(def $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
  class_method =
    "(defs self $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
  "{#{instance_method} #{class_method}}"
end

def matches?(method_name, ivar_assign)

def matches?(method_name, ivar_assign)
  return true if ivar_assign.nil? || method_name == :initialize
  method_name = method_name.to_s.delete('!?')
  variable = ivar_assign.children.first
  variable_name = variable.to_s.sub('@', '')
  variable_name_candidates(method_name).include?(variable_name)
end

def message(variable)

def message(variable)
  variable_name = variable.to_s.sub('@', '')
  return UNDERSCORE_REQUIRED if style == :required &&
                                !variable_name.start_with?('_')
  MSG
end

def on_def(node)

def on_def(node)
  (method_name, ivar_assign) = memoized?(node)
  return if matches?(method_name, ivar_assign)
  msg = format(
    message(ivar_assign.children.first.to_s),
    var: ivar_assign.children.first.to_s,
    suggested_var: suggested_var(method_name),
    method: method_name
  )
  add_offense(node, location: ivar_assign.source_range, message: msg)
end

def style_parameter_name

def style_parameter_name
  'EnforcedStyleForLeadingUnderscores'
end

def suggested_var(method_name)

def suggested_var(method_name)
  suggestion = method_name.to_s.delete('!?')
  style == :required ? "_#{suggestion}" : suggestion
end

def variable_name_candidates(method_name)

def variable_name_candidates(method_name)
  no_underscore = method_name.sub(/\A_/, '')
  with_underscore = "_#{method_name}"
  case style
  when :required
    [with_underscore,
     method_name.start_with?('_') ? method_name : nil].compact
  when :disallowed
    [method_name, no_underscore]
  when :optional
    [method_name, with_underscore, no_underscore]
  else
    raise 'Unreachable'
  end
end