Sha256: e4640ae49cec36f1404b40b8c87faac7b8c91b6548ba341b7ed42ac71028ded9

Contents?: true

Size: 1.36 KB

Versions: 38

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for nested ternary op expressions.
      #
      # @example
      #   # bad
      #   a ? (b ? b1 : b2) : a2
      #
      #   # good
      #   if a
      #     b ? b1 : b2
      #   else
      #     a2
      #   end
      class NestedTernaryOperator < Base
        extend AutoCorrector

        MSG = 'Ternary operators must not be nested. Prefer `if` or `else` constructs instead.'

        def on_if(node)
          return unless node.ternary?

          node.each_descendant(:if).select(&:ternary?).each do |nested_ternary|
            add_offense(nested_ternary) do |corrector|
              if_node = if_node(nested_ternary)

              corrector.replace(if_node, <<~RUBY.chop)
                if #{if_node.condition.source}
                  #{remove_parentheses(if_node.if_branch.source)}
                else
                  #{if_node.else_branch.source}
                end
              RUBY
            end
          end
        end

        private

        def if_node(node)
          node = node.parent
          return node if node.if_type?

          if_node(node)
        end

        def remove_parentheses(source)
          return source unless source.start_with?('(')

          source.delete_prefix('(').delete_suffix(')')
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/nested_ternary_operator.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.29.1 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.29.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.28.2 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.28.1 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.28.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.27.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.26.1 lib/rubocop/cop/style/nested_ternary_operator.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.26.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.25.1 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.25.0 lib/rubocop/cop/style/nested_ternary_operator.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.24.1 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.24.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.23.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.22.3 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.22.2 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.22.1 lib/rubocop/cop/style/nested_ternary_operator.rb