class RuboCop::Cop::Layout::EmptyLineAfterMagicComment

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

Constants

MSG

Public Instance Methods

on_new_investigation() click to toggle source
# File lib/rubocop/cop/layout/empty_line_after_magic_comment.rb, line 29
def on_new_investigation
  return unless processed_source.ast &&
                (last_magic_comment = last_magic_comment(processed_source))
  return if processed_source[last_magic_comment.loc.line].strip.empty?

  offending_range = offending_range(last_magic_comment)

  add_offense(offending_range) do |corrector|
    corrector.insert_before(offending_range, "\n")
  end
end

Private Instance Methods

last_magic_comment(source) click to toggle source

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

# File lib/rubocop/cop/layout/empty_line_after_magic_comment.rb, line 54
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
offending_range(last_magic_comment) click to toggle source
# File lib/rubocop/cop/layout/empty_line_after_magic_comment.rb, line 43
def offending_range(last_magic_comment)
  source_range(processed_source.buffer, last_magic_comment.loc.line + 1, 0)
end