Sha256: 860cc178492161cd5d7b8276a6e40b5a04261f95427c0722f1af92c35b58832e

Contents?: true

Size: 1.64 KB

Versions: 115

Compression:

Stored size: 1.64 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|
              next if part_of_ignored_node?(node)

              autocorrect(corrector, node)
              ignore_node(node)
            end
          end
        end

        private

        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

115 entries across 115 versions & 9 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.68.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.67.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.66.1 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.66.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.65.1 lib/rubocop/cop/style/nested_ternary_operator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.65.0 lib/rubocop/cop/style/nested_ternary_operator.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.64.1 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.63.4 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.63.3 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.63.2 lib/rubocop/cop/style/nested_ternary_operator.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.63.1 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.63.0 lib/rubocop/cop/style/nested_ternary_operator.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.62.1 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.62.0 lib/rubocop/cop/style/nested_ternary_operator.rb
rubocop-1.61.0 lib/rubocop/cop/style/nested_ternary_operator.rb