Class: JsDuck::Js::ScopedTraverser

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/js/scoped_traverser.rb

Overview

Traverses syntax tree while keeping track of variables that are bound to ‘this`.

Instance Method Summary collapse

Constructor Details

#initializeScopedTraverser

Returns a new instance of ScopedTraverser.



7
8
9
10
11
# File 'lib/jsduck/js/scoped_traverser.rb', line 7

def initialize
  @this_map = {
    "this" => true
  }
end

Instance Method Details

#this?(var_name) ⇒ Boolean

True when variable with given name is bound to ‘this`.

Returns:

  • (Boolean)


29
30
31
# File 'lib/jsduck/js/scoped_traverser.rb', line 29

def this?(var_name)
  @this_map[var_name]
end

#traverse(node, &block) ⇒ Object

Loops recursively over all the sub-nodes of the given node, calling the provided block for each sub-node.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jsduck/js/scoped_traverser.rb', line 15

def traverse(node, &block)
  node.body.each do |child|
    yield child

    if this_var?(child)
      var_name = child["id"].to_s
      @this_map[var_name] = true
    end

    traverse(child, &block)
  end
end