Class: RuboCop::Cop::InternalAffairs::NodeTypeMultiplePredicates

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

Overview

Use ‘node.type?(:foo, :bar)` instead of `node.foo_type? || node.bar_type?`, and `!node.type?(:foo, :bar)` instead of `!node.foo_type? && !node.bar_type?`.

Examples:


# bad
node.str_type? || node.sym_type?

# good
node.type?(:str, :sym)

# bad
node.type?(:str, :sym) || node.boolean_type?

# good
node.type?(:str, :sym, :boolean)

# bad
!node.str_type? && !node.sym_type?

# good
!node.type?(:str, :sym)

# bad
!node.type?(:str, :sym) && !node.boolean_type?

# good
!node.type?(:str, :sym, :boolean)

Constant Summary collapse

MSG_OR =
'Use `%<replacement>s` instead of checking for multiple node types.'
MSG_AND =
'Use `%<replacement>s` instead of checking against multiple node types.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

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

#exclude_limit

Methods included from AutocorrectLogic

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

Methods included from RuboCop::Cop::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

#and_not_another_type?(node) ⇒ Object

[View source]

63
64
65
66
67
68
# File 'lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb', line 63

def_node_matcher :and_not_another_type?, "(and {\n  (send $(call _receiver :type? sym+) :!) (send (call _receiver #type_predicate?) :!) |\n  (send (call _receiver #type_predicate?) :!) (send $(call _receiver :type? sym+) :!)\n})\n"

#none_of_node_types?(node) ⇒ Object

[View source]

55
56
57
58
59
60
# File 'lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb', line 55

def_node_matcher :none_of_node_types?, "(and\n  (send $(call _receiver #type_predicate?) :!)\n  (send (call _receiver #type_predicate?) :!)\n)\n"

#on_and(node) ⇒ Object

[View source]

80
81
82
83
84
85
86
87
88
89
# File 'lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb', line 80

def on_and(node)
  return unless (send_node = none_of_node_types?(node) || and_not_another_type?(node))
  return unless send_node.receiver

  replacement = "!#{replacement(node, send_node)}"

  add_offense(node, message: format(MSG_AND, replacement: replacement)) do |corrector|
    corrector.replace(node, replacement)
  end
end

#on_or(node) ⇒ Object

[View source]

70
71
72
73
74
75
76
77
78
# File 'lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb', line 70

def on_or(node)
  return unless (send_node = one_of_node_types?(node) || or_another_type?(node))
  return unless send_node.receiver

  replacement = replacement(node, send_node)
  add_offense(node, message: format(MSG_OR, replacement: replacement)) do |corrector|
    corrector.replace(node, replacement)
  end
end

#one_of_node_types?(node) ⇒ Object

[View source]

42
43
44
# File 'lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb', line 42

def_node_matcher :one_of_node_types?, "(or $(call _receiver #type_predicate?) (call _receiver #type_predicate?))\n"

#or_another_type?(node) ⇒ Object

[View source]

47
48
49
50
51
52
# File 'lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb', line 47

def_node_matcher :or_another_type?, "(or {\n  $(call _receiver :type? sym+) (call _receiver #type_predicate?) |\n  (call _receiver #type_predicate?) $(call _receiver :type? sym+)\n})\n"