Class: SyntaxTree::Haml::MutationVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/haml/mutation_visitor.rb

Direct Known Subclasses

Tailwindcss::HamlMutationVisitor

Instance Method Summary collapse

Instance Method Details

#copy_and_visit_children(node) ⇒ Object Also known as: visit_comment, visit_doctype, visit_filter, visit_haml_comment, visit_plain, visit_root, visit_script, visit_silent_script, visit_tag



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/syntax_tree/haml/mutation_visitor.rb', line 15

def copy_and_visit_children(node)
  # Haml::Parser::ParseNode has these main attributes
  # - value: nil | Hash
  # - children: [ParseNode]
  # - parent: ParseNode
  node.dup.tap do |clone|
    clone.value = node.value.dup unless node.value.nil?
    clone.children =
      node.children.map do |child|
        visit(child).tap { |child_copy| child_copy.parent = clone }
      end
  end
end

#visit(node) ⇒ Object

A base Haml Visitor class that returns a deep copy of the AST. It can be subclassed to mutate some parts of the AST and keep everything else as it was.

Ideally this class (or equivalent) would existing within SyntaxTree::Haml gem.



11
12
13
# File 'lib/syntax_tree/haml/mutation_visitor.rb', line 11

def visit(node)
  node&.accept(self)
end