public final class NodeUtil extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
NodeUtil.Visitor
Interface for use with the visit method.
|
Modifier and Type | Field and Description |
---|---|
static String |
EXTERN_OBJECT_PROPERTY_STRING |
Modifier and Type | Method and Description |
---|---|
static boolean |
containsType(Node node,
Token type) |
static Node |
getAssignedValue(Node n)
For an assignment or variable declaration get the assigned value.
|
static JSDocInfo |
getBestJSDocInfo(Node n)
Find the best JSDoc for the given node.
|
static Node |
getClassMembers(Node n) |
static int |
getCount(Node n,
com.google.common.base.Predicate<Node> pred,
com.google.common.base.Predicate<Node> traverseChildrenPred) |
static Node |
getEnclosingBlock(Node n)
Finds the block containing the given node.
|
static Node |
getEnclosingBlockScopeRoot(Node n) |
static Node |
getEnclosingClass(Node n)
Finds the class containing the given node.
|
static Node |
getEnclosingFunction(Node n)
Finds the function containing the given node.
|
static Node |
getEnclosingNode(Node n,
com.google.common.base.Predicate<Node> pred) |
static Node |
getEnclosingScopeRoot(Node n) |
static Node |
getEnclosingScript(Node n)
Finds the script containing the given node.
|
static Node |
getEnclosingStatement(Node n) |
static Node |
getEnclosingType(Node n,
Token type)
Gets the closest ancestor to the given node of the provided type.
|
static Node |
getFunctionBody(Node fn)
Return a BLOCK node for the given FUNCTION node.
|
static Node |
getFunctionParameters(Node fnNode) |
static InputId |
getInputId(Node n) |
static String |
getName(Node n) |
static Node |
getNameNode(Node n)
Gets the node of a function or class's name.
|
static String |
getNearestFunctionName(Node n)
Gets the function's name.
|
static Node |
getNodeByLineCol(Node ancestor,
int lineNo,
int columNo) |
static Double |
getNumberValue(Node n) |
static StaticSourceFile |
getSourceFile(Node n) |
static String |
getSourceName(Node n) |
static String |
getStringValue(Node n)
Gets the value of a node as a String, or null if it cannot be converted.
|
static boolean |
has(Node node,
com.google.common.base.Predicate<Node> pred,
com.google.common.base.Predicate<Node> traverseChildrenPred) |
static boolean |
isAssignmentOp(Node n) |
static boolean |
isBooleanResult(Node n) |
static boolean |
isCallOrNew(Node node) |
static boolean |
isCallTo(Node n,
String qualifiedName) |
static boolean |
isClassDeclaration(Node n)
|
static boolean |
isCompoundAssignementOp(Node n) |
static boolean |
isControlStructure(Node n)
Determines whether the given node is a FOR, DO, WHILE, WITH, or IF node.
|
static boolean |
isExprCall(Node n)
Is this node a call expression statement?
|
static boolean |
isFunctionDeclaration(Node n)
Is this node a function declaration? A function declaration is a function
that has a name that is added to the current scope (i.e.
|
static boolean |
isGet(Node n)
Is this a GETPROP or GETELEM node?
|
static boolean |
isHoistedFunctionDeclaration(Node n)
Is this node a hoisted function declaration? A function declaration in the
scope root is hoisted to the top of the scope.
|
static boolean |
isImportedName(Node n) |
static boolean |
isInFunction(Node n) |
static boolean |
isLhsByDestructuring(Node n) |
static boolean |
isLiteralValue(Node n,
boolean includeFunctions)
Returns true if this is a literal value.
|
static boolean |
isLValue(Node n)
Determines whether this node is used as an L-value.
|
static boolean |
isNameDeclaration(Node n)
Is this node a name declaration?
|
static boolean |
isNullOrUndefined(Node n) |
static boolean |
isNumericResult(Node n)
Returns true if the result of node evaluation is always a number
|
static boolean |
isObjectResult(Node n) |
static boolean |
isPrototypePropertyDeclaration(Node n) |
static boolean |
isStatement(Node n) |
static boolean |
isStatementBlock(Node n) |
static boolean |
isStringResult(Node n) |
static boolean |
isTopLevel(Node n) |
static boolean |
isUndefined(Node n) |
static boolean |
isValidQualifiedName(CompilerOptions.LanguageMode mode,
String name)
Determines whether the given name is a valid qualified name.
|
static Map<Node,Node> |
mapMainToClone(Node main,
Node clone)
Given an AST and its copy, map the root node of each scope of main to the
corresponding root node of clone
|
static boolean |
mayHaveSideEffects(Node n)
Returns true if the node which may have side effects when executed.
|
static boolean |
mayHaveSideEffects(Node n,
AbstractCompiler compiler) |
static Node |
newPropertyAccess(AbstractCompiler compiler,
Node context,
String name)
Creates a property access on the
context tree. |
static Node |
newQName(AbstractCompiler compiler,
String name)
Creates a node representing a qualified name.
|
static Node |
newQNameDeclaration(AbstractCompiler compiler,
String name,
Node value,
JSDocInfo info)
Creates a node representing a qualified name.
|
static String |
opToStr(Token operator)
Converts an operator's token value (see
Token ) to a string
representation. |
static void |
removeChild(Node parent,
Node node)
Safely remove children while maintaining a valid node structure.
|
static void |
removeName(Node n)
Set the given function/class node to an empty name
|
static void |
replaceDeclarationChild(Node declChild,
Node newStatement)
Replace the child of a var/let/const declaration (usually a name) with a new statement.
|
static boolean |
tryMergeBlock(Node block)
Merge a block with its parent block.
|
static void |
verifyScopeChanges(Map<Node,Node> map,
Node main,
boolean verifyUnchangedNodes)
Checks that the scope roots marked as changed have indeed changed
|
static void |
visitPostOrder(Node node,
NodeUtil.Visitor visitor,
com.google.common.base.Predicate<Node> traverseChildrenPred)
A post-order traversal, calling Visitor.visit for each descendant matching
the predicate.
|
static void |
visitPreOrder(Node node,
NodeUtil.Visitor visitor)
A pre-order traversal, calling Visitor.visit for each decendent.
|
static void |
visitPreOrder(Node node,
NodeUtil.Visitor visitor,
com.google.common.base.Predicate<Node> traverseChildrenPred)
A pre-order traversal, calling Visitor.visit for each child matching
the predicate.
|
public static final String EXTERN_OBJECT_PROPERTY_STRING
public static String getStringValue(Node n)
String()
JavaScript cast function.public static String getName(Node n)
n
- A function or class node.public static Node getNameNode(Node n)
class name {...}
var name = class {...}
qualified.name = class {...}
var name2 = class name1 {...}
qualified.name2 = class name1 {...}
n
- A function or class nodepublic static void removeName(Node n)
public static String getNearestFunctionName(Node n)
{'name': function() ...}
{name: function() ...}
function name() ...
var name = function() ...
var obj = {name() {} ...}
qualified.name = function() ...
var name2 = function name1() ...
qualified.name2 = function name1() ...
n
- a node whose type is Token.FUNCTION
null
if it has no namepublic static boolean isLiteralValue(Node n, boolean includeFunctions)
Function literals do not meet this definition, because they
lexically capture variables. For example, if you have
function() { return a; }
If it is evaluated in a different scope, then it
captures a different variable. Even if the function did not read
any captured variables directly, it would still fail this definition,
because it affects the lifecycle of variables in the enclosing scope.
However, a function literal with respect to a particular scope is a literal.
includeFunctions
- If true, all function expressions will be
treated as literals.public static boolean mayHaveSideEffects(Node n)
public static boolean mayHaveSideEffects(Node n, AbstractCompiler compiler)
public static boolean isUndefined(Node n)
public static boolean isNullOrUndefined(Node n)
public static boolean isNumericResult(Node n)
public static boolean isBooleanResult(Node n)
public static boolean isStringResult(Node n)
public static boolean isObjectResult(Node n)
public static boolean isAssignmentOp(Node n)
public static boolean isCompoundAssignementOp(Node n)
public static Node getEnclosingType(Node n, Token type)
public static Node getEnclosingClass(Node n)
public static Node getEnclosingFunction(Node n)
public static Node getEnclosingScript(Node n)
public static Node getEnclosingBlock(Node n)
public static boolean isInFunction(Node n)
public static Node getEnclosingNode(Node n, com.google.common.base.Predicate<Node> pred)
public static boolean isGet(Node n)
public static boolean isNameDeclaration(Node n)
n
- The noden
is VAR, LET or CONSTpublic static Node getAssignedValue(Node n)
public static boolean isExprCall(Node n)
n
- The noden
is EXPR_RESULT and n
's
first child is CALLpublic static boolean isControlStructure(Node n)
public static boolean isStatementBlock(Node n)
public static boolean isStatement(Node n)
public static void removeChild(Node parent, Node node)
public static void replaceDeclarationChild(Node declChild, Node newStatement)
declChild
- The name node to be replaced.newStatement
- The statement to replace with.public static boolean tryMergeBlock(Node block)
public static boolean isCallOrNew(Node node)
node
- A nodepublic static Node getFunctionBody(Node fn)
public static boolean isFunctionDeclaration(Node n)
isFunctionExpression(com.google.javascript.rhino.Node)
).public static boolean isClassDeclaration(Node n)
public static boolean isHoistedFunctionDeclaration(Node n)
isFunctionDeclaration(com.google.javascript.rhino.Node)
).public static boolean isLValue(Node n)
We treat "var x;" and "let x;" as an L-value because it's syntactically similar to "var x = undefined", even though it's technically not an L-value. But it kind of makes sense if you treat it as "assignment to 'undefined' at the top of the scope".
n
- The nodepublic static boolean isImportedName(Node n)
public static boolean isLhsByDestructuring(Node n)
public static String opToStr(Token operator)
Token
) to a string
representation.operator
- the operator's token value to convertnull
if the token value is
not an operatorpublic static boolean containsType(Node node, Token type)
public static Node newQName(AbstractCompiler compiler, String name)
name
- A qualified name (e.g. "foo" or "foo.bar.baz")public static Node newPropertyAccess(AbstractCompiler compiler, Node context, String name)
context
tree.public static Node newQNameDeclaration(AbstractCompiler compiler, String name, Node value, JSDocInfo info)
name
- A qualified name (e.g. "foo" or "foo.bar.baz")public static boolean isValidQualifiedName(CompilerOptions.LanguageMode mode, String name)
public static boolean isPrototypePropertyDeclaration(Node n)
true
if the node an assignment to a prototype property of
some constructor.public static boolean has(Node node, com.google.common.base.Predicate<Node> pred, com.google.common.base.Predicate<Node> traverseChildrenPred)
public static int getCount(Node n, com.google.common.base.Predicate<Node> pred, com.google.common.base.Predicate<Node> traverseChildrenPred)
public static void visitPreOrder(Node node, NodeUtil.Visitor visitor)
public static void visitPreOrder(Node node, NodeUtil.Visitor visitor, com.google.common.base.Predicate<Node> traverseChildrenPred)
public static void visitPostOrder(Node node, NodeUtil.Visitor visitor, com.google.common.base.Predicate<Node> traverseChildrenPred)
public static Node getFunctionParameters(Node fnNode)
fnNode
- The function.public static String getSourceName(Node n)
n
- The node.public static StaticSourceFile getSourceFile(Node n)
n
- The node.public static InputId getInputId(Node n)
n
- The node.@Nullable public static JSDocInfo getBestJSDocInfo(Node n)
public static Map<Node,Node> mapMainToClone(Node main, Node clone)
public static void verifyScopeChanges(Map<Node,Node> map, Node main, boolean verifyUnchangedNodes)
public static boolean isTopLevel(Node n)
Copyright © 2009-2017 Google. All Rights Reserved.