Package com.google.javascript.jscomp
Class NodeTraversal
java.lang.Object
com.google.javascript.jscomp.NodeTraversal
NodeTraversal allows an iteration through the nodes in the parse tree, and facilitates the
optimizations on the parse tree.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
An traversal base class that tracks and caches the ControlFlowGraph (CFG) during the traversal.static class
Callback that fires on changed scopes.static class
Abstract callback that knows when a global script, goog.provide file, goog.module, goog.loadModule, ES module or CommonJS module is entered or exited.static class
Abstract callback to visit all nodes in postorder.static interface
Abstract callback to visit all nodes in postorder.static class
Abstract callback to visit all nodes in preorder.static class
Abstract scoped callback to visit all nodes in postorder.static class
Abstract callback to visit all nodes but not traverse into function bodies.static class
Abstract callback to visit all structure and statement nodes but doesn't traverse into functions or expressions.static final class
Builderstatic interface
Callback for tree-based traversalsstatic class
Abstract callback to visit all non-extern nodes in postorder.static interface
Callback that also knows about scope changes -
Method Summary
Modifier and TypeMethodDescriptionstatic NodeTraversal.Builder
builder()
AbstractScope
<?, ?> Gets the current scope.@Nullable JSChunk
getChunk()
Gets the current input chunk.@Nullable AbstractScope
<?, ?> @Nullable AbstractScope
<?, ?> @Nullable Node
@Nullable Node
Returns the closest scope binding the `this` or `super` keywordReturns the node currently being traversed.@Nullable Node
Returns the SCRIPT node enclosing the current scope@Nullable Node
Examines the functions stack for the last instance of a function node.getInput()
Gets the current input source.getScope()
com.google.javascript.jscomp.ScopeCreator
@Nullable Node
Returns the current scope's root.Gets the current input source name.boolean
Determines whether the traversal is currently in the scope of the block of a function.boolean
Determines whether the hoist scope of the current traversal is global.boolean
boolean
Determines whether the traversal is currently in the global scope.boolean
Determines whether the hoist scope of the current traversal is global.boolean
Determines whether the traversal is currently in a module scope.boolean
void
report
(Node n, DiagnosticType diagnosticType, String... arguments) Reports a diagnostic (error or warning)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`).void
void
static void
traverse
(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb) Traverses using the SyntacticScopeCreatorstatic void
traverseRoots
(AbstractCompiler compiler, NodeTraversal.Callback cb, Node externs, Node root) 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.
-
Method Details
-
builder
-
traverse
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
-
getSourceName
Gets the current input source name.- Returns:
- A string that may be empty, but not null
-
getInput
Gets the current input source. -
getChunk
Gets the current input chunk. -
getCurrentNode
Returns the node currently being traversed. -
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
Returns the current scope's root. -
getScope
-
getTypedScope
-
getAbstractScope
Gets the current scope. -
isHoistScope
public boolean isHoistScope() -
getClosestHoistScopeRoot
-
getClosestContainerScope
-
getClosestHoistScope
-
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
Reports a diagnostic (error or warning) -
report
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
-
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.
-