Sha256: cd996963a3d2044901b0f84ae92e80bde0c9edf62fdafd1cea83f613d4e0a0de

Contents?: true

Size: 1.5 KB

Versions: 15

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'set'

module RuboCop
  module Cop
    module Style
      # This cops checks for two or more consecutive blank lines.
      class EmptyLines < Cop
        MSG = 'Extra blank line detected.'.freeze
        LINE_OFFSET = 2

        def investigate(processed_source)
          return if processed_source.tokens.empty?

          lines = Set.new
          processed_source.tokens.each do |token|
            lines << token.pos.line
          end

          each_extra_empty_line(lines.sort) do |range|
            add_offense(range, range)
          end
        end

        def autocorrect(range)
          ->(corrector) { corrector.remove(range) }
        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

15 entries across 15 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/empty_lines.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/empty_lines.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/empty_lines.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/empty_lines.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/empty_lines.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/empty_lines.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/empty_lines.rb
rubocop-0.48.1 lib/rubocop/cop/style/empty_lines.rb
rubocop-0.48.0 lib/rubocop/cop/style/empty_lines.rb
rubocop-0.47.1 lib/rubocop/cop/style/empty_lines.rb
rubocop-0.47.0 lib/rubocop/cop/style/empty_lines.rb
rubocop-0.46.0 lib/rubocop/cop/style/empty_lines.rb
rubocop-0.45.0 lib/rubocop/cop/style/empty_lines.rb
rubocop-0.44.1 lib/rubocop/cop/style/empty_lines.rb
rubocop-0.44.0 lib/rubocop/cop/style/empty_lines.rb