class RuboCop::Cop::Style::Not
Checks for uses of the keyword ‘not` instead of `!`.
@example
# bad - parentheses are required because of op precedence x = (not something) # good x = !something
Constants
- MSG
- OPPOSITE_METHODS
- RESTRICT_ON_SEND
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/style/not.rb, line 32 def on_send(node) return unless node.prefix_not? add_offense(node.loc.selector) do |corrector| range = range_with_surrounding_space(node.loc.selector, side: :right) if opposite_method?(node.receiver) correct_opposite_method(corrector, range, node.receiver) elsif requires_parens?(node.receiver) correct_with_parens(corrector, range, node) else correct_without_parens(corrector, range) end end end
Private Instance Methods
correct_opposite_method(corrector, range, child)
click to toggle source
# File lib/rubocop/cop/style/not.rb, line 60 def correct_opposite_method(corrector, range, child) corrector.remove(range) corrector.replace(child.loc.selector, OPPOSITE_METHODS[child.method_name].to_s) end
correct_with_parens(corrector, range, node)
click to toggle source
# File lib/rubocop/cop/style/not.rb, line 65 def correct_with_parens(corrector, range, node) corrector.replace(range, '!(') corrector.insert_after(node, ')') end
correct_without_parens(corrector, range)
click to toggle source
# File lib/rubocop/cop/style/not.rb, line 70 def correct_without_parens(corrector, range) corrector.replace(range, '!') end
opposite_method?(child)
click to toggle source
# File lib/rubocop/cop/style/not.rb, line 50 def opposite_method?(child) child.send_type? && OPPOSITE_METHODS.key?(child.method_name) end
requires_parens?(child)
click to toggle source
# File lib/rubocop/cop/style/not.rb, line 54 def requires_parens?(child) child.and_type? || child.or_type? || (child.send_type? && child.binary_operation?) || (child.if_type? && child.ternary?) end