Module: Spoom::RBS::ExtractRBSComments

Instance Method Summary collapse

Instance Method Details

#node_rbs_comments(node) ⇒ Object

: (Prism::Node) -> Comments



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/spoom/rbs.rb', line 70

def node_rbs_comments(node)
  res = Comments.new

  comments = node.location.leading_comments.reverse
  return res if comments.empty?

  continuation_comments = [] #: Array[Prism::Comment]

  last_line = node.location.start_line

  comments.each do |comment|
    string = comment.slice

    comment_line = comment.location.end_line

    break if comment_line < last_line - 1

    last_line = comment_line

    if string.start_with?("# @")
      string = string.delete_prefix("#").strip
      res.annotations << Annotation.new(string, comment.location)
    elsif string.start_with?("#: ")
      string = string.delete_prefix("#:").strip
      location = comment.location

      continuation_comments.reverse_each do |continuation_comment|
        string = "#{string}#{continuation_comment.slice.delete_prefix("#|")}"
        location = location.join(continuation_comment.location)
      end
      continuation_comments.clear
      res.signatures << Signature.new(string, location)
    elsif string.start_with?("#|")
      continuation_comments << comment
    end
  end

  res
end