Class: RuboCop::Cop::Style::ModuleMemberExistenceCheck

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/style/module_member_existence_check.rb

Overview

Checks for usage of ‘Module` methods returning arrays that can be replaced with equivalent predicates.

Calling a method returning an array then checking if an element is inside it is much slower than using an equivalent predicate method. For example, ‘instance_methods.include?` will return an array of all public and protected instance methods in the module, then check if a given method is inside that array, while `method_defined?` will do direct method lookup, which is much faster and consumes less memory.

NOTE: ‘constants.include?` is not handled by this cop because `Module#const_defined?` has different lookup behavior than `Module#constants` - `const_defined?` searches up to `Object` (top-level constants like `String`, `Integer`, etc.) while `constants` does not, which can cause behavior changes after autocorrection.

Examples:

# bad
Array.instance_methods.include?(:size)
Array.instance_methods.member?(:size)
Array.instance_methods(true).include?(:size)

Array.instance_methods(false).include?(:find)

# good
Array.method_defined?(:size)

Array.method_defined?(:find, false)

# bad
Array.class_variables.include?(:foo)
Array.private_instance_methods.include?(:foo)
Array.protected_instance_methods.include?(:foo)
Array.public_instance_methods.include?(:foo)

# good
Array.class_variable_defined?(:foo)
Array.private_method_defined?(:foo)
Array.protected_method_defined?(:foo)
Array.public_method_defined?(:foo)

Constant Summary collapse

MSG =
'Use `%<replacement>s` instead.'
METHOD_REPLACEMENTS =
{
  class_variables: :class_variable_defined?,
  instance_methods: :method_defined?,
  private_instance_methods: :private_method_defined?,
  protected_instance_methods: :protected_method_defined?,
  public_instance_methods: :public_method_defined?
}.freeze
METHODS_WITHOUT_INHERIT_PARAM =
Set[:class_variables].freeze
METHODS_WITH_INHERIT_PARAM =
(METHOD_REPLACEMENTS.keys.to_set - METHODS_WITHOUT_INHERIT_PARAM).freeze
RESTRICT_ON_SEND =
METHOD_REPLACEMENTS.keys.freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

cop_dir_for, #exclude_limit, read_limits

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#module_member_inclusion?(node) ⇒ Object



53
54
55
56
57
58
# File 'lib/rubocop/cop/style/module_member_existence_check.rb', line 53

def_node_matcher :module_member_inclusion?, <<~PATTERN
  (call
    {(call _ %METHODS_WITH_INHERIT_PARAM _?) (call _ %METHODS_WITHOUT_INHERIT_PARAM)}
    {:include? :member?}
    _)
PATTERN

#on_send(node) ⇒ Object Also known as: on_csend



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubocop/cop/style/module_member_existence_check.rb', line 74

def on_send(node)
  return unless (parent = node.parent)
  return unless module_member_inclusion?(parent)
  return unless simple_method_argument?(node) && simple_method_argument?(parent)

  offense_range = node.location.selector.join(parent.source_range.end)
  replacement = replacement_for(node, parent)

  add_offense(offense_range, message: format(MSG, replacement: replacement)) do |corrector|
    corrector.replace(offense_range, replacement)
  end
end