Sha256: d1bc0595ff2830208dadd0fba45b5afcfa772650d7151a00ed8b3aa690c8099f

Contents?: true

Size: 1.45 KB

Versions: 48

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop checks whether the end statement of a do..end block
      # is on its own line.
      #
      # @example
      #   # bad
      #   blah do |i|
      #     foo(i) end
      #
      #   # good
      #   blah do |i|
      #     foo(i)
      #   end
      #
      #   # bad
      #   blah { |i|
      #     foo(i) }
      #
      #   # good
      #   blah { |i|
      #     foo(i)
      #   }
      class BlockEndNewline < Cop
        include Alignment

        MSG = 'Expression at %<line>d, %<column>d should be on its own line.'

        def on_block(node)
          return if node.single_line?

          # If the end is on its own line, there is no offense
          return if begins_its_line?(node.loc.end)

          add_offense(node, location: :end)
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(delimiter_range(node),
                              "\n#{node.loc.end.source}#{offset(node)}")
          end
        end

        private

        def message(node)
          format(MSG, line: node.loc.end.line, column: node.loc.end.column + 1)
        end

        def delimiter_range(node)
          Parser::Source::Range.new(
            node.loc.expression.source_buffer,
            node.children.compact.last.loc.expression.end_pos,
            node.loc.expression.end_pos
          )
        end
      end
    end
  end
end

Version data entries

48 entries across 48 versions & 5 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/block_end_newline.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/block_end_newline.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/block_end_newline.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/block_end_newline.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/block_end_newline.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.11.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.10.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.9.1 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.9.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.8.1 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.8.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.7.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.6.1 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.6.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.5.2 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.5.1 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.5.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.4.2 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.4.1 lib/rubocop/cop/layout/block_end_newline.rb