Class: RuboCop::Cop::Style::InverseMethods
- Extended by:
- AutoCorrector
- Includes:
- IgnoredNode, RangeHelp
- Defined in:
- lib/rubocop/cop/style/inverse_methods.rb
Overview
This cop check for usages of not (‘not` or `!`) called on a method when an inverse of that method can be used instead. Methods that can be inverted by a not (`not` or `!`) should be defined in `InverseMethods` Methods that are inverted by inverting the return of the block that is passed to the method should be defined in `InverseBlocks`
Constant Summary collapse
- MSG =
'Use `%<inverse>s` instead of inverting `%<method>s`.'
- CLASS_COMPARISON_METHODS =
%i[<= >= < >].freeze
- EQUALITY_METHODS =
%i[== != =~ !~ <= >= < >].freeze
- NEGATED_EQUALITY_METHODS =
%i[!= !~].freeze
- CAMEL_CASE =
/[A-Z]+[a-z]+/.freeze
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #inverse_block?(node) ⇒ Object
- #inverse_candidate?(node) ⇒ Object
- #on_block(node) ⇒ Object
- #on_send(node) ⇒ Object
Methods included from AutoCorrector
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods inherited from Base
#add_global_offense, #add_offense, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
Methods included from Util
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Class Method Details
.autocorrect_incompatible_with ⇒ Object
47 48 49 |
# File 'lib/rubocop/cop/style/inverse_methods.rb', line 47 def self.autocorrect_incompatible_with [Style::Not, Style::SymbolProc] end |
Instance Method Details
#inverse_block?(node) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/rubocop/cop/style/inverse_methods.rb', line 61 def_node_matcher :inverse_block?, <<~PATTERN (block $(send (...) $_) ... { $(send ... :!) $(send (...) {:!= :!~} ...) (begin ... $(send ... :!)) (begin ... $(send (...) {:!= :!~} ...)) }) PATTERN |
#inverse_candidate?(node) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/rubocop/cop/style/inverse_methods.rb', line 52 def_node_matcher :inverse_candidate?, <<~PATTERN { (send $(send $(...) $_ $...) :!) (send (block $(send $(...) $_) $...) :!) (send (begin $(send $(...) $_ $...)) :!) } PATTERN |
#on_block(node) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rubocop/cop/style/inverse_methods.rb', line 82 def on_block(node) inverse_block?(node) do |_method_call, method, block| return unless inverse_blocks.key?(method) return if negated?(node) && negated?(node.parent) return if node.each_node(:next).any? # Inverse method offenses inside of the block of an inverse method # offense, such as `y.reject { |key, _value| !(key =~ /c\d/) }`, # can cause auto-correction to apply improper corrections. ignore_node(block) add_offense(node, message: (method, inverse_blocks[method])) do |corrector| correct_inverse_block(corrector, node) end end end |
#on_send(node) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rubocop/cop/style/inverse_methods.rb', line 69 def on_send(node) inverse_candidate?(node) do |_method_call, lhs, method, rhs| return unless inverse_methods.key?(method) return if negated?(node) return if part_of_ignored_node?(node) return if possible_class_hierarchy_check?(lhs, rhs, method) add_offense(node, message: (method, inverse_methods[method])) do |corrector| correct_inverse_method(corrector, node) end end end |