Sha256: 59b4140586e350e6ea7f64985a1b83470f7d39193930abc33cd9f454fad92e5b

Contents?: true

Size: 1.96 KB

Versions: 209

Compression:

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

209 entries across 200 versions & 19 rubygems

Version Path
harbr-0.1.64 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.63 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.62 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.61 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.60 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.59 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.58 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.57 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.56 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.55 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.54 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.53 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.52 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.50 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.49 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.48 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.47 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.46 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.45 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb
harbr-0.1.44 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/negated_unless.rb