Sha256: f512b599f26adb6062380008cc1932a1434c55610a1add3e543e09c504479a4d

Contents?: true

Size: 1.81 KB

Versions: 14

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # Checks for a newline after the final magic comment.
      #
      # @example
      #   # good
      #   # frozen_string_literal: true
      #
      #   # Some documentation for Person
      #   class Person
      #     # Some code
      #   end
      #
      #   # bad
      #   # frozen_string_literal: true
      #   # Some documentation for Person
      #   class Person
      #     # Some code
      #   end
      class EmptyLineAfterMagicComment < Cop
        include RangeHelp

        MSG = 'Add an empty line after magic comments.'.freeze
        BLANK_LINE = /\A\s*\z/

        def investigate(source)
          return unless source.ast &&
                        (last_magic_comment = last_magic_comment(source))
          return if source[last_magic_comment.loc.line] =~ BLANK_LINE

          offending_range =
            source_range(source.buffer, last_magic_comment.loc.line + 1, 0)

          add_offense(offending_range, location: offending_range)
        end

        def autocorrect(token)
          lambda do |corrector|
            corrector.insert_before(token, "\n")
          end
        end

        private

        # Find the last magic comment in the source file.
        #
        # Take all comments that precede the first line of code, select the
        # magic comments, and return the last magic comment in the file.
        #
        # @return [Parser::Source::Comment] if magic comments exist before code
        # @return [nil] otherwise
        def last_magic_comment(source)
          source
            .comments
            .take_while { |comment| comment.loc.line < source.ast.loc.line }
            .select     { |comment| MagicComment.parse(comment.text).any?  }
            .last
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
rubocop-0.59.2 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.59.1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.59.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.58.2 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.58.1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.58.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.57.2 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.57.1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.57.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.56.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.55.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.54.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.53.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb