class RuboCop::Cop::Style::InverseMethods
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`.
@safety
This cop is unsafe because it cannot be guaranteed that the method and its inverse method are both defined on receiver, and also are actually inverse of each other.
@example
# bad !foo.none? !foo.any? { |f| f.even? } !foo.blank? !(foo == bar) foo.select { |f| !f.even? } foo.reject { |f| f != 7 } # good foo.none? foo.blank? foo.any? { |f| f.even? } foo != bar foo == bar !!('foo' =~ /^\w+$/) !(foo.class < Numeric) # Checking class hierarchy is allowed # Blocks with guard clauses are ignored: foo.select do |f| next if f.zero? f != 1 end
Constants
- CAMEL_CASE
- CLASS_COMPARISON_METHODS
- EQUALITY_METHODS
- MSG
- NEGATED_EQUALITY_METHODS
- RESTRICT_ON_SEND
- SAFE_NAVIGATION_INCOMPATIBLE_METHODS
Public Class Methods
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 56 def self.autocorrect_incompatible_with [Style::Not, Style::SymbolProc] end
Public Instance Methods
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 92 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 autocorrection to apply improper corrections. ignore_node(block) add_offense(node, message: message(method, inverse_blocks[method])) do |corrector| correct_inverse_block(corrector, node) end end end
Also aliased as: on_numblock, on_itblock
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 78 def on_send(node) inverse_candidate?(node) do |method_call, lhs, method, rhs| return unless inverse_methods.key?(method) return if negated?(node) || safe_navigation_incompatible?(method_call) return if part_of_ignored_node?(node) return if possible_class_hierarchy_check?(lhs, rhs, method) add_offense(node, message: message(method, inverse_methods[method])) do |corrector| correct_inverse_method(corrector, node) end end end
Also aliased as: on_csend
Private Instance Methods
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 180 def camel_case_constant?(node) node.const_type? && CAMEL_CASE.match?(node.source) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 122 def correct_inverse_block(corrector, node) method_call, method, block = inverse_block?(node) corrector.replace(method_call.loc.selector, inverse_blocks[method].to_s) correct_inverse_selector(block, corrector) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 113 def correct_inverse_method(corrector, node) method_call, _lhs, method, _rhs = inverse_candidate?(node) return unless method_call && method corrector.remove(not_to_receiver(node, method_call)) corrector.replace(method_call.loc.selector, inverse_methods[method].to_s) remove_end_parenthesis(corrector, node, method, method_call) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 129 def correct_inverse_selector(block, corrector) selector_loc = block.loc.selector selector = selector_loc.source if NEGATED_EQUALITY_METHODS.include?(selector.to_sym) selector[0] = '=' corrector.replace(selector_loc, selector) else if block.loc.dot range = dot_range(block.loc) corrector.remove(range) end corrector.remove(selector_loc) end end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 184 def dot_range(loc) range_between(loc.dot.begin_pos, loc.expression.end_pos) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 163 def end_parentheses(node, method_call) method_call.source_range.end.join(node.source_range.end) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 151 def inverse_blocks @inverse_blocks ||= cop_config['InverseBlocks'].merge(cop_config['InverseBlocks'].invert) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 146 def inverse_methods @inverse_methods ||= cop_config['InverseMethods'] .merge(cop_config['InverseMethods'].invert) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 194 def message(method, inverse) format(MSG, method: method, inverse: inverse) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 155 def negated?(node) node.parent.respond_to?(:method?) && node.parent.method?(:!) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 159 def not_to_receiver(node, method_call) node.loc.selector.begin.join(method_call.source_range.begin) end
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 175 def possible_class_hierarchy_check?(lhs, rhs, method) CLASS_COMPARISON_METHODS.include?(method) && (camel_case_constant?(lhs) || (rhs.size == 1 && camel_case_constant?(rhs.first))) end
When comparing classes, ‘!(Integer < Numeric)` is not the same as `Integer > Numeric`.
Source
# File lib/rubocop/cop/style/inverse_methods.rb, line 188 def remove_end_parenthesis(corrector, node, method, method_call) return unless EQUALITY_METHODS.include?(method) || method_call.parent.begin_type? corrector.remove(end_parentheses(node, method_call)) end