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.AbstractNodeTypePruningCallbackAbstract callback to visit a pruned set of nodes.static classNodeTraversal.AbstractPostOrderCallbackAbstract callback to visit all nodes in post order.static classNodeTraversal.AbstractScopedCallbackAbstract scoped callback to visit all nodes in post order.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.CallbackCallbackstatic interfaceNodeTraversal.ScopedCallbackCallback that also knows about scope changes
-
Field Summary
Fields Modifier and Type Field Description static DiagnosticTypeNODE_TRAVERSAL_ERROR
-
Constructor Summary
Constructors Constructor Description NodeTraversal(AbstractCompiler compiler, NodeTraversal.Callback cb)Creates a node traversal using the specified callback interface.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 CompilergetCompiler()Gets the compiler.com.google.javascript.jscomp.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()Gets the current scope.NodegetScopeRoot()Returns the current scope's root.java.lang.StringgetSourceName()Gets the current input source name.booleanhasScope()JSErrormakeError(Node n, CheckLevel level, DiagnosticType type, java.lang.String... arguments)Creates a JSError during NodeTraversal.JSErrormakeError(Node n, DiagnosticType type, java.lang.String... arguments)Creates a JSError during NodeTraversal.voidreport(Node n, DiagnosticType diagnosticType, java.lang.String... arguments)Reports a diagnostic (error or warning)static voidtraverse(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb)Traverses a node recursively.voidtraverse(Node root)Traverses a parse tree recursively.protected voidtraverseInnerNode(Node node, Node parent, Scope refinedScope)Traverses an inner node recursively with a refined scope.static voidtraverseRoots(AbstractCompiler compiler, NodeTraversal.Callback cb, Node... roots)static voidtraverseRoots(AbstractCompiler compiler, java.util.List<Node> roots, NodeTraversal.Callback cb)Traverses a list of node trees.voidtraverseRoots(Node... roots)voidtraverseRoots(java.util.List<Node> roots)
-
-
-
Field Detail
-
NODE_TRAVERSAL_ERROR
public static final DiagnosticType NODE_TRAVERSAL_ERROR
-
-
Constructor Detail
-
NodeTraversal
public NodeTraversal(AbstractCompiler compiler, NodeTraversal.Callback cb)
Creates a node traversal using the specified callback interface.
-
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.
-
traverseRoots
public void traverseRoots(Node... roots)
-
traverseRoots
public void traverseRoots(java.util.List<Node> roots)
-
traverseInnerNode
protected void traverseInnerNode(Node node, Node parent, Scope refinedScope)
Traverses an inner node recursively with a refined scope. An inner node may be any node with a nonnullparent (i.e. all nodes except the root).- Parameters:
node- the node to traverseparent- the node's parent, it may not benullrefinedScope- the refined scope of the scope currently at the top of the scope stack or in trivial cases that very scope ornull
-
getCompiler
public Compiler getCompiler()
Gets the compiler.
-
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.
-
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.
-
traverse
public static void traverse(AbstractCompiler compiler, Node root, NodeTraversal.Callback cb)
Traverses a node recursively.
-
traverseRoots
public static void traverseRoots(AbstractCompiler compiler, java.util.List<Node> roots, NodeTraversal.Callback cb)
Traverses a list of node trees.
-
traverseRoots
public static void traverseRoots(AbstractCompiler compiler, NodeTraversal.Callback cb, Node... roots)
-
getEnclosingFunction
public Node getEnclosingFunction()
Examines the functions stack for the last instance of a function node.
-
getScope
public Scope getScope()
Gets the current scope.
-
getControlFlowGraph
public com.google.javascript.jscomp.ControlFlowGraph<Node> getControlFlowGraph()
Gets the control flow graph for the current JS scope.
-
getScopeRoot
public Node getScopeRoot()
Returns the current scope's root.
-
hasScope
public boolean hasScope()
-
report
public void report(Node n, DiagnosticType diagnosticType, java.lang.String... arguments)
Reports a diagnostic (error or warning)
-
makeError
public JSError makeError(Node n, CheckLevel level, DiagnosticType type, java.lang.String... arguments)
Creates a JSError during NodeTraversal.- Parameters:
n- Determines the line and char position within the source file nametype- The DiagnosticTypearguments- Arguments to be incorporated into the message
-
makeError
public JSError makeError(Node n, DiagnosticType type, java.lang.String... arguments)
Creates a JSError during NodeTraversal.- Parameters:
n- Determines the line and char position within the source file nametype- The DiagnosticTypearguments- Arguments to be incorporated into the message
-
-