Sha256: cd39c64dedb72d66864c123ac258525fe85545a8733b7d3b4ae541b24bdd65cc

Contents?: true

Size: 1.96 KB

Versions: 6897

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

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

        def on_if(node)
          return 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

6,897 entries across 6,891 versions & 26 rubygems

Version Path
ory-hydra-client-1.11.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha99 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha98 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha97 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha96 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha95 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha94 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha93 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha92 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-oathkeeper-client-0.38.20.beta1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha91 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha90 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha89 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha88 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha87 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha86 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha85 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha84 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha83 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb
ory-client-0.0.1.alpha82 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/negated_if.rb