class RuboCop::Cop::Layout::EmptyLineAfterMagicComment

end
# Some code
class Person
# Some documentation for Person
# frozen_string_literal: true
# bad
end
# Some code
class Person
# Some documentation for Person
# frozen_string_literal: true
# good
@example
Checks for a newline after the final magic comment.

def autocorrect(token)

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

def investigate(source)

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 last_magic_comment(source)

Returns:
  • (nil) - otherwise
  • (Parser::Source::Comment) - if magic comments exist before code
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