Class: RuboCop::Cop::Style::ModuleMemberExistenceCheck
- Extended by:
- AutoCorrector
- Includes:
- AllowedMethods
- 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.
Constant Summary collapse
- MSG =
'Use `%<replacement>s` instead.'- METHOD_REPLACEMENTS =
{ class_variables: :class_variable_defined?, constants: :const_defined?, included_modules: :include?, 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, :included_modules].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
Instance Method Summary collapse
- #module_member_inclusion?(node) ⇒ Object
- #on_send(node) ⇒ Object (also: #on_csend)
Methods included from AutoCorrector
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
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#module_member_inclusion?(node) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/rubocop/cop/style/module_member_existence_check.rb', line 57 def_node_matcher :module_member_inclusion?, "(call\n {(call _ %METHODS_WITH_INHERIT_PARAM _?) (call _ %METHODS_WITHOUT_INHERIT_PARAM)}\n {:include? :member?}\n _)\n" |
#on_send(node) ⇒ Object Also known as: on_csend
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rubocop/cop/style/module_member_existence_check.rb', line 80 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) return if allowed_method?(node.method_name) 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 |