module HamlLint::HamlVisitor

Provides an interface which when included allows a class to visit nodes in the parse tree of a HAML document.

Public Instance Methods

visit(node) click to toggle source
# File lib/haml_lint/haml_visitor.rb, line 7
def visit(node)
  # Keep track of whether this block was consumed by the visitor. This
  # allows us to visit all nodes by default, but can override the behavior
  # by specifying `yield false` in a visit method, indicating that no
  # further visiting should occur for the current node's children.
  block_called = false

  block = ->(descend = :children) do
    block_called = true
    visit_children(node) if descend == :children
  end

  disabled = node.disabled?(self)

  safe_send("visit_#{node_name(node)}", node, &block) unless disabled

  # Visit all children by default unless the block was invoked (indicating
  # the user intends to not recurse further, or wanted full control over
  # when the children were visited).
  visit_children(node) unless block_called

  safe_send("after_visit_#{node_name(node)}", node, &block) unless disabled
end
visit_children(parent) click to toggle source
# File lib/haml_lint/haml_visitor.rb, line 31
def visit_children(parent)
  parent.children.each { |node| visit(node) }
end

Private Instance Methods

node_name(node) click to toggle source
# File lib/haml_lint/haml_visitor.rb, line 37
def node_name(node)
  node.type
end
safe_send(name, *args, &block) click to toggle source
# File lib/haml_lint/haml_visitor.rb, line 41
def safe_send(name, *args, &block)
  send(name, *args, &block) if respond_to?(name, true)
end