Sha256: b57bcd12729f60cd4cd26417056e3e730c57c92ad29707a77bb93a6e85e3e3a8

Contents?: true

Size: 1.87 KB

Versions: 13

Compression:

Stored size: 1.87 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop looks for trailing blank lines and a final newline in the
      # source code.
      class TrailingBlankLines < Cop
        include ConfigurableEnforcedStyle

        def investigate(processed_source)
          sb = processed_source.buffer
          return if sb.source.empty?

          whitespace_at_end = sb.source[/\s*\Z/]
          blank_lines = whitespace_at_end.count("\n") - 1
          wanted_blank_lines = style == :final_newline ? 0 : 1

          return unless blank_lines != wanted_blank_lines

          begin_pos = sb.source.length - whitespace_at_end.length
          autocorrect_range = Parser::Source::Range.new(sb, begin_pos,
                                                        sb.source.length)
          begin_pos += "\n".length unless whitespace_at_end.length == 0
          report_range = Parser::Source::Range.new(sb, begin_pos,
                                                   sb.source.length)
          add_offense(autocorrect_range, report_range,
                      message(wanted_blank_lines, blank_lines))
        end

        private

        def message(wanted_blank_lines, blank_lines)
          case blank_lines
          when -1
            'Final newline missing.'
          when 0
            'Trailing blank line missing.'
          else
            instead_of = if wanted_blank_lines == 0
                           ''
                         else
                           "instead of #{wanted_blank_lines} "
                         end
            format('%d trailing blank lines %sdetected.', blank_lines,
                   instead_of)
          end
        end

        def autocorrect(range)
          @corrections << lambda do |corrector|
            corrector.replace(range, style == :final_newline ? "\n" : "\n\n")
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/trailing_blank_lines.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.29.1 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.29.0 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.28.0 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.27.1 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.27.0 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.26.1 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.26.0 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.25.0 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.24.1 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.24.0 lib/rubocop/cop/style/trailing_blank_lines.rb
rubocop-0.23.0 lib/rubocop/cop/style/trailing_blank_lines.rb