Module: SlimLint::SexpVisitor

Included in:
Linter, RubyExtractor
Defined in:
lib/slim_lint/sexp_visitor.rb

Overview

Provides an interface which when included allows a class to visit nodes in the Sexp of a Slim document.

Defined Under Namespace

Modules: DSL

Instance Method Summary collapse

Instance Method Details

#capturesHash, CaptureMap

Returns the map of capture names to captured values.

Returns:



49
50
51
# File 'lib/slim_lint/sexp_visitor.rb', line 49

def captures
  self.class.captures || {}
end

#on_startSymbol

Executed before searching for any pattern matches.

Parameters:

Returns:



64
65
66
# File 'lib/slim_lint/sexp_visitor.rb', line 64

def on_start(*)
  # Overidden by DSL.on_start
end

#patternsArray<SlimLint::SexpVisitor::SexpPattern>

Returns the list of registered Sexp patterns.

Returns:

  • (Array<SlimLint::SexpVisitor::SexpPattern>)


56
57
58
# File 'lib/slim_lint/sexp_visitor.rb', line 56

def patterns
  self.class.patterns || []
end

#traverse(sexp) ⇒ Object

Traverse the given Sexp, firing callbacks if they are defined.

Parameters:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/slim_lint/sexp_visitor.rb', line 20

def traverse(sexp)
  patterns.each do |pattern|
    next unless sexp.match?(pattern.sexp)

    result = method(pattern.callback_method_name).call(sexp)

    # Returning :stop indicates we should stop searching this Sexp
    # (i.e. stop descending this branch of depth-first search).
    # The `return` here is very intentional.
    return if result == :stop # rubocop:disable Lint/NonLocalExitFromIterator
  end

  # Continue traversing children by default (match blocks can return `:stop`
  # to not continue).
  traverse_children(sexp)
end

#traverse_children(sexp) ⇒ Object

Traverse the children of this SlimLint::Sexp.

Parameters:



40
41
42
43
44
# File 'lib/slim_lint/sexp_visitor.rb', line 40

def traverse_children(sexp)
  sexp.each do |nested_sexp|
    traverse nested_sexp if nested_sexp.is_a?(Sexp)
  end
end

#trigger_pattern_callbacks(sexp) ⇒ Object

Traverse the Sexp looking for matches with registered patterns, firing callbacks for all matches.

Parameters:



11
12
13
14
15
# File 'lib/slim_lint/sexp_visitor.rb', line 11

def trigger_pattern_callbacks(sexp)
  return if on_start(sexp) == :stop

  traverse sexp
end