Sha256: 4446e46ebe43cc67c7098f279046afe73068ec6db5971eb74bcd3bb025c1d082

Contents?: true

Size: 1.83 KB

Versions: 70

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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
        include RangeHelp
        include IgnoredNode

        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)
              next if part_of_ignored_node?(if_node)

              autocorrect(corrector, if_node)
              ignore_node(if_node)
            end
          end
        end

        private

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

          if_node(node)
        end

        def autocorrect(corrector, if_node)
          replace_loc_and_whitespace(corrector, if_node.loc.question, "\n")
          replace_loc_and_whitespace(corrector, if_node.loc.colon, "\nelse\n")
          corrector.replace(if_node.if_branch, remove_parentheses(if_node.if_branch.source))
          corrector.wrap(if_node, 'if ', "\nend")
        end

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

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

        def replace_loc_and_whitespace(corrector, range, replacement)
          corrector.replace(
            range_with_surrounding_space(range: range, whitespace: true),
            replacement
          )
        end
      end
    end
  end
end

Version data entries

70 entries across 63 versions & 11 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/nested_ternary_operator.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/nested_ternary_operator.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/nested_ternary_operator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/nested_ternary_operator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/nested_ternary_operator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/nested_ternary_operator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.56.4 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.56.3 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.56.2 lib/rubocop/cop/style/nested_ternary_operator.rb
synctera_ruby_sdk-1.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/nested_ternary_operator.rb
synctera_ruby_sdk-1.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/nested_ternary_operator.rb
synctera_ruby_sdk-1.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/nested_ternary_operator.rb
sampero-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.1/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.56.1 lib/rubocop/cop/style/nested_ternary_operator.rb
tursodb-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/nested_ternary_operator.rb
synctera_ruby_sdk-1.0.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.56.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.55.1 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.55.0 lib/rubocop/cop/style/nested_ternary_operator.rb