class RDocRuboCop::Lang::Ruby::CommentExtractor

Attributes

comments[R]

Public Class Methods

new(source_file) click to toggle source
# File lib/rdoc_rubocop/lang/ruby/comment_extractor.rb, line 11
def initialize(source_file)
  @source_file = source_file
  @comments = []
end

Public Instance Methods

extract() click to toggle source
# File lib/rdoc_rubocop/lang/ruby/comment_extractor.rb, line 16
def extract
  @comments = extract_comments
end

Private Instance Methods

extract_comments() click to toggle source
# File lib/rdoc_rubocop/lang/ruby/comment_extractor.rb, line 22
def extract_comments
  chunk = []
  comments = []

  tokens.each do |tokens_in_line|
    token = tokens_in_line.pop

    if token.comment?
      if tokens_in_line.all?(&:sp?)
        chunk << token
      else
        comments << Comment.new(chunk, @source_file) if chunk.any?
        chunk = []
      end
    else
      if chunk.any?
        comments << Comment.new(chunk, @source_file)
        chunk = []
      end
    end
  end
  comments << Comment.new(chunk, @source_file) if chunk.any?

  comments
end
tokens() click to toggle source
# File lib/rdoc_rubocop/lang/ruby/comment_extractor.rb, line 48
def tokens
  Ripper.
    lex(@source_file.source).
    map { |token| Token.build(*token) }.
    slice_when { |token_before, token_after| token_before.lineno != token_after.lineno }
end