class RubbyCop::Cop::Style::Not

This cop checks for uses if the keyword not instead of !.

Constants

MSG
OPPOSITE_METHODS

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubbycop/cop/style/not.rb, line 19
def on_send(node)
  return unless node.keyword_not?

  add_offense(node, :selector)
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubbycop/cop/style/not.rb, line 27
def autocorrect(node)
  range = range_with_surrounding_space(node.loc.selector, :right)

  if opposite_method?(node.receiver)
    correct_opposite_method(range, node.receiver)
  elsif requires_parens?(node.receiver)
    correct_with_parens(range, node)
  else
    correct_without_parens(range)
  end
end
correct_opposite_method(range, child) click to toggle source
# File lib/rubbycop/cop/style/not.rb, line 48
def correct_opposite_method(range, child)
  lambda do |corrector|
    corrector.remove(range)
    corrector.replace(child.loc.selector,
                      OPPOSITE_METHODS[child.method_name].to_s)
  end
end
correct_with_parens(range, node) click to toggle source
# File lib/rubbycop/cop/style/not.rb, line 56
def correct_with_parens(range, node)
  lambda do |corrector|
    corrector.replace(range, '!(')
    corrector.insert_after(node.source_range, ')')
  end
end
correct_without_parens(range) click to toggle source
# File lib/rubbycop/cop/style/not.rb, line 63
def correct_without_parens(range)
  ->(corrector) { corrector.replace(range, '!') }
end
opposite_method?(child) click to toggle source
# File lib/rubbycop/cop/style/not.rb, line 39
def opposite_method?(child)
  child.send_type? && OPPOSITE_METHODS.key?(child.method_name)
end
requires_parens?(child) click to toggle source
# File lib/rubbycop/cop/style/not.rb, line 43
def requires_parens?(child)
  child.and_type? || child.or_type? || child.binary_operation? ||
    child.if_type? && child.ternary?
end