Module: Spoom::RBS::ExtractRBSComments

Instance Method Summary collapse

Instance Method Details

#node_rbs_comments(node) ⇒ Object

: (Prism::Node) -> Comments



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/spoom/rbs.rb', line 90

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_locations = [] #: Array[Prism::Location]

      continuation_comments.reverse_each do |continuation_comment|
        string = "#{string}#{continuation_comment.slice.delete_prefix("#|")}"
        location = location.join(continuation_comment.location)
        continuation_locations << continuation_comment.location
      end
      continuation_comments.clear

      next if string.start_with?("type ")

      res.signatures.prepend(Signature.new(string, location, continuation_locations:))
    elsif string.start_with?("#|")
      continuation_comments << comment
    end
  end

  res
end