Package com.google.javascript.jscomp
Class NodeTraversal
- java.lang.Object
-
- com.google.javascript.jscomp.NodeTraversal
-
public class NodeTraversal extends java.lang.ObjectNodeTraversal allows an iteration through the nodes in the parse tree, and facilitates the optimizations on the parse tree.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNodeTraversal.AbstractModuleCallbackAbstract callback that knows when a global script, goog.provide file, goog.module, goog.loadModule, ES module or CommonJS module is entered or exited.static classNodeTraversal.AbstractPostOrderCallbackAbstract callback to visit all nodes in postorder.static interfaceNodeTraversal.AbstractPostOrderCallbackInterfaceAbstract callback to visit all nodes in postorder.static classNodeTraversal.AbstractPreOrderCallbackAbstract callback to visit all nodes in preorder.static classNodeTraversal.AbstractScopedCallbackAbstract scoped callback to visit all nodes in postorder.static classNodeTraversal.AbstractShallowCallbackAbstract callback to visit all nodes but not traverse into function bodies.static classNodeTraversal.AbstractShallowStatementCallbackAbstract callback to visit all structure and statement nodes but doesn't traverse into functions or expressions.static interfaceNodeTraversal.CallbackCallback for tree-based traversalsstatic interfaceNodeTraversal.ChangeScopeRootCallbackCallback for passes that iterate over a list of change scope roots (FUNCTIONs and SCRIPTs)static classNodeTraversal.ExternsSkippingCallbackAbstract callback to visit all non-extern nodes in postorder.static interfaceNodeTraversal.ScopedCallbackCallback that also knows about scope changes
-
Constructor Summary
Constructors Constructor Description NodeTraversal(AbstractCompiler compiler, NodeTraversal.Callback cb, com.google.javascript.jscomp.ScopeCreator scopeCreator)Creates a node traversal using the specified callback interface and the scope creator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AbstractScope<?,?>getAbstractScope()Gets the current scope.intgetCharno()Gets the current char number, or zero if it cannot be determined.AbstractScope<?,?>getClosestContainerScope()AbstractScope<?,?>getClosestHoistScope()NodegetClosestHoistScopeRoot()AbstractCompilergetCompiler()ControlFlowGraph<Node>getControlFlowGraph()Gets the control flow graph for the current JS scope.NodegetCurrentNode()Returns the node currently being traversed.NodegetEnclosingFunction()Examines the functions stack for the last instance of a function node.CompilerInputgetInput()Gets the current input source.intgetLineNumber()Gets the current line number, or zero if it cannot be determined.JSModulegetModule()Gets the current input module.ScopegetScope()com.google.javascript.jscomp.ScopeCreatorgetScopeCreator()NodegetScopeRoot()Returns the current scope's root.java.lang.StringgetSourceName()Gets the current input source name.TypedScopegetTypedScope()booleaninFunctionBlockScope()Determines whether the traversal is currently in the scope of the block of a function.booleaninGlobalHoistScope()Determines whether the hoist scope of the current traversal is global.booleaninGlobalOrModuleScope()booleaninGlobalScope()Determines whether the traversal is currently in the global scope.booleaninModuleHoistScope()Determines whether the hoist scope of the current traversal is global.booleaninModuleScope()Determines whether the traversal is currently in the global scope.booleanisHoistScope()voidreport(Node n, DiagnosticType diagnosticType, java.lang.String... arguments)Reports a diagnostic (error or warning)voidreportCodeChange()voidreportCodeChange(Node n)static voidtraverse(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb)Traverses using the SyntacticScopeCreatorvoidtraverse(Node root)Traverses a parse tree recursively.static voidtraverseChangedFunctions(AbstractCompiler compiler, NodeTraversal.ChangeScopeRootCallback callback)Traversal for passes that work only on changed functions.voidtraverseFunctionOutOfBand(Node node, AbstractScope<?,?> scope)Traverse a function out-of-band of normal traversal.static voidtraversePostOrder(AbstractCompiler compiler, Node root, NodeTraversal.AbstractPostOrderCallbackInterface cb)Traverses in post order.static voidtraverseRoots(AbstractCompiler compiler, NodeTraversal.Callback cb, Node externs, Node root)static voidtraverseScopeRoots(AbstractCompiler compiler, Node root, java.util.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.static voidtraverseScopeRoots(AbstractCompiler compiler, Node root, java.util.List<Node> scopeNodes, NodeTraversal.Callback cb, NodeTraversal.ChangeScopeRootCallback changeCallback, 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.
-
-
-
Constructor Detail
-
NodeTraversal
public NodeTraversal(AbstractCompiler compiler, NodeTraversal.Callback cb, com.google.javascript.jscomp.ScopeCreator scopeCreator)
Creates a node traversal using the specified callback interface and the scope creator.
-
-
Method Detail
-
traverse
public void traverse(Node root)
Traverses a parse tree recursively.
-
traverse
public static void traverse(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb)
Traverses using the SyntacticScopeCreator
-
traversePostOrder
public static void traversePostOrder(AbstractCompiler compiler, Node root, NodeTraversal.AbstractPostOrderCallbackInterface cb)
Traverses in post order.
-
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 java.util.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.
-
traverseScopeRoots
public static void traverseScopeRoots(AbstractCompiler compiler, @Nullable Node root, @Nullable java.util.List<Node> scopeNodes, NodeTraversal.Callback cb, @Nullable NodeTraversal.ChangeScopeRootCallback changeCallback, 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. Also allows for a callback to notify when starting on one of the provided scope nodes.- Parameters:
root- If scopeNodes is null, this method will just traverse 'root' instead. If scopeNodes is not null, this parameter is ignored.
-
traverseFunctionOutOfBand
public void traverseFunctionOutOfBand(Node node, AbstractScope<?,?> scope)
Traverse a function out-of-band of normal traversal.- Parameters:
node- The function node.scope- The scope the function is contained in. Does not fire enter/exit callback events for this scope.
-
getCompiler
public AbstractCompiler getCompiler()
-
getLineNumber
public int getLineNumber()
Gets the current line number, or zero if it cannot be determined. The line number is retrieved lazily as a running time optimization.
-
getCharno
public int getCharno()
Gets the current char number, or zero if it cannot be determined. The line number is retrieved lazily as a running time optimization.
-
getSourceName
public java.lang.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.
-
getModule
public JSModule getModule()
Gets the current input module.
-
getCurrentNode
public Node getCurrentNode()
Returns the node currently being traversed.
-
traverseChangedFunctions
public static void traverseChangedFunctions(AbstractCompiler compiler, NodeTraversal.ChangeScopeRootCallback callback)
Traversal for passes that work only on changed functions. Suppose a loopable pass P1 uses this traversal. Then, if a function doesn't change between two runs of P1, it won't look at the function the second time. (We're assuming that P1 runs to a fixpoint, o/w we may miss optimizations.)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. Passes that do cross-scope modifications call Compiler.reportChangeToEnclosingScope(Node n).
-
getEnclosingFunction
public 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.
-
getAbstractScope
public AbstractScope<?,?> getAbstractScope()
Gets the current scope.
-
isHoistScope
public boolean isHoistScope()
-
getClosestHoistScopeRoot
public Node getClosestHoistScopeRoot()
-
getClosestContainerScope
public AbstractScope<?,?> getClosestContainerScope()
-
getClosestHoistScope
public AbstractScope<?,?> getClosestHoistScope()
-
getScope
public Scope getScope()
-
getTypedScope
public TypedScope getTypedScope()
-
getControlFlowGraph
public ControlFlowGraph<Node> getControlFlowGraph()
Gets the control flow graph for the current JS scope.
-
getScopeRoot
public Node getScopeRoot()
Returns the current scope's root.
-
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 the global scope. Note that this returns false in a global block 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, java.lang.String... arguments)
Reports a diagnostic (error or warning)
-
reportCodeChange
public void reportCodeChange()
-
reportCodeChange
public void reportCodeChange(Node n)
-
-