Sha256: 95f53878fa632a3c8a2b48844bae926998c2228477540ad3be3656da7ffb28ee

Contents?: true

Size: 1.76 KB

Versions: 33

Compression:

Stored size: 1.76 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.'

        def investigate(source)
          return unless source.ast &&
                        (last_magic_comment = last_magic_comment(source))
          return if source[last_magic_comment.loc.line].strip.empty?

          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 }
            .reverse
            .find { |comment| MagicComment.parse(comment.text).any? }
        end
      end
    end
  end
end

Version data entries

33 entries across 29 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.89.1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.89.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.88.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.87.1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.87.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.86.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.85.1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.85.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.84.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.83.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.82.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.81.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.80.1 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
rubocop-0.80.0 lib/rubocop/cop/layout/empty_line_after_magic_comment.rb