Sha256: 8e26cb76d0476b8307621275ce158d89cff14ab7e90fa19cb67177fd53f6b198

Contents?: true

Size: 1.45 KB

Versions: 192

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Looks for `unless` expressions with `else` clauses.
      #
      # @example
      #   # bad
      #   unless foo_bar.nil?
      #     # do something...
      #   else
      #     # do a different thing...
      #   end
      #
      #   # good
      #   if foo_bar.present?
      #     # do something...
      #   else
      #     # do a different thing...
      #   end
      class UnlessElse < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Do not use `unless` with `else`. Rewrite these with the positive case first.'

        def on_if(node)
          return unless node.unless? && node.else?

          add_offense(node) do |corrector|
            body_range = range_between_condition_and_else(node, node.condition)
            else_range = range_between_else_and_end(node)

            next if part_of_ignored_node?(node)

            corrector.replace(node.loc.keyword, 'if')
            corrector.replace(body_range, else_range.source)
            corrector.replace(else_range, body_range.source)
          end

          ignore_node(node)
        end

        def range_between_condition_and_else(node, condition)
          range_between(condition.source_range.end_pos, node.loc.else.begin_pos)
        end

        def range_between_else_and_end(node)
          range_between(node.loc.else.end_pos, node.loc.end.begin_pos)
        end
      end
    end
  end
end

Version data entries

192 entries across 185 versions & 18 rubygems

Version Path
rubocop-1.71.0 lib/rubocop/cop/style/unless_else.rb
rubocop-1.70.0 lib/rubocop/cop/style/unless_else.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/unless_else.rb
rubocop-1.69.2 lib/rubocop/cop/style/unless_else.rb
rubocop-1.69.1 lib/rubocop/cop/style/unless_else.rb
rubocop-1.69.0 lib/rubocop/cop/style/unless_else.rb
rubocop-1.68.0 lib/rubocop/cop/style/unless_else.rb
rubocop-1.67.0 lib/rubocop/cop/style/unless_else.rb
rubocop-1.66.1 lib/rubocop/cop/style/unless_else.rb
rubocop-1.66.0 lib/rubocop/cop/style/unless_else.rb
rubocop-1.65.1 lib/rubocop/cop/style/unless_else.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/unless_else.rb
rubocop-1.65.0 lib/rubocop/cop/style/unless_else.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/unless_else.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/unless_else.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/unless_else.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/unless_else.rb
rubocop-1.64.1 lib/rubocop/cop/style/unless_else.rb
rubocop-1.63.4 lib/rubocop/cop/style/unless_else.rb
rubocop-1.63.3 lib/rubocop/cop/style/unless_else.rb