instance method
Prism::ParseResult::Comments#attach!
attach!
lib/prism/parse_result/comments.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/prism/parse_result/comments.rb, line 93
def attach!
parse_result.comments.each do |comment|
preceding, enclosing, following = nearest_targets(parse_result.value, comment)
if comment.trailing?
if preceding
preceding.trailing_comment(comment)
else
(following || enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
end
else
# If a comment exists on its own line, prefer a leading comment.
if following
following.leading_comment(comment)
elsif preceding
preceding.trailing_comment(comment)
else
(enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
end
end
end
end
Attach the comments to their respective locations in the tree by mutating the parse result.