Sha256: 876894d9992107df0b30534a9409aa7079642aa96ad3ead7f0d8cd6494a78df2

Contents?: true

Size: 1008 Bytes

Versions: 11

Compression:

Stored size: 1008 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for if and unless statements used as modifers of other if or
      # unless statements.
      #
      # @example
      #
      #  # bad
      #  tired? ? 'stop' : 'go faster' if running?
      #
      #  # bad
      #  if tired?
      #    "please stop"
      #  else
      #    "keep going"
      #  end if running?
      #
      #  # good
      #  if running?
      #    tired? ? 'stop' : 'go faster'
      #  end
      class IfUnlessModifierOfIfUnless < Cop
        include StatementModifier

        MESSAGE = 'Avoid modifier `%s` after another conditional.'.freeze

        def message(keyword)
          format(MESSAGE, keyword)
        end

        def on_if(node)
          return unless modifier_if?(node)
          _cond, body, _else = if_node_parts(node)
          return unless body.if_type?

          add_offense(node, :keyword, message(node.loc.keyword.source))
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
rubocop-0.46.0 lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
rubocop-0.45.0 lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
rubocop-0.44.1 lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
rubocop-0.44.0 lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb