Sha256: 4e673a5ca4fcd1d59c4df1f4a2d3c5c166d4e7858e05f4e5e7e199f7c0953fd8

Contents?: true

Size: 1.89 KB

Versions: 13

Compression:

Stored size: 1.89 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

13 entries across 13 versions & 3 rubygems

Version Path
rubocop-0.89.1 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.89.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.88.0 lib/rubocop/cop/style/negated_unless.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.87.1 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.87.0 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.86.0 lib/rubocop/cop/style/negated_unless.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/negated_unless.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/negated_unless.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.85.1 lib/rubocop/cop/style/negated_unless.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/negated_unless.rb
rubocop-0.85.0 lib/rubocop/cop/style/negated_unless.rb