Method: Parser::Source::Comment::Associator#associate

Defined in:
lib/parser/source/comment/associator.rb

#associateHash<Parser::AST::Node, Array<Parser::Source::Comment>>

Deprecated.

Compute a mapping between AST nodes and comments. Comment is associated with the node, if it is one of the following types:

  • preceding comment, it ends before the node start
  • sparse comment, it is located inside the node, after all child nodes
  • decorating comment, it starts at the same line, where the node ends

This rule is unambiguous and produces the result one could reasonably expect; for example, this code

# foo
hoge # bar
  + fuga

will result in the following association:

{
  (send (lvar :hoge) :+ (lvar :fuga)) =>
    [#<Parser::Source::Comment (string):2:1 "# foo">],
  (lvar :fuga) =>
    [#<Parser::Source::Comment (string):3:8 "# bar">]
}

Note that comments after the end of the end of a passed tree range are ignored (except root decorating comment).

Note that #associate produces unexpected result for nodes which are equal but have distinct locations; comments for these nodes are merged. You may prefer using #associate_by_identity or #associate_locations.

Returns:



92
93
94
95
# File 'lib/parser/source/comment/associator.rb', line 92

def associate
  @map_using = :eql
  do_associate
end