class RuboCop::Cop::Naming::MemoizedInstanceVariableName
end
@_foo ||= calculate_expensive_thing
define_method(:foo) do
# good
end
@foo ||= calculate_expensive_thing
define_method(:foo) do
# good
end
@_foo = calculate_expensive_thing
return @_foo if defined?(@_foo)
def foo
# good
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
return @_foo if defined?(@_foo)
define_method(:foo) do
# good
end
@_foo ||= calculate_expensive_thing
define_method(:foo) do
# good
end
@_foo = calculate_expensive_thing
return @_foo if defined?(@_foo)
def foo
end
@_foo ||= calculate_expensive_thing
def _foo
# good
end
@_foo ||= calculate_expensive_thing
def foo
# good
end
@foo = calculate_expensive_thing
return @foo if defined?(@foo)
def foo
end
@foo ||= calculate_expensive_thing
def foo
# bad
end
@something ||= calculate_expensive_thing
def foo
# bad
@example EnforcedStyleForLeadingUnderscores: required
end
@foo = calculate_expensive_thing
return @foo if defined?(@foo)
define_method(:foo) do
# good
end
@foo ||= calculate_expensive_thing
define_method(:foo) do
# good
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
return @something if defined?(@something)
def foo
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)
because it may conflict with instance variable names already in use.
so this cop is considered unsafe. Also, its autocorrection is unsafe
but this is sometimes used for other purposes than memoization
This cop relies on the pattern `@instance_var ||= …`,
@safety
be set or referenced 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
`define_method` or `define_singleton_method`).
(defined with `def`) and dynamic methods (defined with
does not match the method name. Applies to both regular methods
Checks for memoized methods whose instance variable name
def find_definition(node)
def find_definition(node) # Methods can be defined in a `def` or `defs`, # or dynamically via a `block` node. node.each_ancestor(:any_def, :block).each do |ancestor| method_node, method_name = method_definition?(ancestor) return [method_node, method_name] if method_node end nil end
def matches?(method_name, ivar_assign)
def matches?(method_name, ivar_assign) return true if ivar_assign.nil? || INITIALIZE_METHODS.include?(method_name) method_name = method_name.to_s.delete('!?=') variable_name = ivar_assign.name.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_defined?(node)
def on_defined?(node) arg = node.first_argument return false unless arg.ivar_type? method_node, method_name = find_definition(node) return false unless method_node defined_memoized?(method_node.body, arg.name) do |defined_ivar, return_ivar, ivar_assign| return false if matches?(method_name, ivar_assign) suggested_var = suggested_var(method_name) msg = format( message(arg.name), var: arg.name, suggested_var: suggested_var, method: method_name ) add_offense(defined_ivar, message: msg) do |corrector| corrector.replace(defined_ivar, "@#{suggested_var}") end add_offense(return_ivar, message: msg) do |corrector| corrector.replace(return_ivar, "@#{suggested_var}") end add_offense(ivar_assign.loc.name, message: msg) do |corrector| corrector.replace(ivar_assign.loc.name, "@#{suggested_var}") end end end
def on_or_asgn(node)
rubocop:disable Metrics/AbcSize
def on_or_asgn(node) lhs = node.lhs return unless lhs.ivasgn_type? method_node, method_name = find_definition(node) return unless method_node body = method_node.body return unless body == node || body.children.last == node return if matches?(method_name, lhs) suggested_var = suggested_var(method_name) msg = format( message(lhs.name), var: lhs.name, suggested_var: suggested_var, method: method_name ) add_offense(lhs, message: msg) do |corrector| corrector.replace(lhs.loc.name, "@#{suggested_var}") end 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.delete_prefix('_') 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