Module: HamlLint::HamlVisitor

Included in:
Linter, ScriptExtractor
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


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/haml_lint/haml_visitor.rb', line 5

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

  method = "visit_#{node_name(node)}"
  send(method, node, &block) if respond_to?(method, true)

  # 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
end

#visit_children(parent) ⇒ Object


26
27
28
# File 'lib/haml_lint/haml_visitor.rb', line 26

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