Class: Transpec::AST::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/transpec/ast/scanner.rb

Constant Summary collapse

SCOPE_TYPES =
[:module, :class, :sclass, :def, :defs, :block].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Scanner

Returns a new instance of Scanner.



19
20
21
22
23
# File 'lib/transpec/ast/scanner.rb', line 19

def initialize(&block)
  @callback = block
  @ancestor_nodes = []
  @scope_stack = ScopeStack.new
end

Instance Attribute Details

#scope_stackObject (readonly)

Returns the value of attribute scope_stack.



11
12
13
# File 'lib/transpec/ast/scanner.rb', line 11

def scope_stack
  @scope_stack
end

Class Method Details

.scan(origin_node, &block) ⇒ Object



13
14
15
16
17
# File 'lib/transpec/ast/scanner.rb', line 13

def self.scan(origin_node, &block)
  instance = new(&block)
  instance.scan(origin_node, true)
  nil
end

Instance Method Details

#scan(origin_node, yield_origin_node = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/transpec/ast/scanner.rb', line 25

def scan(origin_node, yield_origin_node = false)
  return unless origin_node

  yield_node(origin_node) if yield_origin_node

  @ancestor_nodes.push(origin_node)
  @scope_stack.push_scope(origin_node) if scope_node?(origin_node)

  origin_node.children.each_with_index do |child, index|
    next unless child.is_a?(Parser::AST::Node)
    node = child
    yield_node(node)
    scan(node)
  end

  @scope_stack.pop_scope if scope_node?(origin_node)
  @ancestor_nodes.pop
end