Sha256: 6ae5da4be21867091848abfc0d720f36a579a5d2071f50d29a28e6096c8a50ad

Contents?: true

Size: 1.41 KB

Versions: 40

Compression:

Stored size: 1.41 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 < Base
        include Alignment
        extend AutoCorrector

        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.loc.end, message: message(node)) 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

40 entries across 40 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/block_end_newline.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.29.1 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.29.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.28.2 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.28.1 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.28.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.27.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.26.1 lib/rubocop/cop/layout/block_end_newline.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.26.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.25.1 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.25.0 lib/rubocop/cop/layout/block_end_newline.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.24.1 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.24.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.23.0 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.22.3 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.22.2 lib/rubocop/cop/layout/block_end_newline.rb
rubocop-1.22.1 lib/rubocop/cop/layout/block_end_newline.rb