Module: HamlLint::HamlVisitor

Included in:
Linter, RubyExtractor
Defined in:
lib/haml_lint/haml_visitor.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#visit(node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



31
32
33
# File 'lib/haml_lint/haml_visitor.rb', line 31

def visit_children(parent)
  parent.children.each { |node| visit(node) }
end