Sha256: bf8fa3601f55c8467d7549ec88a643a3cbbe9ad3399873666fe0edee77128cbb

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

##
# Collect all global comments or comments attached to methods in a class.
# At this point we don't care whether these are Rage OpenAPI comments or not.
#
class Rage::OpenAPI::Collector < Prism::Visitor
  def initialize(comments)
    @comments = comments.dup
    @method_comments = {}
  end

  def dangling_comments
    @comments
  end

  def method_comments(method_name)
    @method_comments[method_name.to_s]
  end

  def visit_def_node(node)
    method_comments = []
    start_line = node.location.start_line - 1

    loop do
      comment_i = @comments.find_index { |comment| comment.location.start_line == start_line }
      if comment_i
        comment = @comments.delete_at(comment_i)
        method_comments << comment
        start_line -= 1
      end

      break unless comment
    end

    @method_comments[node.name.to_s] = method_comments.reverse

    # reject comments inside methods
    @comments.reject! do |comment|
      comment.location.start_line >= node.location.start_line && comment.location.start_line <= node.location.end_line
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rage-rb-1.11.0 lib/rage/openapi/collector.rb