class JsDuck::Js::ScopedTraverser

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

Public Class Methods

new() click to toggle source
# File lib/jsduck/js/scoped_traverser.rb, line 7
def initialize
  @this_map = {
    "this" => true
  }
end

Public Instance Methods

this?(var_name) click to toggle source

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

# File lib/jsduck/js/scoped_traverser.rb, line 29
def this?(var_name)
  @this_map[var_name]
end
traverse(node) { |child| ... } click to toggle source

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

# 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

Private Instance Methods

this_var?(node) click to toggle source

True when initialization of variable with `this`

# File lib/jsduck/js/scoped_traverser.rb, line 36
def this_var?(node)
  node.type == "VariableDeclarator" && node["init"].type == "ThisExpression"
end