in lib/primer/view_components/linters/tag_tree_helpers.rb [15:54]
def build_tag_tree(processed_source)
nodes = processed_source.ast.children
tag_tree = {}
tags = []
current_opened_tag = nil
nodes.each do |node|
if node.type == :tag
tag = BetterHtml::Tree::Tag.from_node(node)
tags << tag
if tag.closing?
if current_opened_tag && tag.name == current_opened_tag.name
tag_tree[current_opened_tag][:closing] = tag
current_opened_tag = tag_tree[current_opened_tag][:parent]
end
next
end
self_closing = self_closing?(tag)
tag_tree[tag] = {
tag: tag,
closing: self_closing ? tag : nil,
parent: current_opened_tag,
children: []
}
tag_tree[current_opened_tag][:children] << tag_tree[tag] if current_opened_tag
current_opened_tag = tag unless self_closing
elsif current_opened_tag
tag_tree[current_opened_tag][:children] << node
end
end
[tags, tag_tree]
end