Sha256: ba592b19a70b97cb05f7af07431e86491f247ab08d1fc27807f0ebf18176b57f

Contents?: true

Size: 1.75 KB

Versions: 35

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop checks for two or more consecutive blank lines.
      #
      # @example
      #
      #   # bad - It has two empty lines.
      #   some_method
      #   # one empty line
      #   # two empty lines
      #   some_method
      #
      #   # good
      #   some_method
      #   # one empty line
      #   some_method
      #
      class EmptyLines < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Extra blank line detected.'
        LINE_OFFSET = 2

        def on_new_investigation
          return if processed_source.tokens.empty?

          lines = Set.new
          processed_source.each_token do |token|
            lines << token.line
          end

          each_extra_empty_line(lines.sort) do |range|
            add_offense(range) do |corrector|
              corrector.remove(range)
            end
          end
        end

        private

        def each_extra_empty_line(lines)
          prev_line = 1

          lines.each do |cur_line|
            if exceeds_line_offset?(cur_line - prev_line)
              # we need to be wary of comments since they
              # don't show up in the tokens
              ((prev_line + 1)...cur_line).each do |line|
                next unless previous_and_current_lines_empty?(line)

                yield source_range(processed_source.buffer, line, 0)
              end
            end

            prev_line = cur_line
          end
        end

        def exceeds_line_offset?(line_diff)
          line_diff > LINE_OFFSET
        end

        def previous_and_current_lines_empty?(line)
          processed_source[line - 2].empty? && processed_source[line - 1].empty?
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 3 rubygems

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