Sha256: 13da30ff5a676263a51f07181e2ac56b9db3ec27f9618066473dc8f65d62d5d7

Contents?: true

Size: 712 Bytes

Versions: 4

Compression:

Stored size: 712 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # TODO: Make configurable.
      # Checks for uses of if/then/else/end on a single line.
      class OneLineConditional < Cop
        include OnNormalIfUnless

        MSG = 'Favor the ternary operator (`?:`) ' \
              'over `%s/then/else/end` constructs.'.freeze

        def on_normal_if_unless(node)
          exp = node.source
          return if exp.include?("\n")
          return unless node.loc.respond_to?(:else) && node.loc.else
          condition = exp.include?('if') ? 'if' : 'unless'

          add_offense(node, :expression, format(MSG, condition))
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.37.2 lib/rubocop/cop/style/one_line_conditional.rb
rubocop-0.37.1 lib/rubocop/cop/style/one_line_conditional.rb
rubocop-0.37.0 lib/rubocop/cop/style/one_line_conditional.rb
rubocop-0.36.0 lib/rubocop/cop/style/one_line_conditional.rb