Sha256: 6a60d1363660d5db9b962028e6bcf6689685cb18c47b1834efddc72f3f87d0e9

Contents?: true

Size: 1.42 KB

Versions: 12

Compression:

Stored size: 1.42 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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
        MSG = 'Expression at %d, %d should be on its own line.'.freeze

        def on_block(node)
          end_loc = node.loc.end
          do_loc = node.loc.begin # Actually it's either do or {.
          return if do_loc.line == end_loc.line # Ignore one-liners.

          # If the end is on its own line, there is no offense
          return if end_loc.source_line =~ /^\s*#{end_loc.source}/

          msg = format(MSG, end_loc.line, end_loc.column + 1)
          add_offense(node, end_loc, msg)
        end

        def autocorrect(node)
          lambda do |corrector|
            indentation = indentation_of_block_start_line(node)
            corrector.insert_before(node.loc.end, "\n" + (' ' * indentation))
          end
        end

        def indentation_of_block_start_line(node)
          match = /\S.*/.match(node.loc.begin.source_line)
          match.begin(0)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/block_end_newline.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.43.0 lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.42.0 lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.41.2 lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.41.1 lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.41.0 lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.40.0 lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.39.0 lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.38.0 lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.37.2 lib/rubocop/cop/style/block_end_newline.rb
rubocop-0.37.1 lib/rubocop/cop/style/block_end_newline.rb