Ruby 3.4 リファレンスマニュアル

instance method Prism::ParseResult#attach_comments!

attach_comments! -> ArrayRuby 3.3 から[permalink][rdoc][edit]

Prism::ParseResult#comments の各コメントを、前後の位置関係から構文木の各ノードの位置情報(Prism::Location)に関連付けます。関連付けた結果は Prism::Location#leading_commentsPrism::Location#trailing_commentsPrism::Location#comments で参照できます。

戻り値は Prism::ParseResult#comments と同じ配列です。

require "prism"

result = Prism.parse("# leading\na = 1\n")
result.attach_comments!
loc = result.value.statements.body[0].location
p loc.comments.map { |c| c.location.slice } # => ["# leading"]