Class NodeTraversal

java.lang.Object
com.google.javascript.jscomp.NodeTraversal

public class NodeTraversal extends Object
NodeTraversal allows an iteration through the nodes in the parse tree, and facilitates the optimizations on the parse tree.
  • Method Details

    • builder

      public static NodeTraversal.Builder builder()
    • traverse

      public static void traverse(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb)
      Traverses using the SyntacticScopeCreator
    • traverseRoots

      public static void traverseRoots(AbstractCompiler compiler, NodeTraversal.Callback cb, Node externs, Node root)
    • traverseScopeRoots

      public static void traverseScopeRoots(AbstractCompiler compiler, @Nullable Node root, @Nullable List<Node> scopeNodes, NodeTraversal.Callback cb, boolean traverseNested)
      Traverses *just* the contents of provided scope nodes (and optionally scopes nested within them) but will fall back on traversing the entire AST from root if a null scope nodes list is provided.
      Parameters:
      root - If scopeNodes is null, this method will just traverse 'root' instead. If scopeNodes is not null, this parameter is ignored.
    • getCompiler

      public AbstractCompiler getCompiler()
    • getSourceName

      public String getSourceName()
      Gets the current input source name.
      Returns:
      A string that may be empty, but not null
    • getInput

      public CompilerInput getInput()
      Gets the current input source.
    • getChunk

      public @Nullable JSChunk getChunk()
      Gets the current input chunk.
    • getCurrentNode

      public Node getCurrentNode()
      Returns the node currently being traversed.
    • getEnclosingFunction

      public @Nullable Node getEnclosingFunction()
      Examines the functions stack for the last instance of a function node. When possible, prefer this method over NodeUtil.getEnclosingFunction() because this in general looks at less nodes.
    • getScopeRoot

      public @Nullable Node getScopeRoot()
      Returns the current scope's root.
    • getScope

      public Scope getScope()
    • getTypedScope

      public TypedScope getTypedScope()
    • getAbstractScope

      public AbstractScope<?,?> getAbstractScope()
      Gets the current scope.
    • isHoistScope

      public boolean isHoistScope()
    • getClosestHoistScopeRoot

      public @Nullable Node getClosestHoistScopeRoot()
    • getClosestContainerScope

      public @Nullable AbstractScope<?,?> getClosestContainerScope()
    • getClosestHoistScope

      public @Nullable AbstractScope<?,?> getClosestHoistScope()
    • getClosestScopeRootNodeBindingThisOrSuper

      public @Nullable Node getClosestScopeRootNodeBindingThisOrSuper()
      Returns the closest scope binding the `this` or `super` keyword
    • getScopeCreator

      public com.google.javascript.jscomp.ScopeCreator getScopeCreator()
    • inGlobalScope

      public boolean inGlobalScope()
      Determines whether the traversal is currently in the global scope. Note that this returns false in a global block scope.
    • inModuleScope

      public boolean inModuleScope()
      Determines whether the traversal is currently in a module scope.
    • inGlobalOrModuleScope

      public boolean inGlobalOrModuleScope()
    • inFunctionBlockScope

      public boolean inFunctionBlockScope()
      Determines whether the traversal is currently in the scope of the block of a function.
    • inGlobalHoistScope

      public boolean inGlobalHoistScope()
      Determines whether the hoist scope of the current traversal is global.
    • inModuleHoistScope

      public boolean inModuleHoistScope()
      Determines whether the hoist scope of the current traversal is global.
    • report

      public void report(Node n, DiagnosticType diagnosticType, String... arguments)
      Reports a diagnostic (error or warning)
    • report

      public void report(Node start, Node end, DiagnosticType diagnosticType, String... arguments)
      Reports a diagnostic (error or warning) This variant is particularly useful for reporting on GETPROP nodes whose length is otherwise just the last component (`abc` in `foo.abc`).
    • reportCodeChange

      public void reportCodeChange()
    • reportCodeChange

      public void reportCodeChange(Node n)
    • getCurrentScript

      public @Nullable Node getCurrentScript()
      Returns the SCRIPT node enclosing the current scope
      Throws:
      UnsupportedOperationException - if inside a NodeTraversal that does not support this operation, or if called while visiting a ROOT node during a global traversal. Only traversals that begin at a ROOT node (which includes most usages of NodeTraversal) will support this operation. For example, NodeTraversal.Builder.traverseAtScope(AbstractScope<?, ?>) is unsupported because it can begin at any arbitrary scope root like a FUNCTION.