Module: Yoda::AST::Vnode::CommentAssociation

Included in:
Yoda::AST::Vnode
Defined in:
lib/yoda/ast/vnode.rb

Instance Method Summary collapse

Instance Method Details

#all_nodes_lazyEnumerable<Vnode>

Returns:



96
97
98
# File 'lib/yoda/ast/vnode.rb', line 96

def all_nodes_lazy
  [self, *children].lazy.flat_map { |el| self == el ? self : el.all_nodes_lazy }
end

#associate_comments(comments) ⇒ Hash{Vnode => Array<Parser::Source::Comment>}

Parameters:

  • comments (Array<::Parser::Source::Comment>)

Returns:

  • (Hash{Vnode => Array<Parser::Source::Comment>})


79
80
81
82
83
84
85
86
87
# File 'lib/yoda/ast/vnode.rb', line 79

def associate_comments(comments)
  if node = try(:node)
    Parser::Source::Comment.associate(node, comments).map do |ast_key_node, comments|
      [all_nodes_lazy.find { |node| node.wrapping?(ast_key_node) }, comments]
    end.to_h
  else
    {}
  end
end

#commentsObject

Parameters:

  • comment_by_node (Array<Parser::Source::Comment>)


101
102
103
# File 'lib/yoda/ast/vnode.rb', line 101

def comments
  @comments ||= comments_by_node[node] || []
end

#wrapping?(node) ⇒ boolean

Parameters:

  • node (::AST::Node)

Returns:

  • (boolean)


91
92
93
# File 'lib/yoda/ast/vnode.rb', line 91

def wrapping?(node)
  try(:node) && try(:node) == node
end