public final class NodeUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
(package private) static class |
NodeUtil.MatchDeclaration
A predicate for matching var or function declarations.
|
private static class |
NodeUtil.MatchNameNode
A predicate for matching name nodes with the specified node.
|
(package private) static class |
NodeUtil.MatchNodeType
A predicate for matching nodes with the specified type.
|
private static class |
NodeUtil.MatchNotClass
A predicate for matching anything except class nodes.
|
private static class |
NodeUtil.MatchNotFunction
A predicate for matching anything except function nodes.
|
(package private) static class |
NodeUtil.MatchShallowStatement
A predicate for matching statements without exiting the current scope.
|
(package private) static class |
NodeUtil.ValueType |
private static class |
NodeUtil.VarCollector |
static interface |
NodeUtil.Visitor
Interface for use with the visit method.
|
Modifier and Type | Field and Description |
---|---|
private static java.util.Set<java.lang.String> |
BUILTIN_FUNCTIONS_WITHOUT_SIDEEFFECTS |
private static java.util.Set<java.lang.String> |
CONSTRUCTORS_WITHOUT_SIDE_EFFECTS
the set of builtin constructors that don't have side effects.
|
(package private) static com.google.common.base.Predicate<Node> |
IMMUTABLE_PREDICATE |
private static com.google.common.base.Predicate<Node> |
isStatement |
(package private) static java.lang.String |
JSC_PROPERTY_NAME_FN |
(package private) static char |
LARGEST_BASIC_LATIN |
(package private) static com.google.common.base.Predicate<Node> |
MATCH_NOT_CLASS |
(package private) static com.google.common.base.Predicate<Node> |
MATCH_NOT_FUNCTION |
(package private) static com.google.common.base.Predicate<Node> |
MATCH_NOT_THIS_BINDING |
(package private) static long |
MAX_POSITIVE_INTEGER_NUMBER |
private static java.util.Set<java.lang.String> |
OBJECT_METHODS_WITHOUT_SIDEEFFECTS |
private static java.util.Set<java.lang.String> |
REGEXP_METHODS |
private static java.util.Set<java.lang.String> |
STRING_REGEXP_METHODS |
Modifier | Constructor and Description |
---|---|
private |
NodeUtil() |
Modifier and Type | Method and Description |
---|---|
(package private) static boolean |
allArgsUnescapedLocal(Node callOrNew) |
(package private) static boolean |
allResultsMatch(Node n,
com.google.common.base.Predicate<Node> p)
Apply the supplied predicate against
all possible result Nodes of the expression.
|
(package private) static NodeUtil.ValueType |
and(NodeUtil.ValueType a,
NodeUtil.ValueType b) |
(package private) static java.lang.String |
arrayToString(Node literal) |
(package private) static Node |
booleanNode(boolean value) |
(package private) static boolean |
callHasLocalResult(Node n) |
(package private) static boolean |
canBeSideEffected(Node n) |
(package private) static boolean |
canBeSideEffected(Node n,
java.util.Set<java.lang.String> knownConstants,
Scope scope) |
private static boolean |
checkForStateChangeHelper(Node n,
boolean checkForNewObjects,
AbstractCompiler compiler)
Returns true if some node in n's subtree changes application state.
|
(package private) static boolean |
constructorCallHasSideEffects(Node callNode)
Do calls to this constructor have side effects?
|
(package private) static boolean |
containsFunction(Node n)
Determines if the given node contains a function statement or function
expression.
|
static boolean |
containsType(Node node,
int type) |
(package private) static boolean |
containsType(Node node,
int type,
com.google.common.base.Predicate<Node> traverseChildrenPred) |
(package private) static void |
copyNameAnnotations(Node source,
Node destination)
Copy any annotations that follow a named value.
|
(package private) static int |
countAstSizeUpToLimit(Node n,
int limit) |
(package private) static JSDocInfo |
createConstantJsDoc() |
(package private) static boolean |
createsBlockScope(Node n)
A block scope is created by a non-synthetic block node, a for loop node,
or a for-of loop node.
|
(package private) static boolean |
evaluatesToLocalValue(Node value) |
(package private) static boolean |
evaluatesToLocalValue(Node value,
com.google.common.base.Predicate<Node> locals) |
(package private) static boolean |
functionCallHasSideEffects(Node callNode)
Returns true if calls to this function have side effects.
|
(package private) static boolean |
functionCallHasSideEffects(Node callNode,
AbstractCompiler compiler)
Returns true if calls to this function have side effects.
|
(package private) static boolean |
functionHasInlineJsdocs(Node fn) |
private static Node |
getAddingRoot(Node n)
Gets a Node at the top of the current scope where we can add new var
declarations as children.
|
(package private) static Node |
getArgumentForCallOrNew(Node call,
int index)
Given the new or call, this returns the nth
argument of the call or null if no such argument exists.
|
(package private) static Node |
getArgumentForFunction(Node function,
int index)
Given the function, this returns the nth
argument or null if no such parameter exists.
|
(package private) static java.lang.String |
getArrayElementStringValue(Node n)
When converting arrays to string using Array.prototype.toString or
Array.prototype.join, the rules for conversion to String are different
than converting each element individually.
|
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.
|
(package private) static Node |
getBestLValue(Node n)
Find the l-value that the given r-value is being assigned to.
|
(package private) static java.lang.String |
getBestLValueName(Node lValue)
Get the name of the given l-value node.
|
(package private) static Node |
getBestLValueOwner(Node lValue)
Get the owner of the given l-value node.
|
(package private) static Node |
getCatchBlock(Node n) |
(package private) static Node |
getConditionExpression(Node n)
Gets the condition of an ON_TRUE / ON_FALSE CFG edge.
|
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.
|
(package private) static Node |
getEnclosingClass(Node n)
Finds the class containing the given node.
|
(package private) static Node |
getEnclosingClassMemberFunction(Node n)
Finds the class member function 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 |
getEnclosingScript(Node n)
Finds the script containing the given node.
|
static Node |
getEnclosingStatement(Node n) |
(package private) static Node |
getEnclosingType(Node n,
int type)
Gets the closest ancestor to the given node of the provided type.
|
(package private) static Node |
getFirstComputedPropMatchingKey(Node objlit,
Node key) |
(package private) static Node |
getFirstPropMatchingKey(Node objlit,
java.lang.String keyName) |
(package private) static Node |
getFunctionBody(Node fn)
Return a BLOCK node for the given FUNCTION node.
|
static Node |
getFunctionParameters(Node fnNode) |
(package private) static TernaryValue |
getImpureBooleanValue(Node n)
Gets the boolean value of a node that represents a expression.
|
static InputId |
getInputId(Node n) |
(package private) static int |
getInverseOperator(int type)
Returns the inverse of an operator if it is invertible.
|
(package private) static NodeUtil.ValueType |
getKnownValueType(Node n)
Apply the supplied predicate against
all possible result Nodes of the expression.
|
(package private) static Node |
getLoopCodeBlock(Node n) |
static java.lang.String |
getName(Node n) |
static Node |
getNameNode(Node n)
Gets the node of a function or class's name.
|
(package private) static int |
getNameReferenceCount(Node node,
java.lang.String name)
Finds the number of times a simple name is referenced within the node tree.
|
static java.lang.String |
getNearestFunctionName(Node n)
Gets the function's name.
|
(package private) static int |
getNodeTypeReferenceCount(Node node,
int type,
com.google.common.base.Predicate<Node> traverseChildrenPred)
Finds the number of times a type is referenced within the node tree.
|
private static Node |
getNthSibling(Node first,
int index)
Given the first sibling, this returns the nth
sibling or null if no such sibling exists.
|
static java.lang.Double |
getNumberValue(Node n) |
(package private) static java.lang.Double |
getNumberValue(Node n,
boolean useType)
Gets the value of a node as a Number, or null if it cannot be converted.
|
(package private) static java.lang.Iterable<Node> |
getObjectDefinedPropertiesKeys(Node definePropertiesCall) |
(package private) static java.lang.String |
getObjectLitKeyName(Node key)
Get the name of an object literal key.
|
(package private) static int |
getOpFromAssignmentOp(Node n) |
(package private) static Node |
getPrototypeClassName(Node qName) |
(package private) static java.lang.String |
getPrototypePropertyName(Node qName) |
(package private) static TernaryValue |
getPureBooleanValue(Node n)
Gets the boolean value of a node that represents a literal.
|
(package private) static Node |
getRootOfQualifiedName(Node qName)
Gets the root node of a qualified name.
|
(package private) static Node |
getRValueOfLValue(Node n)
Gets the r-value (or intializer) of a node returned by getBestLValue.
|
static StaticSourceFile |
getSourceFile(Node n) |
static java.lang.String |
getSourceName(Node n) |
(package private) static java.lang.Double |
getStringNumberValue(java.lang.String rawJsString) |
(package private) static java.lang.String |
getStringValue(double value) |
static java.lang.String |
getStringValue(Node n)
Gets the value of a node as a String, or null if it cannot be converted.
|
(package private) static java.util.Collection<Node> |
getVarsDeclaredInBranch(Node root)
Retrieves vars declared in the current node tree, excluding descent scopes.
|
static boolean |
has(Node node,
com.google.common.base.Predicate<Node> pred,
com.google.common.base.Predicate<Node> traverseChildrenPred) |
(package private) static boolean |
hasCatchHandler(Node n) |
(package private) static boolean |
hasFinally(Node n) |
static boolean |
isAssignmentOp(Node n) |
(package private) static boolean |
isAssignmentTarget(Node n) |
(package private) static boolean |
isAssociative(int type)
Returns true if the operator is associative.
|
(package private) static boolean |
isBleedingFunctionName(Node n)
Returns whether this is a bleeding function (an anonymous named function
that bleeds into the inner scope).
|
(package private) static boolean |
isBlockScopedDeclaration(Node n)
Is this node the name of a block-scoped declaration?
Checks for let, const, class, or block-scoped function declarations.
|
(package private) static boolean |
isBlockScopedFunctionDeclaration(Node n) |
static boolean |
isBooleanResult(Node n) |
static boolean |
isCallOrNew(Node node) |
(package private) static boolean |
isCallOrNewArgument(Node n) |
(package private) static boolean |
isCallOrNewTarget(Node n)
Returns whether this is a target of a call or new.
|
static boolean |
isClassDeclaration(Node n)
|
(package private) static boolean |
isClassExpression(Node n)
|
(package private) static boolean |
isCommutative(int type)
Returns true if the operator is commutative.
|
static boolean |
isCompoundAssignementOp(Node n) |
(package private) static boolean |
isConstantByConvention(CodingConvention convention,
Node node)
Whether the given name is constant by coding convention.
|
(package private) static boolean |
isConstantDeclaration(CodingConvention convention,
JSDocInfo info,
Node node)
Temporary function to determine if a node is constant
in the old or new world.
|
(package private) static boolean |
isConstantName(Node node)
Determines whether a variable is constant:
In Normalize, any name that matches the
CodingConvention.isConstant(String) is annotated with an
IS_CONSTANT_NAME property. |
(package private) static boolean |
isConstantVar(Node node,
Scope scope) |
static boolean |
isControlStructure(Node n)
Determines whether the given node is a FOR, DO, WHILE, WITH, or IF node.
|
(package private) static boolean |
isControlStructureCodeBlock(Node parent,
Node n)
Determines whether the given node is code node for FOR, DO,
WHILE, WITH, or IF node.
|
private static boolean |
isDeclarationParent(Node parent) |
(package private) static boolean |
isDestructuringDeclaration(Node n) |
(package private) static boolean |
isEmptyBlock(Node block)
Returns whether this a BLOCK node with no children.
|
(package private) static boolean |
isEmptyFunctionExpression(Node node)
Determines if a node is a function expression that has an empty body.
|
(package private) static boolean |
isEnhancedFor(Node n) |
(package private) static boolean |
isEnumDecl(Node n) |
private static boolean |
isEquivalentToExcludingFunctions(Node thisNode,
Node thatNode) |
(package private) static boolean |
isExecutedExactlyOnce(Node n) |
(package private) static boolean |
isExprAssign(Node n)
Is this node an assignment expression statement?
|
(package private) static boolean |
isExprCall(Node n)
Is this node a call expression statement?
|
(package private) static boolean |
isExpressionResultUsed(Node expr) |
static boolean |
isForIn(Node n) |
(package private) static boolean |
isFunctionBind(Node expr) |
(package private) static boolean |
isFunctionBlock(Node n) |
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.
|
(package private) static boolean |
isFunctionExpression(Node n)
Is a FUNCTION node an function expression? An function expression is one
that has either no name or a name that is not added to the current scope.
|
(package private) static boolean |
isFunctionObjectApply(Node callNode) |
(package private) static boolean |
isFunctionObjectCall(Node callNode) |
static boolean |
isGet(Node n)
Is this a GETPROP or GETELEM node?
|
(package private) static boolean |
isGetOrSetKey(Node node)
Determines whether a node represents an object literal get or set key
(e.g.
|
(package private) static boolean |
isGoogBind(Node n) |
private static boolean |
isGoogModuleCall(Node n) |
(package private) static boolean |
isGoogPartial(Node n) |
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.
|
(package private) static boolean |
isImmutableResult(Node n) |
(package private) static boolean |
isImmutableValue(Node n)
Returns true if this is an immutable value.
|
static boolean |
isImportedName(Node n) |
(package private) static boolean |
isImpureTrue(Node n) |
static boolean |
isInFunction(Node n) |
(package private) static boolean |
isLatin(java.lang.String s)
Test if all characters in the string are in the Basic Latin (aka ASCII)
character set - that they have UTF-16 values equal to or below 0x7f.
|
static boolean |
isLhsByDestructuring(Node n) |
static boolean |
isLiteralValue(Node n,
boolean includeFunctions)
Returns true if this is a literal value.
|
(package private) static boolean |
isLoopStructure(Node n)
Determines whether the given node is a FOR, DO, or WHILE node.
|
static boolean |
isLValue(Node n)
Determines whether this node is used as an L-value.
|
(package private) static boolean |
isModuleFile(Node n) |
static boolean |
isNameDeclaration(Node n)
Is this node a name declaration?
|
(package private) static boolean |
isNameReferenced(Node node,
java.lang.String name)
Whether a simple name is referenced within the node tree.
|
(package private) static boolean |
isNameReferenced(Node node,
java.lang.String name,
com.google.common.base.Predicate<Node> traverseChildrenPred)
Whether a simple name is referenced within the node tree.
|
(package private) static boolean |
isNamespaceDecl(Node n)
Returns true iff this node defines a namespace, such as goog or goog.math.
|
(package private) static boolean |
isNaN(Node n) |
static boolean |
isNullOrUndefined(Node n) |
static boolean |
isNumericResult(Node n)
Returns true if the result of node evaluation is always a number
|
(package private) static boolean |
isObjectCallMethod(Node callNode,
java.lang.String methodName) |
(package private) static boolean |
isObjectDefinePropertiesDefinition(Node n) |
(package private) static boolean |
isObjectLitKey(Node node)
Determines whether a node represents an object literal key
(e.g.
|
static boolean |
isObjectResult(Node n) |
(package private) static boolean |
isPropertyTest(AbstractCompiler compiler,
Node propAccess)
Determines whether this node is testing for the existence of a property.
|
(package private) static boolean |
isPrototypeAssignment(Node getProp) |
(package private) static boolean |
isPrototypeMethod(Node n) |
(package private) static boolean |
isPrototypeProperty(Node n) |
static boolean |
isPrototypePropertyDeclaration(Node n) |
(package private) static boolean |
isReferenceName(Node n) |
(package private) static boolean |
isRelationalOperation(Node n)
Returns true if the operator on this node is relational.
|
(package private) static boolean |
isSimpleOperator(Node n) |
(package private) static boolean |
isSimpleOperatorType(int type)
A "simple" operator is one whose children are expressions,
has no direct side-effects (unlike '+='), and has no
conditional aspects (unlike '||').
|
static boolean |
isStatement(Node n) |
static boolean |
isStatementBlock(Node n) |
(package private) static boolean |
isStatementParent(Node parent) |
static boolean |
isStringResult(Node n) |
(package private) static boolean |
isSwitchCase(Node n)
Whether the node is part of a switch statement.
|
(package private) static boolean |
isSymmetricOperation(Node n)
Returns true if the operator on this node is symmetric
|
private static boolean |
isToStringMethodCall(Node call) |
(package private) static boolean |
isTryCatchNodeContainer(Node n)
Whether the node is a CATCH container BLOCK.
|
(package private) static boolean |
isTryFinallyNode(Node parent,
Node child)
Whether the child node is the FINALLY block of a try.
|
(package private) static boolean |
isTypedefDecl(Node n) |
static boolean |
isUndefined(Node n) |
(package private) static boolean |
isValidCfgRoot(Node n) |
(package private) static boolean |
isValidDefineValue(Node val,
java.util.Set<java.lang.String> defines)
Determines whether the given value may be assigned to a define.
|
(package private) static boolean |
isValidPropertyName(CompilerOptions.LanguageMode mode,
java.lang.String name)
Determines whether the given name can appear on the right side of
the dot operator.
|
static boolean |
isValidQualifiedName(CompilerOptions.LanguageMode mode,
java.lang.String name)
Determines whether the given name is a valid qualified name.
|
static boolean |
isValidQualifiedName(java.lang.String name)
Deprecated.
|
(package private) static boolean |
isValidSimpleName(java.lang.String name)
Determines whether the given name is a valid variable name.
|
(package private) static boolean |
isVanillaFor(Node n) |
(package private) static boolean |
isVanillaFunction(Node n) |
(package private) static boolean |
isVarArgsFunction(Node function)
Determines if a function takes a variable number of arguments by
looking for references to the "arguments" var_args object.
|
(package private) static boolean |
isVarDeclaration(Node n)
Is this node the name of a variable being declared?
|
(package private) static boolean |
isVarOrSimpleAssignLhs(Node n,
Node parent)
Determines whether this node is strictly on the left hand side of an assign
or var initialization.
|
(package private) static boolean |
isWithinLoop(Node n) |
static java.util.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
|
(package private) static void |
maybeAddFinally(Node tryNode)
Add a finally block if one does not exist.
|
(package private) static boolean |
mayBeObect(Node n) |
(package private) static boolean |
mayBeObect(NodeUtil.ValueType type) |
(package private) static boolean |
mayBeString(Node n) |
(package private) static boolean |
mayBeString(Node n,
boolean useType)
Return if the node is possibly a string.
|
(package private) static boolean |
mayBeString(NodeUtil.ValueType type) |
(package private) static boolean |
mayEffectMutableState(Node n)
Returns true if the node may create new mutable state, or change existing
state.
|
(package private) static boolean |
mayEffectMutableState(Node n,
AbstractCompiler compiler) |
static boolean |
mayHaveSideEffects(Node n)
Returns true if the node which may have side effects when executed.
|
static boolean |
mayHaveSideEffects(Node n,
AbstractCompiler compiler) |
private static void |
mtocHelper(java.util.Map<Node,Node> map,
Node main,
Node clone) |
(package private) static Node |
newCallNode(Node callTarget,
Node... parameters)
A new CALL node with the "FREE_CALL" set based on call target.
|
(package private) static Node |
newExpr(Node child)
Creates an EXPR_RESULT.
|
(package private) static boolean |
newHasLocalResult(Node n) |
private static Node |
newName(AbstractCompiler compiler,
java.lang.String name) |
(package private) static Node |
newName(AbstractCompiler compiler,
java.lang.String name,
Node srcref)
Creates a new node representing an *existing* name, copying over the source
location information from the basis node.
|
(package private) static Node |
newName(AbstractCompiler compiler,
java.lang.String name,
Node basisNode,
java.lang.String originalName)
Creates a new node representing an *existing* name, copying over the source
location information from the basis node and assigning the given original
name to the node.
|
static Node |
newPropertyAccess(AbstractCompiler compiler,
Node context,
java.lang.String name)
Creates a property access on the
context tree. |
static Node |
newQName(AbstractCompiler compiler,
java.lang.String name)
Creates a node representing a qualified name.
|
(package private) static Node |
newQName(AbstractCompiler compiler,
java.lang.String name,
Node basisNode,
java.lang.String originalName)
Creates a node representing a qualified name, copying over the source
location information from the basis node and assigning the given original
name to the node.
|
static Node |
newQNameDeclaration(AbstractCompiler compiler,
java.lang.String name,
Node value,
JSDocInfo info)
Creates a node representing a qualified name.
|
(package private) static Node |
newUndefinedNode(Node srcReferenceNode)
Create a node for an empty result expression:
"void 0"
|
(package private) static Node |
newVarNode(java.lang.String name,
Node value)
Create a VAR node containing the given name and initial value expression.
|
(package private) static boolean |
nodeTypeMayHaveSideEffects(Node n)
Returns true if the current node's type implies side effects.
|
(package private) static boolean |
nodeTypeMayHaveSideEffects(Node n,
AbstractCompiler compiler) |
(package private) static Node |
numberNode(double value,
Node srcref) |
static java.lang.String |
opToStr(int operator)
Converts an operator's token value (see
Token ) to a string
representation. |
(package private) static java.lang.String |
opToStrNoFail(int operator)
Converts an operator's token value (see
Token ) to a string
representation or fails. |
(package private) static int |
precedence(int type) |
(package private) static void |
redeclareVarsInsideBranch(Node branch)
Given a node tree, finds all the VAR declarations in that tree that are
not in an inner scope.
|
(package private) static boolean |
referencesSuper(Node n)
Returns true if the current scope contains references to the 'super' keyword.
|
(package private) static boolean |
referencesThis(Node n)
Returns true if the shallow scope contains references to 'this' keyword
|
static void |
removeChild(Node parent,
Node node)
Safely remove children while maintaining a valid node structure.
|
(package private) static void |
setDebugInformation(Node node,
Node basisNode,
java.lang.String originalName)
Sets the debug information (source file info and original name)
on the given node.
|
(package private) static int |
toInt32(double d) |
(package private) static java.lang.String |
trimJsWhiteSpace(java.lang.String s) |
static boolean |
tryMergeBlock(Node block)
Merge a block with its parent block.
|
static void |
verifyScopeChanges(java.util.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 child matching
the predicate.
|
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.
|
static final long MAX_POSITIVE_INTEGER_NUMBER
static final java.lang.String JSC_PROPERTY_NAME_FN
static final char LARGEST_BASIC_LATIN
private static final java.util.Set<java.lang.String> CONSTRUCTORS_WITHOUT_SIDE_EFFECTS
private static final java.util.Set<java.lang.String> BUILTIN_FUNCTIONS_WITHOUT_SIDEEFFECTS
private static final java.util.Set<java.lang.String> OBJECT_METHODS_WITHOUT_SIDEEFFECTS
private static final java.util.Set<java.lang.String> REGEXP_METHODS
private static final java.util.Set<java.lang.String> STRING_REGEXP_METHODS
static final com.google.common.base.Predicate<Node> IMMUTABLE_PREDICATE
private static final com.google.common.base.Predicate<Node> isStatement
static final com.google.common.base.Predicate<Node> MATCH_NOT_FUNCTION
static final com.google.common.base.Predicate<Node> MATCH_NOT_CLASS
static final com.google.common.base.Predicate<Node> MATCH_NOT_THIS_BINDING
static boolean isImpureTrue(Node n)
static TernaryValue getImpureBooleanValue(Node n)
Boolean()
JavaScript cast function.
Note: unlike getPureBooleanValue this function does not return UNKNOWN
for expressions with side-effects.static TernaryValue getPureBooleanValue(Node n)
Boolean()
JavaScript cast function
except it return UNKNOWN for known values with side-effects, use
getImpureBooleanValue if you don't care about side-effects.public static java.lang.String getStringValue(Node n)
String()
JavaScript cast function.static java.lang.String getStringValue(double value)
static java.lang.String getArrayElementStringValue(Node n)
n
- A node that is a member of an Array.static java.lang.String arrayToString(Node literal)
public static java.lang.Double getNumberValue(Node n)
static java.lang.Double getNumberValue(Node n, boolean useType)
Number()
JavaScript cast function.n
- The node.useType
- If true, return 0.0 if the type is null, and NaN if the type is undefined.static java.lang.Double getStringNumberValue(java.lang.String rawJsString)
static java.lang.String trimJsWhiteSpace(java.lang.String s)
public static java.lang.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 java.lang.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 namestatic boolean isImmutableValue(Node n)
static boolean isSymmetricOperation(Node n)
static boolean isRelationalOperation(Node n)
static boolean isAssignmentTarget(Node n)
static int getInverseOperator(int type)
public 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.static boolean isValidDefineValue(Node val, java.util.Set<java.lang.String> defines)
val
- The value being assigned.defines
- The list of names of existing defines.static boolean isEmptyBlock(Node block)
block
- The node.static boolean isSimpleOperator(Node n)
static boolean isSimpleOperatorType(int type)
static boolean isTypedefDecl(Node n)
static boolean isEnumDecl(Node n)
static boolean isNamespaceDecl(Node n)
static Node newExpr(Node child)
child
- The expression itself.static boolean mayEffectMutableState(Node n)
static boolean mayEffectMutableState(Node n, AbstractCompiler compiler)
public static boolean mayHaveSideEffects(Node n)
public static boolean mayHaveSideEffects(Node n, AbstractCompiler compiler)
private static boolean checkForStateChangeHelper(Node n, boolean checkForNewObjects, AbstractCompiler compiler)
checkForNewObjects
is true, we assume that newly created
mutable objects (like object literals) change state. Otherwise, we assume
that they have no side effects.static boolean constructorCallHasSideEffects(Node callNode)
callNode
- - constructor call nodestatic boolean functionCallHasSideEffects(Node callNode)
callNode
- - function call nodestatic boolean functionCallHasSideEffects(Node callNode, @Nullable AbstractCompiler compiler)
callNode
- The call node to inspected.compiler
- A compiler object to provide program state changing
context information. Can be null.static boolean callHasLocalResult(Node n)
static boolean newHasLocalResult(Node n)
static boolean nodeTypeMayHaveSideEffects(Node n)
This is a non-recursive version of the may have side effects check; used to check wherever the current node's type is one of the reason's why a subtree has side effects.
static boolean nodeTypeMayHaveSideEffects(Node n, AbstractCompiler compiler)
static boolean allArgsUnescapedLocal(Node callOrNew)
static boolean canBeSideEffected(Node n)
static boolean canBeSideEffected(Node n, java.util.Set<java.lang.String> knownConstants, Scope scope)
knownConstants
- A set of names known to be constant value at
node 'n' (such as locals that are last written before n can execute).static int precedence(int type)
public static boolean isUndefined(Node n)
public static boolean isNullOrUndefined(Node n)
static boolean isImmutableResult(Node n)
static boolean allResultsMatch(Node n, com.google.common.base.Predicate<Node> p)
static NodeUtil.ValueType getKnownValueType(Node n)
static NodeUtil.ValueType and(NodeUtil.ValueType a, NodeUtil.ValueType b)
public static boolean isNumericResult(Node n)
public static boolean isBooleanResult(Node n)
public static boolean isStringResult(Node n)
public static boolean isObjectResult(Node n)
static boolean mayBeString(Node n)
static boolean mayBeString(Node n, boolean useType)
n
- The node.useType
- If true and the node has a primitive type, return true if that type is string
and false otherwise.static boolean mayBeString(NodeUtil.ValueType type)
static boolean mayBeObect(Node n)
static boolean mayBeObect(NodeUtil.ValueType type)
static boolean isAssociative(int type)
static boolean isCommutative(int type)
public static boolean isAssignmentOp(Node n)
public static boolean isCompoundAssignementOp(Node n)
static int getOpFromAssignmentOp(Node n)
static boolean containsFunction(Node n)
static Node getEnclosingType(Node n, int type)
static Node getEnclosingClassMemberFunction(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)
@Nullable static Node getFirstPropMatchingKey(Node objlit, java.lang.String keyName)
@Nullable static Node getFirstComputedPropMatchingKey(Node objlit, Node key)
key
.static boolean referencesThis(Node n)
static boolean referencesSuper(Node n)
public static boolean isGet(Node n)
static boolean isVarDeclaration(Node n)
n
- The noden
is NAME and parent
is VARstatic boolean isBlockScopedDeclaration(Node n)
n
- The noden
is the NAME of a block-scoped declaration.public static boolean isNameDeclaration(Node n)
n
- The noden
is VAR, LET or CONSTstatic boolean isDestructuringDeclaration(Node n)
n
- The noden
is a VAR, LET or CONST that contains a
destructuring pattern.public static Node getAssignedValue(Node n)
static boolean isExprAssign(Node n)
n
- The noden
is EXPR_RESULT and n
's
first child is ASSIGNstatic boolean isExprCall(Node n)
n
- The noden
is EXPR_RESULT and n
's
first child is CALLstatic boolean isVanillaFunction(Node n)
static boolean isVanillaFor(Node n)
static boolean isEnhancedFor(Node n)
public static boolean isForIn(Node n)
static boolean isLoopStructure(Node n)
static Node getLoopCodeBlock(Node n)
n
- The node to inspect.static boolean isWithinLoop(Node n)
public static boolean isControlStructure(Node n)
static boolean isControlStructureCodeBlock(Node parent, Node n)
static Node getConditionExpression(Node n)
n
- a node with an outgoing conditional CFG edgepublic static boolean isStatementBlock(Node n)
static boolean createsBlockScope(Node n)
Note: for functions, we use two separate scopes for parameters and declarations in the body. We need to make sure default parameters cannot reference var / function declarations in the body.
static boolean isValidCfgRoot(Node n)
public static boolean isStatement(Node n)
static boolean isStatementParent(Node parent)
private static boolean isDeclarationParent(Node parent)
static boolean isSwitchCase(Node n)
static boolean isReferenceName(Node n)
static boolean isTryFinallyNode(Node parent, Node child)
static boolean isTryCatchNodeContainer(Node n)
public static void removeChild(Node parent, Node node)
static void maybeAddFinally(Node tryNode)
public static boolean tryMergeBlock(Node block)
public static boolean isCallOrNew(Node node)
node
- A nodestatic 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)
).static boolean isBlockScopedFunctionDeclaration(Node n)
static boolean isFunctionBlock(Node n)
static boolean isFunctionExpression(Node n)
Some examples of function expressions:
(function () {}) (function f() {})() [ function f() {} ] var f = function f() {}; for (function f() {};;) {}
Some examples of functions that are not expressions:
function f() {} if (x); else function f() {} for (;;) { function f() {} }
n
- A nodestatic boolean isClassExpression(Node n)
n
- A nodestatic boolean isBleedingFunctionName(Node n)
static boolean isEmptyFunctionExpression(Node node)
node
- a nodestatic boolean isVarArgsFunction(Node function)
static boolean isObjectCallMethod(Node callNode, java.lang.String methodName)
static boolean isFunctionObjectCall(Node callNode)
static boolean isFunctionObjectApply(Node callNode)
static boolean isGoogBind(Node n)
static boolean isGoogPartial(Node n)
static boolean isFunctionBind(Node expr)
static boolean isVarOrSimpleAssignLhs(Node n, Node parent)
n
- The nodeparent
- Parent of the nodepublic 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)
static boolean isObjectLitKey(Node node)
node
- A nodestatic java.lang.String getObjectLitKeyName(Node key)
key
- A nodestatic boolean isGetOrSetKey(Node node)
node
- A nodepublic static java.lang.String opToStr(int operator)
Token
) to a string
representation.operator
- the operator's token value to convertnull
if the token value is
not an operatorstatic java.lang.String opToStrNoFail(int operator)
Token
) to a string
representation or fails.operator
- the operator's token value to convertjava.lang.Error
- if the token value is not an operatorstatic boolean containsType(Node node, int type, com.google.common.base.Predicate<Node> traverseChildrenPred)
public static boolean containsType(Node node, int type)
static void redeclareVarsInsideBranch(Node branch)
static void copyNameAnnotations(Node source, Node destination)
source
- destination
- private static Node getAddingRoot(Node n)
public static Node newQName(AbstractCompiler compiler, java.lang.String name)
name
- A qualified name (e.g. "foo" or "foo.bar.baz")public static Node newPropertyAccess(AbstractCompiler compiler, Node context, java.lang.String name)
context
tree.public static Node newQNameDeclaration(AbstractCompiler compiler, java.lang.String name, Node value, JSDocInfo info)
name
- A qualified name (e.g. "foo" or "foo.bar.baz")static Node newQName(AbstractCompiler compiler, java.lang.String name, Node basisNode, java.lang.String originalName)
name
- A qualified name (e.g. "foo" or "foo.bar.baz")basisNode
- The node that represents the name as currently found in
the AST.originalName
- The original name of the item being represented by the
NAME node. Used for debugging information.static Node getRootOfQualifiedName(Node qName)
static void setDebugInformation(Node node, Node basisNode, java.lang.String originalName)
node
- The node on which to set the debug information.basisNode
- The basis node from which to copy the source file info.originalName
- The original name of the node.private static Node newName(AbstractCompiler compiler, java.lang.String name)
static Node newName(AbstractCompiler compiler, java.lang.String name, Node srcref)
name
- The name for the new NAME node.srcref
- The node that represents the name as currently found in
the AST.static Node newName(AbstractCompiler compiler, java.lang.String name, Node basisNode, java.lang.String originalName)
name
- The name for the new NAME node.basisNode
- The node that represents the name as currently found in
the AST.originalName
- The original name of the item being represented by the
NAME node. Used for debugging information.static boolean isLatin(java.lang.String s)
s
- The string to be checked for ASCII-goodness.static boolean isValidSimpleName(java.lang.String name)
@Deprecated public static boolean isValidQualifiedName(java.lang.String name)
public static boolean isValidQualifiedName(CompilerOptions.LanguageMode mode, java.lang.String name)
static boolean isValidPropertyName(CompilerOptions.LanguageMode mode, java.lang.String name)
static java.util.Collection<Node> getVarsDeclaredInBranch(Node root)
static boolean isObjectDefinePropertiesDefinition(Node n)
true
if the node is a definition with Object.definePropertiesstatic java.lang.Iterable<Node> getObjectDefinedPropertiesKeys(Node definePropertiesCall)
public static boolean isPrototypePropertyDeclaration(Node n)
true
if the node an assignment to a prototype property of
some constructor.static boolean isPrototypeProperty(Node n)
static boolean isPrototypeMethod(Node n)
static boolean isPrototypeAssignment(Node getProp)
static boolean isPropertyTest(AbstractCompiler compiler, Node propAccess)
propAccess
- The GETPROP or GETELEM being tested.static Node getPrototypeClassName(Node qName)
static java.lang.String getPrototypePropertyName(Node qName)
static Node newUndefinedNode(Node srcReferenceNode)
static Node newVarNode(java.lang.String name, Node value)
static int getNodeTypeReferenceCount(Node node, int type, com.google.common.base.Predicate<Node> traverseChildrenPred)
static boolean isNameReferenced(Node node, java.lang.String name, com.google.common.base.Predicate<Node> traverseChildrenPred)
static boolean isNameReferenced(Node node, java.lang.String name)
static int getNameReferenceCount(Node node, java.lang.String name)
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, com.google.common.base.Predicate<Node> traverseChildrenPred)
public static void visitPostOrder(Node node, NodeUtil.Visitor visitor, com.google.common.base.Predicate<Node> traverseChildrenPred)
static boolean hasFinally(Node n)
static Node getCatchBlock(Node n)
static boolean hasCatchHandler(Node n)
getCatchBlock(com.google.javascript.rhino.Node)
public static Node getFunctionParameters(Node fnNode)
fnNode
- The function.static boolean isConstantName(Node node)
Determines whether a variable is constant:
CodingConvention.isConstant(String)
is annotated with an
IS_CONSTANT_NAME property.
node
- A NAME or STRING nodestatic boolean isConstantByConvention(CodingConvention convention, Node node)
static boolean isConstantDeclaration(CodingConvention convention, JSDocInfo info, Node node)
static boolean functionHasInlineJsdocs(Node fn)
public static java.lang.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.static Node newCallNode(Node callTarget, Node... parameters)
static boolean evaluatesToLocalValue(Node value)
static boolean evaluatesToLocalValue(Node value, com.google.common.base.Predicate<Node> locals)
locals
- A predicate to apply to unknown local values.private static Node getNthSibling(Node first, int index)
static Node getArgumentForFunction(Node function, int index)
static Node getArgumentForCallOrNew(Node call, int index)
static boolean isCallOrNewTarget(Node n)
static boolean isCallOrNewArgument(Node n)
private static boolean isToStringMethodCall(Node call)
@Nullable public static JSDocInfo getBestJSDocInfo(Node n)
static Node getBestLValue(Node n)
static Node getRValueOfLValue(Node n)
static Node getBestLValueOwner(@Nullable Node lValue)
static java.lang.String getBestLValueName(@Nullable Node lValue)
static boolean isExpressionResultUsed(Node expr)
static boolean isExecutedExactlyOnce(Node n)
n
- The expression to check.static Node booleanNode(boolean value)
static Node numberNode(double value, Node srcref)
static boolean isNaN(Node n)
public static java.util.Map<Node,Node> mapMainToClone(Node main, Node clone)
public static void verifyScopeChanges(java.util.Map<Node,Node> map, Node main, boolean verifyUnchangedNodes)
static int countAstSizeUpToLimit(Node n, int limit)
private static boolean isEquivalentToExcludingFunctions(Node thisNode, Node thatNode)
static JSDocInfo createConstantJsDoc()
static int toInt32(double d)
private static boolean isGoogModuleCall(Node n)
static boolean isModuleFile(Node n)