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 classAn traversal base class that tracks and caches the ControlFlowGraph (CFG) during the traversal.static classCallback that fires on changed scopes.static classAbstract callback that knows when a global script, goog.provide file, goog.module, goog.loadModule, ES module or CommonJS module is entered or exited.static classAbstract callback to visit all nodes in postorder.static interfaceAbstract callback to visit all nodes in postorder.static classAbstract callback to visit all nodes in preorder.static classAbstract scoped callback to visit all nodes in postorder.static classAbstract callback to visit all nodes but not traverse into function bodies.static classAbstract callback to visit all structure and statement nodes but doesn't traverse into functions or expressions.static final classBuilderstatic interfaceCallback for tree-based traversalsstatic classAbstract callback to visit all non-extern nodes in postorder.static interfaceCallback that also knows about scope changes -
Method Summary
Modifier and TypeMethodDescriptionstatic NodeTraversal.Builderbuilder()AbstractScope<?, ?> Gets the current scope.@Nullable JSChunkgetChunk()Gets the current input chunk.@Nullable AbstractScope<?, ?> @Nullable AbstractScope<?, ?> @Nullable Node@Nullable NodeReturns the closest scope binding the `this` or `super` keywordReturns the node currently being traversed.@Nullable NodeReturns the SCRIPT node enclosing the current scope@Nullable NodeExamines the functions stack for the last instance of a function node.getInput()Gets the current input source.getScope()com.google.javascript.jscomp.ScopeCreator@Nullable NodeReturns the current scope's root.Gets the current input source name.booleanDetermines whether the traversal is currently in the scope of the block of a function.booleanDetermines whether the hoist scope of the current traversal is global.booleanbooleanDetermines whether the traversal is currently in the global scope.booleanDetermines whether the hoist scope of the current traversal is global.booleanDetermines whether the traversal is currently in a module scope.booleanvoidreport(Node n, DiagnosticType diagnosticType, String... arguments) Reports a diagnostic (error or warning)voidreport(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`).voidvoidstatic voidtraverse(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb) Traverses using the SyntacticScopeCreatorstatic voidtraverseRoots(AbstractCompiler compiler, NodeTraversal.Callback cb, Node externs, Node root) static voidtraverseScopeRoots(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.
-