Class: RuboCop::Cop::Style::PredicateWithKind
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/style/predicate_with_kind.rb
Overview
Looks for uses of any?, all?, none?, or one? with a block containing only an is_a?, kind_of?, or instance_of? check, and suggests using the predicate method with the class argument directly.
Constant Summary collapse
- MSG =
'Prefer `%<replacement>s` to `%<original>s` with a kind check.'- RESTRICT_ON_SEND =
i[any? all? none? one?].freeze
- KIND_METHODS =
i[is_a? kind_of? instance_of?].to_set.freeze
Constants included from RangeHelp
RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #kind_call?(node, name) ⇒ Object
- #kind_check?(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
#kind_call?(node, name) ⇒ Object
45 46 47 |
# File 'lib/rubocop/cop/style/predicate_with_kind.rb', line 45 def_node_matcher :kind_call?, "(send (lvar %1) %KIND_METHODS _)\n" |
#kind_check?(node) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/rubocop/cop/style/predicate_with_kind.rb', line 36 def_node_matcher :kind_check?, "{\n (block call (args (arg $_)) $(send (lvar _) %KIND_METHODS _))\n (numblock call $1 $(send (lvar _) %KIND_METHODS _))\n (itblock call $_ $(send (lvar _) %KIND_METHODS _))\n}\n" |
#on_send(node) ⇒ Object Also known as: on_csend
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rubocop/cop/style/predicate_with_kind.rb', line 49 def on_send(node) return unless (block_node = node.block_node) return if block_node.body&.begin_type? return unless (kind_check_node = extract_send_node(block_node)) klass = kind_check_node.first_argument replacement = "#{node.method_name}(#{klass.source})" register_offense(node, block_node, klass, replacement) end |