Sha256: 3fede66e0efa7e344124840165c32e9daee9998dc1fb6e907fe554dd9462257c

Contents?: true

Size: 1.97 KB

Versions: 35

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for uses of unless with a negated condition. Only unless
      # without else are considered. There are three different styles:
      #
      # * both
      # * prefix
      # * postfix
      #
      # @example EnforcedStyle: both (default)
      #   # enforces `if` for `prefix` and `postfix` conditionals
      #
      #   # bad
      #   unless !foo
      #     bar
      #   end
      #
      #   # good
      #   if foo
      #     bar
      #   end
      #
      #   # bad
      #   bar unless !foo
      #
      #   # good
      #   bar if foo
      #
      # @example EnforcedStyle: prefix
      #   # enforces `if` for just `prefix` conditionals
      #
      #   # bad
      #   unless !foo
      #     bar
      #   end
      #
      #   # good
      #   if foo
      #     bar
      #   end
      #
      #   # good
      #   bar unless !foo
      #
      # @example EnforcedStyle: postfix
      #   # enforces `if` for just `postfix` conditionals
      #
      #   # bad
      #   bar unless !foo
      #
      #   # good
      #   bar if foo
      #
      #   # good
      #   unless !foo
      #     bar
      #   end
      class NegatedUnless < Base
        include ConfigurableEnforcedStyle
        include NegativeConditional
        extend AutoCorrector

        def on_if(node)
          return if node.if? || node.elsif? || node.ternary?
          return if correct_style?(node)

          message = message(node)
          check_negative_conditional(node, message: message) do |corrector|
            ConditionCorrector.correct_negative_condition(corrector, node)
          end
        end

        private

        def message(node)
          format(MSG, inverse: node.inverse_keyword, current: node.keyword)
        end

        def correct_style?(node)
          style == :prefix && node.modifier_form? ||
            style == :postfix && !node.modifier_form?
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/negated_unless.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/negated_unless.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/negated_unless.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/negated_unless.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/negated_unless.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/negated_unless.rb
rubocop-1.12.1 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.12.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.11.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.10.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.9.1 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.9.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.8.1 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.8.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.7.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.6.1 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.6.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.5.2 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.5.1 lib/rubocop/cop/style/negated_unless.rb
rubocop-1.5.0 lib/rubocop/cop/style/negated_unless.rb