Sha256: 3f616e009bfd3ebad9736d894f84b6802ffd5b822492a0081b6191bc78756ece

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cops checks redundant empty lines around the bodies of classes,
      # modules & methods.
      #
      # @example
      #
      #   class Test
      #
      #      def something
      #        ...
      #      end
      #
      #   end
      #
      #   def something(arg)
      #
      #     ...
      #   end
      #
      class EmptyLinesAroundBody < Cop
        include CheckMethods

        MSG_BEG = 'Extra empty line detected at body beginning.'
        MSG_END = 'Extra empty line detected at body end.'

        def on_class(node)
          check(node)
        end

        def on_module(node)
          check(node)
        end

        def on_sclass(node)
          check(node)
        end

        def autocorrect(range)
          @corrections << ->(corrector) { corrector.remove(range) }
        end

        private

        def check(node, *_)
          start_line = node.loc.keyword.line
          end_line = node.loc.end.line

          return if start_line == end_line

          check_source(start_line, end_line)
        end

        def check_source(start_line, end_line)
          check_line(start_line, MSG_BEG)
          check_line(end_line - 2, MSG_END) unless end_line - 2 == start_line
        end

        def check_line(line, msg)
          if processed_source.lines[line].empty?
            range = source_range(processed_source.buffer,
                                 processed_source[0...line],
                                 0,
                                 1)
            add_offense(range, range, msg)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.21.0 lib/rubocop/cop/style/empty_lines_around_body.rb
rubocop-0.20.1 lib/rubocop/cop/style/empty_lines_around_body.rb
rubocop-0.20.0 lib/rubocop/cop/style/empty_lines_around_body.rb
rubocop-0.19.1 lib/rubocop/cop/style/empty_lines_around_body.rb
rubocop-0.19.0 lib/rubocop/cop/style/empty_lines_around_body.rb