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.Behaviorand then callingprocess(Node, Node).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceReferenceCollectingCallback.BehaviorWay 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 voidenterScope(NodeTraversal t)Updates block stack and invokes any additional behavior.voidexitScope(NodeTraversal t)Updates block stack and invokes any additional behavior.java.lang.Iterable<Var>getAllSymbols()Gets the variables that were referenced in this callback.ReferenceCollectiongetReferences(Var v)Gets the reference collection for the given variable.ScopegetScope(Var var)Returns the scope for a given symbol.voidhotSwapScript(Node scriptRoot, Node originalRoot)Same as process but only runs on a part of AST associated to one script.voidprocess(Node root)voidprocess(Node externs, Node root)Convenience method for running this pass over a tree with this class as a callback.booleanshouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)Updates block stack.voidvisit(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:
processin 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:
hotSwapScriptin 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:
getAllSymbolsin interfaceStaticSymbolTable<Var,Reference>
-
getScope
public Scope getScope(Var var)
Description copied from interface:StaticSymbolTableReturns the scope for a given symbol.- Specified by:
getScopein interfaceStaticSymbolTable<Var,Reference>
-
getReferences
public ReferenceCollection getReferences(Var v)
Gets the reference collection for the given variable.- Specified by:
getReferencesin 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:
visitin 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:
enterScopein interfaceNodeTraversal.ScopedCallback
-
exitScope
public void exitScope(NodeTraversal t)
Updates block stack and invokes any additional behavior.- Specified by:
exitScopein interfaceNodeTraversal.ScopedCallback
-
shouldTraverse
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)
Updates block stack.- Specified by:
shouldTraversein 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
-
-