Sha256: b4e32b6f902f48b0b92c5d8b8709cb86d4f7f3bda896e54df0523382cccb3237

Contents?: true

Size: 1.99 KB

Versions: 185

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for uses of the keyword `not` instead of `!`.
      #
      # @example
      #
      #   # bad - parentheses are required because of op precedence
      #   x = (not something)
      #
      #   # good
      #   x = !something
      #
      class Not < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Use `!` instead of `not`.'
        RESTRICT_ON_SEND = %i[!].freeze

        OPPOSITE_METHODS = {
          :== => :!=,
          :!= => :==,
          :<= => :>,
          :> => :<=,
          :< => :>=,
          :>= => :<
        }.freeze

        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

        def opposite_method?(child)
          child.send_type? && OPPOSITE_METHODS.key?(child.method_name)
        end

        def requires_parens?(child)
          child.and_type? || child.or_type? ||
            (child.send_type? && child.binary_operation?) ||
            (child.if_type? && child.ternary?)
        end

        def correct_opposite_method(corrector, range, child)
          corrector.remove(range)
          corrector.replace(child.loc.selector, OPPOSITE_METHODS[child.method_name].to_s)
        end

        def correct_with_parens(corrector, range, node)
          corrector.replace(range, '!(')
          corrector.insert_after(node, ')')
        end

        def correct_without_parens(corrector, range)
          corrector.replace(range, '!')
        end
      end
    end
  end
end

Version data entries

185 entries across 178 versions & 18 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/not.rb
rubocop-1.68.0 lib/rubocop/cop/style/not.rb
rubocop-1.67.0 lib/rubocop/cop/style/not.rb
rubocop-1.66.1 lib/rubocop/cop/style/not.rb
rubocop-1.66.0 lib/rubocop/cop/style/not.rb
rubocop-1.65.1 lib/rubocop/cop/style/not.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/not.rb
rubocop-1.65.0 lib/rubocop/cop/style/not.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/not.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/not.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/not.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/not.rb
rubocop-1.64.1 lib/rubocop/cop/style/not.rb
rubocop-1.63.4 lib/rubocop/cop/style/not.rb
rubocop-1.63.3 lib/rubocop/cop/style/not.rb
rubocop-1.63.2 lib/rubocop/cop/style/not.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/not.rb
rubocop-1.63.1 lib/rubocop/cop/style/not.rb
rubocop-1.63.0 lib/rubocop/cop/style/not.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/not.rb