Sha256: 7bb4a04dfcca14b8221f680b348d98f59c90647206977f9b9b6f3a77087f0b74

Contents?: true

Size: 1.9 KB

Versions: 49

Compression:

Stored size: 1.9 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 < Cop
        include ConfigurableEnforcedStyle
        include NegativeConditional

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

          check_negative_conditional(node)
        end

        def autocorrect(node)
          ConditionCorrector.correct_negative_condition(node)
        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

49 entries across 30 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/negated_unless.rb
rubocop-0.84.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.83.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.82.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.81.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.80.1 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.80.0 lib/rubocop/cop/style/negated_unless.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/negated_unless.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/negated_unless.rb
rubocop-0.79.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.78.0 lib/rubocop/cop/style/negated_unless.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/negated_unless.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/negated_unless.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/negated_unless.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/negated_unless.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/negated_unless.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/negated_unless.rb
rubocop-0.77.0 lib/rubocop/cop/style/negated_unless.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/negated_unless.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/negated_unless.rb