Package com.google.javascript.jscomp
Class ReferenceCollectingCallback
- java.lang.Object
-
- com.google.javascript.jscomp.ReferenceCollectingCallback
-
- All Implemented Interfaces:
CompilerPass
,HotSwapCompilerPass
,NodeTraversal.Callback
,NodeTraversal.ScopedCallback
,StaticSymbolTable<Var,Reference>
public final class ReferenceCollectingCallback extends java.lang.Object implements NodeTraversal.ScopedCallback, HotSwapCompilerPass, StaticSymbolTable<Var,Reference>
A helper class for passes that want to access all information about where a variable is referenced and declared at once and then make a decision as to how it should be handled, possibly inlining, reordering, or generating warnings. Callers do this by providingReferenceCollectingCallback.Behavior
and then callingprocess(Node, Node)
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ReferenceCollectingCallback.Behavior
Way for callers to add specific behavior during traversal that utilizes the built-up reference information.
-
Constructor Summary
Constructors Constructor Description ReferenceCollectingCallback(AbstractCompiler compiler, ReferenceCollectingCallback.Behavior behavior, com.google.javascript.jscomp.ScopeCreator creator)
Constructor initializes block stack.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
enterScope(NodeTraversal t)
Updates block stack and invokes any additional behavior.void
exitScope(NodeTraversal t)
Updates block stack and invokes any additional behavior.java.lang.Iterable<Var>
getAllSymbols()
Gets the variables that were referenced in this callback.ReferenceCollection
getReferences(Var v)
Gets the reference collection for the given variable.Scope
getScope(Var var)
Returns the scope for a given symbol.void
hotSwapScript(Node scriptRoot, Node originalRoot)
Same as process but only runs on a part of AST associated to one script.void
process(Node root)
void
process(Node externs, Node root)
Convenience method for running this pass over a tree with this class as a callback.boolean
shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)
Updates block stack.void
visit(NodeTraversal t, Node n, Node parent)
For each node, update the block stack and reference collection as appropriate.
-
-
-
Constructor Detail
-
ReferenceCollectingCallback
public ReferenceCollectingCallback(AbstractCompiler compiler, ReferenceCollectingCallback.Behavior behavior, com.google.javascript.jscomp.ScopeCreator creator)
Constructor initializes block stack.
-
-
Method Detail
-
process
public void process(Node externs, Node root)
Convenience method for running this pass over a tree with this class as a callback.- Specified by:
process
in interfaceCompilerPass
- Parameters:
externs
- Top of external JS treeroot
- Top of JS tree
-
process
public void process(Node root)
-
hotSwapScript
public void hotSwapScript(Node scriptRoot, Node originalRoot)
Same as process but only runs on a part of AST associated to one script.- Specified by:
hotSwapScript
in interfaceHotSwapCompilerPass
- Parameters:
scriptRoot
- Root node corresponding to the file that is modified, should be of typeToken.SCRIPT
.originalRoot
- Root node corresponding to the original version of the file that is modified. Should be of typetoken.SCRIPT
.
-
getAllSymbols
public java.lang.Iterable<Var> getAllSymbols()
Gets the variables that were referenced in this callback.- Specified by:
getAllSymbols
in interfaceStaticSymbolTable<Var,Reference>
-
getScope
public Scope getScope(Var var)
Description copied from interface:StaticSymbolTable
Returns the scope for a given symbol.- Specified by:
getScope
in interfaceStaticSymbolTable<Var,Reference>
-
getReferences
public ReferenceCollection getReferences(Var v)
Gets the reference collection for the given variable.- Specified by:
getReferences
in interfaceStaticSymbolTable<Var,Reference>
-
visit
public void visit(NodeTraversal t, Node n, Node parent)
For each node, update the block stack and reference collection as appropriate.- Specified by:
visit
in interfaceNodeTraversal.Callback
- Parameters:
t
- The current traversal.n
- The current node.parent
- The parent of the current node.
-
enterScope
public void enterScope(NodeTraversal t)
Updates block stack and invokes any additional behavior.- Specified by:
enterScope
in interfaceNodeTraversal.ScopedCallback
-
exitScope
public void exitScope(NodeTraversal t)
Updates block stack and invokes any additional behavior.- Specified by:
exitScope
in interfaceNodeTraversal.ScopedCallback
-
shouldTraverse
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)
Updates block stack.- Specified by:
shouldTraverse
in interfaceNodeTraversal.Callback
- Parameters:
nodeTraversal
- The current traversal.n
- The current node.parent
- The parent of the current node.- Returns:
- whether the children of this node should be visited
-
-