public class NodeTraversal
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
NodeTraversal.AbstractNodeTypePruningCallback
Abstract callback to visit a pruned set of nodes.
|
static class |
NodeTraversal.AbstractPostOrderCallback
Abstract callback to visit all nodes in postorder.
|
static class |
NodeTraversal.AbstractPreOrderCallback
Abstract callback to visit all nodes in preorder.
|
static class |
NodeTraversal.AbstractScopedCallback
Abstract scoped callback to visit all nodes in postorder.
|
static class |
NodeTraversal.AbstractShallowCallback
Abstract callback to visit all nodes but not traverse into function
bodies.
|
static class |
NodeTraversal.AbstractShallowStatementCallback
Abstract callback to visit all structure and statement nodes but doesn't
traverse into functions or expressions.
|
static interface |
NodeTraversal.Callback
Callback for tree-based traversals
|
static interface |
NodeTraversal.FunctionCallback
Callback for passes that iterate over a list of functions
|
static interface |
NodeTraversal.ScopedCallback
Callback that also knows about scope changes
|
Modifier and Type | Field and Description |
---|---|
private NodeTraversal.Callback |
callback |
private java.util.Deque<Node> |
cfgRoots
A stack of scope roots that are valid cfg roots.
|
(package private) java.util.Deque<ControlFlowGraph<Node>> |
cfgs
Stack of control flow graphs (CFG).
|
private AbstractCompiler |
compiler |
private Node |
curNode
Contains the current node
|
private InputId |
inputId
The current input
|
private static java.lang.String |
MISSING_SOURCE |
static DiagnosticType |
NODE_TRAVERSAL_ERROR |
private NodeTraversal.ScopedCallback |
scopeCallback
Possible callback for scope entry and exist
|
private ScopeCreator |
scopeCreator
The scope creator
|
private java.util.Deque<Node> |
scopeRoots
A stack of scope roots.
|
private java.util.Deque<Scope> |
scopes
Stack containing the Scopes that have been created.
|
private java.lang.String |
sourceName
The current source file name
|
private boolean |
useBlockScope |
Constructor and Description |
---|
NodeTraversal(AbstractCompiler compiler,
NodeTraversal.Callback cb)
Creates a node traversal using the specified callback interface.
|
NodeTraversal(AbstractCompiler compiler,
NodeTraversal.Callback cb,
ScopeCreator scopeCreator)
Creates a node traversal using the specified callback interface
and the scope creator.
|
Modifier and Type | Method and Description |
---|---|
private java.lang.String |
formatNodeContext(java.lang.String label,
Node n) |
private java.lang.String |
formatNodePosition(Node n) |
private java.lang.String |
getBestSourceFileName(Node n) |
private Node |
getCfgRoot() |
int |
getCharno()
Gets the current char number, or zero if it cannot be determined.
|
Scope |
getClosestHoistScope() |
AbstractCompiler |
getCompiler() |
ControlFlowGraph<Node> |
getControlFlowGraph()
Gets the control flow graph for the current JS scope.
|
Node |
getCurrentNode()
Returns the node currently being traversed.
|
Node |
getEnclosingFunction()
Examines the functions stack for the last instance of a function node.
|
CompilerInput |
getInput()
Gets the current input source.
|
(package private) InputId |
getInputId() |
int |
getLineNumber()
Gets the current line number, or zero if it cannot be determined.
|
JSModule |
getModule()
Gets the current input module.
|
Scope |
getScope()
Gets the current scope.
|
(package private) int |
getScopeDepth() |
Node |
getScopeRoot()
Returns the current scope's root.
|
java.lang.String |
getSourceName()
Gets the current input source name.
|
private static java.lang.String |
getSourceName(Node n) |
TypedScope |
getTypedScope() |
boolean |
hasScope() |
boolean |
inGlobalHoistScope()
Determines whether the hoist scope of the current traversal is global.
|
boolean |
inGlobalScope()
Determines whether the traversal is currently in the global scope.
|
JSError |
makeError(Node n,
CheckLevel level,
DiagnosticType type,
java.lang.String... arguments)
Creates a JSError during NodeTraversal.
|
JSError |
makeError(Node n,
DiagnosticType type,
java.lang.String... arguments)
Creates a JSError during NodeTraversal.
|
private void |
popScope() |
private void |
popScope(boolean quietly)
Pops back to the previous scope (e.g.
|
private void |
pushScope(Node node)
Creates a new scope (e.g.
|
private void |
pushScope(Scope s)
Creates a new scope (e.g.
|
private void |
pushScope(Scope s,
boolean quietly)
Creates a new scope (e.g.
|
void |
report(Node n,
DiagnosticType diagnosticType,
java.lang.String... arguments)
Reports a diagnostic (error or warning)
|
private void |
throwUnexpectedException(java.lang.Exception unexpectedException) |
static void |
traverse(AbstractCompiler compiler,
Node root,
NodeTraversal.Callback cb)
Deprecated.
Use traverseEs6 whenever possible.
|
void |
traverse(Node root)
Traverses a parse tree recursively.
|
(package private) void |
traverseAtScope(Scope s)
Traverses a parse tree recursively with a scope, starting at that scope's
root.
|
private void |
traverseBlockScope(Node n)
Traverses a non-function block.
|
private void |
traverseBranch(Node n,
Node parent)
Traverses a branch.
|
static void |
traverseChangedFunctions(AbstractCompiler compiler,
NodeTraversal.FunctionCallback callback)
Traversal for passes that work only on changed functions.
|
private void |
traverseClass(Node n,
Node parent)
Traverses a class.
|
static void |
traverseEs6(AbstractCompiler compiler,
Node root,
NodeTraversal.Callback cb)
Traverses using the ES6SyntacticScopeCreator
|
private void |
traverseFunction(Node n,
Node parent)
Traverses a function.
|
void |
traverseFunctionOutOfBand(Node node,
Scope scope)
Traverse a function out-of-band of normal traversal.
|
(package private) void |
traverseInnerNode(Node node,
Node parent,
Scope refinedScope)
Traverses an inner node recursively with a refined scope.
|
static void |
traverseRoots(AbstractCompiler compiler,
NodeTraversal.Callback cb,
Node externs,
Node root)
Deprecated.
|
(package private) void |
traverseRoots(Node externs,
Node root) |
static void |
traverseRootsEs6(AbstractCompiler compiler,
NodeTraversal.Callback cb,
Node externs,
Node root) |
static void |
traverseRootsTyped(AbstractCompiler compiler,
NodeTraversal.Callback cb,
Node externs,
Node root) |
static void |
traverseTyped(AbstractCompiler compiler,
Node root,
NodeTraversal.Callback cb) |
(package private) void |
traverseWithScope(Node root,
Scope s)
Traverses a parse tree recursively with a scope, starting with the given
root.
|
private final AbstractCompiler compiler
private final NodeTraversal.Callback callback
private Node curNode
public static final DiagnosticType NODE_TRAVERSAL_ERROR
private final java.util.Deque<Scope> scopes
scopeRoots
stack contains the
Nodes for all Scopes that have not been created yet.private final java.util.Deque<Node> scopeRoots
private final java.util.Deque<Node> cfgRoots
java.util.Deque<ControlFlowGraph<Node>> cfgs
null
until requested by
getControlFlowGraph()
. Note that ArrayDeque
does not allow
null
elements, so LinkedList
is used instead.private java.lang.String sourceName
private InputId inputId
private final ScopeCreator scopeCreator
private final boolean useBlockScope
private NodeTraversal.ScopedCallback scopeCallback
private static final java.lang.String MISSING_SOURCE
public NodeTraversal(AbstractCompiler compiler, NodeTraversal.Callback cb)
public NodeTraversal(AbstractCompiler compiler, NodeTraversal.Callback cb, ScopeCreator scopeCreator)
private void throwUnexpectedException(java.lang.Exception unexpectedException)
private java.lang.String formatNodeContext(java.lang.String label, Node n)
public void traverse(Node root)
private java.lang.String formatNodePosition(Node n)
void traverseWithScope(Node root, Scope s)
traverseAtScope(com.google.javascript.jscomp.Scope)
.void traverseAtScope(Scope s)
public void traverseFunctionOutOfBand(Node node, Scope scope)
node
- The function node.scope
- The scope the function is contained in. Does not fire enter/exit
callback events for this scope.void traverseInnerNode(Node node, Node parent, Scope refinedScope)
null
parent (i.e. all nodes except the
root).node
- the node to traverseparent
- the node's parent, it may not be null
refinedScope
- the refined scope of the scope currently at the top of
the scope stack or in trivial cases that very scope or null
public AbstractCompiler getCompiler()
public int getLineNumber()
public int getCharno()
public java.lang.String getSourceName()
public CompilerInput getInput()
public JSModule getModule()
public Node getCurrentNode()
public static void traverseChangedFunctions(AbstractCompiler compiler, NodeTraversal.FunctionCallback callback)
Most changes are reported with calls to Compiler.reportCodeChange(), which doesn't know which scope changed. We keep track of the current scope by calling Compiler.setScope inside pushScope and popScope. The automatic tracking can be wrong in rare cases when a pass changes scope w/out causing a call to pushScope or popScope. It's very hard to find the places where this happens unless a bug is triggered. Passes that do cross-scope modifications call Compiler.reportChangeToEnclosingScope(Node n).
@Deprecated public static void traverse(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb)
public static void traverseEs6(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb)
public static void traverseTyped(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb)
@Deprecated public static void traverseRoots(AbstractCompiler compiler, NodeTraversal.Callback cb, Node externs, Node root)
public static void traverseRootsEs6(AbstractCompiler compiler, NodeTraversal.Callback cb, Node externs, Node root)
public static void traverseRootsTyped(AbstractCompiler compiler, NodeTraversal.Callback cb, Node externs, Node root)
private void traverseBlockScope(Node n)
public Node getEnclosingFunction()
private void pushScope(Node node)
private void pushScope(Scope s)
private void pushScope(Scope s, boolean quietly)
quietly
- Don't fire an enterScope callback.private void popScope()
private void popScope(boolean quietly)
quietly
- Don't fire the exitScope callback.public Scope getScope()
public Scope getClosestHoistScope()
public TypedScope getTypedScope()
public ControlFlowGraph<Node> getControlFlowGraph()
public Node getScopeRoot()
private Node getCfgRoot()
public boolean inGlobalScope()
public boolean inGlobalHoistScope()
int getScopeDepth()
public boolean hasScope()
public void report(Node n, DiagnosticType diagnosticType, java.lang.String... arguments)
private static java.lang.String getSourceName(Node n)
InputId getInputId()
public JSError makeError(Node n, CheckLevel level, DiagnosticType type, java.lang.String... arguments)
n
- Determines the line and char position within the source file nametype
- The DiagnosticTypearguments
- Arguments to be incorporated into the messagepublic JSError makeError(Node n, DiagnosticType type, java.lang.String... arguments)
n
- Determines the line and char position within the source file nametype
- The DiagnosticTypearguments
- Arguments to be incorporated into the messageprivate java.lang.String getBestSourceFileName(Node n)