本文整理汇总了Java中com.google.javascript.rhino.Node.getAncestors方法的典型用法代码示例。如果您正苦于以下问题:Java Node.getAncestors方法的具体用法?Java Node.getAncestors怎么用?Java Node.getAncestors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.Node
的用法示例。
在下文中一共展示了Node.getAncestors方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: hasConditionalAncestor
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* return true if the node has any form of conditional in its ancestry
* TODO(nicksantos) keep track of the conditionals in the ancestory, so
* that we don't have to recrawl it.
*/
private boolean hasConditionalAncestor(Node n) {
for (Node ancestor : n.getAncestors()) {
switch (ancestor.getType()) {
case Token.DO:
case Token.FOR:
case Token.HOOK:
case Token.IF:
case Token.SWITCH:
case Token.WHILE:
return true;
}
}
return false;
}
示例2: isCallWithinLoop
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @return Whether the specified callNode has a loop parent that
* is within the current scope.
*/
private boolean isCallWithinLoop(Node callNode) {
for (Node parent : callNode.getAncestors()) {
if (NodeUtil.isLoopStructure(parent)) {
return true;
}
if (NodeUtil.isFunction(parent)) {
break;
}
}
return false;
}
示例3: isInThrowExpression
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Is the {@link Node} currently within a 'throw' expression?
*/
private static boolean isInThrowExpression(Node n) {
// Look up the traversal stack to find a THROW node
for (Node ancestor : n.getAncestors()) {
switch (ancestor.getType()) {
case Token.THROW:
return true;
case Token.IF:
case Token.WHILE:
case Token.DO:
case Token.FOR:
case Token.SWITCH:
case Token.CASE:
case Token.DEFAULT:
case Token.BLOCK:
case Token.SCRIPT:
case Token.FUNCTION:
case Token.TRY:
case Token.CATCH:
case Token.RETURN:
case Token.EXPR_RESULT:
// early exit - these nodes types can't be within a THROW
return false;
}
}
return false;
}
示例4: findExpressionRoot
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @return The statement containing the expression. null if subExpression
* is not contain by in by a Node where inlining is known to be possible.
* For example, a WHILE node condition expression.
*/
static Node findExpressionRoot(Node subExpression) {
Node child = subExpression;
for (Node parent : child.getAncestors()) {
int parentType = parent.getType();
switch (parentType) {
// Supported expression roots:
// SWITCH and IF can have multiple children, but the CASE, DEFAULT,
// or BLOCK will be encountered first for any of the children other
// than the condition.
case Token.EXPR_RESULT:
case Token.IF:
case Token.SWITCH:
case Token.RETURN:
case Token.VAR:
Preconditions.checkState(child == parent.getFirstChild());
return parent;
// Any of these indicate an unsupported expression:
case Token.SCRIPT:
case Token.BLOCK:
case Token.LABEL:
case Token.CASE:
case Token.DEFAULT:
return null;
}
child = parent;
}
throw new IllegalStateException("Unexpected AST structure.");
}
示例5: getDependencyScope
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Gets the nearest enclosing dependency scope, or null if there isn't one.
*/
private NameInformation getDependencyScope(Node n) {
for (Node node : n.getAncestors()) {
NameInformation ref = scopes.get(node);
if (ref != null) {
return ref;
}
}
return null;
}
示例6: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.getType() != Token.CALL) {
return;
}
String callName = n.getFirstChild().getQualifiedName();
NameGenerator nameGenerator = nameGenerators.get(callName);
if (nameGenerator == null) {
return;
}
if (!t.inGlobalScope()) {
// Warn about calls not in the global scope.
compiler.report(JSError.make(t, n, NON_GLOBAL_ID_GENERATOR_CALL));
return;
}
for (Node ancestor : n.getAncestors()) {
if (NodeUtil.isControlStructure(ancestor)) {
// Warn about conditional calls.
compiler.report(JSError.make(t, n, CONDITIONAL_ID_GENERATOR_CALL));
return;
}
}
String nextName = nameGenerator.generateNextName();
parent.replaceChild(n, Node.newString(nextName));
compiler.reportCodeChange();
}
示例7: determineGetTypeForHookOrBooleanExpr
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Determines whether the result of a hook (x?y:z) or boolean expression
* (x||y) or (x&&y) is assigned to a specific global name.
*
* @param t The traversal
* @param parent The parent of the current node in the traversal. This node
* should already be known to be a HOOK, AND, or OR node.
* @param name A name that is already known to be global in the current
* scope (e.g. "a" or "a.b.c.d")
* @return The expression's get type, either {@link Ref.Type#DIRECT_GET} or
* {@link Ref.Type#ALIASING_GET}
*/
Ref.Type determineGetTypeForHookOrBooleanExpr(
NodeTraversal t, Node parent, String name) {
Node prev = parent;
for (Node anc : parent.getAncestors()) {
switch (anc.getType()) {
case Token.EXPR_RESULT:
case Token.VAR:
case Token.IF:
case Token.WHILE:
case Token.FOR:
case Token.TYPEOF:
case Token.VOID:
case Token.NOT:
case Token.BITNOT:
case Token.POS:
case Token.NEG:
return Ref.Type.DIRECT_GET;
case Token.HOOK:
if (anc.getFirstChild() == prev) {
return Ref.Type.DIRECT_GET;
}
break;
case Token.ASSIGN:
if (!name.equals(anc.getFirstChild().getQualifiedName())) {
return Ref.Type.ALIASING_GET;
}
break;
case Token.NAME: // a variable declaration
if (!name.equals(anc.getString())) {
return Ref.Type.ALIASING_GET;
}
break;
case Token.CALL:
if (anc.getFirstChild() != prev) {
return Ref.Type.ALIASING_GET;
}
break;
}
prev = anc;
}
return Ref.Type.ALIASING_GET;
}
示例8: maybeProcessDeclaration
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Determines whether the given NAME node belongs to a delcaration that
* can be moved across modules. If it is, registers it properly.
*
* There are four types of movable declarations:
* 1) var NAME = [movable object];
* 2) function NAME() {}
* 3) NAME = [movable object];
* NAME.prop = [movable object];
* NAME.prop.prop2 = [movable object];
* etc.
* 4) Class-defining function calls, like "inherits" and "mixin".
* NAME.inherits([some other name]);
* where "movable object" is a literal or a function.
*/
private boolean maybeProcessDeclaration(NodeTraversal t, Node name,
Node parent, NamedInfo info) {
Node gramps = parent.getParent();
switch (parent.getType()) {
case Token.VAR:
if (canMoveValue(name.getFirstChild())) {
return info.addDeclaration(
new Declaration(t.getModule(), name, parent, gramps));
}
return false;
case Token.FUNCTION:
if (NodeUtil.isFunctionDeclaration(parent)) {
return info.addDeclaration(
new Declaration(t.getModule(), name, parent, gramps));
}
return false;
case Token.ASSIGN:
case Token.GETPROP:
Node child = name;
// Look for assignment expressions where the name is the root
// of a qualified name on the left hand side of the assignment.
for (Node current : name.getAncestors()) {
if (current.getType() == Token.GETPROP) {
// fallthrough
} else if (current.getType() == Token.ASSIGN &&
current.getFirstChild() == child) {
Node currentParent = current.getParent();
if (NodeUtil.isExpressionNode(currentParent) &&
canMoveValue(current.getLastChild())) {
return info.addDeclaration(
new Declaration(t.getModule(), current, currentParent,
currentParent.getParent()));
}
} else {
return false;
}
child = current;
}
return false;
case Token.CALL:
if (NodeUtil.isExprCall(gramps)) {
SubclassRelationship relationship =
compiler.getCodingConvention().getClassesDefinedByCall(parent);
if (relationship != null &&
name.getString().equals(relationship.subclassName)) {
return info.addDeclaration(
new Declaration(t.getModule(), parent, gramps,
gramps.getParent()));
}
}
return false;
default:
return false;
}
}
示例9: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.getType() == Token.STRING &&
parent.getType() != Token.GETPROP &&
parent.getType() != Token.REGEXP &&
!NodeUtil.isObjectLitKey(n, parent)) {
String str = n.getString();
// "undefined" is special-cased, since it needs to be used when JS code
// is unloading and therefore variable references aren't available.
// This is because of a bug in FireFox.
if ("undefined".equals(str)) {
return;
}
if (blacklist != null && blacklist.reset(str).find()) {
return;
}
if (aliasableStrings == null || aliasableStrings.contains(str)) {
StringOccurrence occurrence = new StringOccurrence(n, parent);
StringInfo info = getOrCreateStringInfo(str);
info.occurrences.add(occurrence);
info.numOccurrences++;
if (t.inGlobalScope() || isInThrowExpression(n)) {
info.numOccurrencesInfrequentlyExecuted++;
}
if (info.numOccurrences == 1) {
info.moduleToContainDecl = t.getModule();
// Take note of the optimal place to insert the var declaration.
// We'll insert it as the previous sibling of our first ancestor
// in global scope that has a SCRIPT parent.
Node prev = n;
for (Node ancestor : n.getAncestors()) {
if (ancestor.getType() == Token.SCRIPT) {
info.parentForNewVarDecl = ancestor;
info.siblingToInsertVarDeclBefore = prev;
break;
}
prev = ancestor;
}
} else {
// Check whether the current module depends on the module containing
// the declaration.
JSModule module = t.getModule();
if (module != null &&
info.moduleToContainDecl != null &&
module != info.moduleToContainDecl &&
!moduleGraph.dependsOn(module, info.moduleToContainDecl)) {
// We need to declare this string in the deepest module in the
// module dependency graph that both of these modules depend on.
module = moduleGraph.getDeepestCommonDependency(
module, info.moduleToContainDecl);
Node varParent = moduleVarParentMap.get(module);
if (varParent == null) {
varParent = compiler.getNodeForCodeInsertion(module);
moduleVarParentMap.put(module, varParent);
}
info.moduleToContainDecl = module;
info.parentForNewVarDecl = varParent;
info.siblingToInsertVarDeclBefore = varParent.getFirstChild();
}
}
}
}
}
示例10: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
public void visit(NodeTraversal t, Node n, Node parent) {
// VOID nodes appear when there are extra semicolons at the BLOCK level.
// I've been unable to think of any cases where this indicates a bug,
// and apparently some people like keeping these semicolons around,
// so we'll allow it.
if (n.getType() == Token.EMPTY ||
n.getType() == Token.COMMA) {
return;
}
if (parent == null)
return;
int pt = parent.getType();
if (pt == Token.COMMA) {
Node gramps = parent.getParent();
if (gramps.getType() == Token.CALL &&
parent == gramps.getFirstChild()) {
// Semantically, a direct call to eval is different from an indirect
// call to an eval. See Ecma-262 S15.1.2.1. So it's ok for the first
// expression to a comma to be a no-op if it's used to indirect
// an eval.
if (n == parent.getFirstChild() &&
parent.getChildCount() == 2 &&
n.getNext().getType() == Token.NAME &&
"eval".equals(n.getNext().getString())) {
return;
}
}
if (n == parent.getLastChild()) {
for (Node an : parent.getAncestors()) {
int ancestorType = an.getType();
if (ancestorType == Token.COMMA)
continue;
if (ancestorType != Token.EXPR_RESULT &&
ancestorType != Token.BLOCK)
return;
else
break;
}
}
} else if (pt != Token.EXPR_RESULT && pt != Token.BLOCK) {
if (pt == Token.FOR && parent.getChildCount() == 4 &&
(n == parent.getFirstChild() ||
n == parent.getFirstChild().getNext().getNext())) {
// Fall through and look for warnings for the 1st and 3rd child
// of a for.
} else {
return; // it might be ok to not have a side-effect
}
}
if (NodeUtil.isSimpleOperatorType(n.getType()) ||
!NodeUtil.mayHaveSideEffects(n)) {
if (n.isQualifiedName() && n.getJSDocInfo() != null) {
// This no-op statement was there so that JSDoc information could
// be attached to the name. This check should not complain about it.
return;
} else if (NodeUtil.isExpressionNode(n)) {
// we already reported the problem when we visited the child.
return;
}
String msg = "This code lacks side-effects. Is there a bug?";
if (n.getType() == Token.STRING) {
msg = "Is there a missing '+' on the previous line?";
}
t.getCompiler().report(
JSError.make(t, n, level, USELESS_CODE_ERROR, msg));
}
}