Sha256: ff5a3a0c6d0f6e60ad78c30b05c7b10c87fc83bce44ffe01efc7fe01a29c7881

Contents?: true

Size: 1.32 KB

Versions: 28

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for odd else block layout - like
      # having an expression on the same line as the else keyword,
      # which is usually a mistake.
      #
      # @example
      #
      #   # bad
      #
      #   if something
      #     # ...
      #   else do_this
      #     do_that
      #   end
      #
      # @example
      #
      #   # good
      #
      #   if something
      #     # ...
      #   else
      #     do_this
      #     do_that
      #   end
      class ElseLayout < Cop
        MSG = 'Odd `else` layout detected. Did you mean to use `elsif`?'.freeze

        def on_if(node)
          return if node.ternary? || node.elsif?

          check(node)
        end

        private

        def check(node)
          return unless node.else_branch

          if node.else? && node.loc.else.is?('else')
            check_else(node)
          elsif node.if?
            check(node.else_branch)
          end
        end

        def check_else(node)
          else_branch = node.else_branch

          return unless else_branch.begin_type?

          first_else = else_branch.children.first

          return unless first_else.source_range.line == node.loc.else.line

          add_offense(first_else)
        end
      end
    end
  end
end

Version data entries

28 entries across 26 versions & 3 rubygems

Version Path
rubocop-0.65.0 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.64.0 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.63.1 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.63.0 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.62.0 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.61.1 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.61.0 lib/rubocop/cop/lint/else_layout.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/else_layout.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/else_layout.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/else_layout.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/else_layout.rb
rubocop-0.60.0 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.59.2 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.59.1 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.59.0 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.58.2 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.58.1 lib/rubocop/cop/lint/else_layout.rb
rubocop-0.58.0 lib/rubocop/cop/lint/else_layout.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/lint/else_layout.rb
rubocop-0.57.2 lib/rubocop/cop/lint/else_layout.rb