Class: RuboCop::Cop::Style::ModuleMemberExistenceCheck
- 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.
Constant Summary collapse
- MSG =
'Use `%<replacement>s` instead.'- RESTRICT_ON_SEND =
i[instance_methods].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #instance_methods_inclusion?(node) ⇒ Object
-
#on_send(node) ⇒ Object
(also: #on_csend)
rubocop:disable Metrics/AbcSize.
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
#instance_methods_inclusion?(node) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/rubocop/cop/style/module_member_existence_check.rb', line 37 def_node_matcher :instance_methods_inclusion?, "(call\n (call _ :instance_methods _?)\n {:include? :member?}\n _)\n" |
#on_send(node) ⇒ Object Also known as: on_csend
rubocop:disable Metrics/AbcSize
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rubocop/cop/style/module_member_existence_check.rb', line 44 def on_send(node) # rubocop:disable Metrics/AbcSize return unless (parent = node.parent) return unless instance_methods_inclusion?(parent) return unless simple_method_argument?(node) && simple_method_argument?(parent) offense_range = node.location.selector.join(parent.source_range.end) replacement = if node.first_argument.nil? || node.first_argument.true_type? "method_defined?(#{parent.first_argument.source})" else "method_defined?(#{parent.first_argument.source}, #{node.first_argument.source})" end add_offense(offense_range, message: format(MSG, replacement: replacement)) do |corrector| corrector.replace(offense_range, replacement) end end |