Sha256: 5451d93e60bd1083c2286c56623c0290bf720aac73ba9acef75fa9864aae3bc7

Contents?: true

Size: 966 Bytes

Versions: 28

Compression:

Stored size: 966 Bytes

Contents

module JsDuck
  module Js

    # Traverses syntax tree while keeping track of variables that are
    # bound to `this`.
    class ScopedTraverser
      def initialize
        @this_map = {
          "this" => true
        }
      end

      # Loops recursively over all the sub-nodes of the given node,
      # calling the provided block for each sub-node.
      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

      # True when variable with given name is bound to `this`.
      def this?(var_name)
        @this_map[var_name]
      end

      private

      # True when initialization of variable with `this`
      def this_var?(node)
        node.type == "VariableDeclarator" && node["init"].type == "ThisExpression"
      end

    end
  end
end

Version data entries

28 entries across 28 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/js/scoped_traverser.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/js/scoped_traverser.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/js/scoped_traverser.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/js/scoped_traverser.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/js/scoped_traverser.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/js/scoped_traverser.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/js/scoped_traverser.rb
jsduck-troopjs-0.0.10 lib/jsduck/js/scoped_traverser.rb
jsduck-troopjs-0.0.9 lib/jsduck/js/scoped_traverser.rb
jsduck-troopjs-0.0.8 lib/jsduck/js/scoped_traverser.rb
jsduck-troopjs-0.0.7 lib/jsduck/js/scoped_traverser.rb
jsduck-troopjs-0.0.5 lib/jsduck/js/scoped_traverser.rb
jsduck-troopjs-0.0.4 lib/jsduck/js/scoped_traverser.rb
jsduck-troopjs-0.0.3 lib/jsduck/js/scoped_traverser.rb
jsduck-troopjs-0.0.1 lib/jsduck/js/scoped_traverser.rb
jsduck-6.0.0beta lib/jsduck/js/scoped_traverser.rb
jsduck-5.3.4 lib/jsduck/js/scoped_traverser.rb
jsduck-5.3.3 lib/jsduck/js/scoped_traverser.rb
jsduck-5.3.2 lib/jsduck/js/scoped_traverser.rb
jsduck-5.3.1 lib/jsduck/js/scoped_traverser.rb