class RuboCop::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/rubocop/cop/style/not.rb, line 18
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/rubocop/cop/style/not.rb, line 26
def autocorrect(node)
  range = range_with_surrounding_space(node.loc.selector, :right)
  child = node.children.first

  if child.send_type? && OPPOSITE_METHODS.key?(child.method_name)
    lambda do |corrector|
      corrector.remove(range)
      corrector.replace(child.loc.selector,
                        OPPOSITE_METHODS[child.method_name].to_s)
    end
  elsif child.and_type? || child.or_type? || child.binary_operation? ||
        ternary_op?(child)
    lambda do |corrector|
      corrector.replace(range, '!(')
      corrector.insert_after(node.source_range, ')')
    end
  else
    ->(corrector) { corrector.replace(range, '!') }
  end
end