Sha256: 7b92d00b998b7823ddce7eb701ed10f984c1c8ceff68969722730f80d2ee7cad

Contents?: true

Size: 1.86 KB

Versions: 151

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # 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?
          # Quick check if we possibly have consecutive blank lines.
          return unless processed_source.raw_source.include?("\n\n\n")

          lines = Set.new
          processed_source.tokens.each { |token| lines << token.line }

          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

151 entries across 150 versions & 15 rubygems

Version Path
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.70 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/empty_lines.rb