本文整理汇总了Java中com.google.javascript.rhino.Node.getParent方法的典型用法代码示例。如果您正苦于以下问题:Java Node.getParent方法的具体用法?Java Node.getParent怎么用?Java Node.getParent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.Node
的用法示例。
在下文中一共展示了Node.getParent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tryMergeBlock
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Merge a block with its parent block.
* @return Whether the block was removed.
*/
static boolean tryMergeBlock(Node block) {
Preconditions.checkState(block.getType() == Token.BLOCK);
Node parent = block.getParent();
// Try to remove the block if its parent is a block/script or if its
// parent is label and it has exactly one child.
if (NodeUtil.isStatementBlock(parent)) {
Node previous = block;
while (block.hasChildren()) {
Node child = block.removeFirstChild();
parent.addChildAfter(child, previous);
previous = child;
}
parent.removeChild(block);
return true;
} else if (parent.getType() == Token.LABEL && block.hasOneChild()) {
parent.replaceChild(block, block.removeFirstChild());
return true;
} else {
return false;
}
}
示例2: isNameAssignedTo
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @return Whether name is assigned in the expression rooted at node.
*/
private boolean isNameAssignedTo(String name, Node node) {
for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
if (isNameAssignedTo(name, c)) {
return true;
}
}
if (node.getType() == Token.NAME) {
Node parent = node.getParent();
if (parent.getType() == Token.ASSIGN && parent.getFirstChild() == node) {
if (name.equals(node.getString())) {
return true;
}
}
}
return false;
}
示例3: checkNodesMatch
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Check that the two nodes have the same relative position in the tree.
*/
private boolean checkNodesMatch(Node nodeA, Node nodeB) {
Node currentA = nodeA;
Node currentB = nodeB;
while (currentA != null && currentB != null) {
if (currentA.getType() != currentB.getType() ||
!currentA.isEquivalentTo(currentB)) {
return false;
}
currentA = currentA.getParent();
currentB = currentB.getParent();
}
return currentA == null && currentB == null;
}
示例4: shouldTraverse
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public boolean shouldTraverse(NodeTraversal traversal,
Node node,
Node parent) {
// Functions need to be processed as part of pre-traversal so an
// entry for the enclosing function exists in the
// FunctionInformation map when processing assignments and calls
// inside visit.
if (NodeUtil.isFunction(node)) {
Node gramp = parent.getParent();
visitFunction(traversal, node, parent, gramp);
}
return true;
}
示例5: processForTesting
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/** Main entry point of this phase for testing code. */
public Scope processForTesting(Node externsRoot, Node jsRoot) {
Preconditions.checkState(scopeCreator == null);
Preconditions.checkState(topScope == null);
Preconditions.checkState(jsRoot.getParent() != null);
Node externsAndJsRoot = jsRoot.getParent();
scopeCreator = new MemoizedScopeCreator(new TypedScopeCreator(compiler));
topScope = scopeCreator.createScope(externsAndJsRoot, null);
TypeInferencePass inference = new TypeInferencePass(compiler,
reverseInterpreter, topScope, scopeCreator);
inference.process(externsRoot, jsRoot);
process(externsRoot, jsRoot);
return topScope;
}
示例6: isVariableStillLiveWithinExpression
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Given a variable, node n in the tree and a sub-tree denoted by exprRoot as
* the root, this function returns true if there exists a read of that
* variable before a write to that variable that is on the right side of n.
*
* For example, suppose the node is x = 1:
*
* y = 1, x = 1; // false, there is no reads at all.
* y = 1, x = 1, print(x) // true, there is a read right of n.
* y = 1, x = 1, x = 2, print(x) // false, there is a read right of n but
* // it is after a write.
*
* @param n The current node we should look at.
* @param exprRoot The node
*/
private boolean isVariableStillLiveWithinExpression(
Node n, Node exprRoot, String variable) {
while (n != exprRoot) {
for(Node sibling = n.getNext(); sibling != null;
sibling = sibling.getNext()) {
if (!ControlFlowGraph.isEnteringNewCfgNode(sibling)) {
VariableLiveness state = readVariableBeforeKilling(sibling, variable);
// If we see a READ or KILL there is no need to continue.
if (state == VariableLiveness.READ) {
return true;
} else if (state == VariableLiveness.KILL) {
return false;
}
}
}
n = n.getParent();
}
return false;
}
示例7: maybeEliminateAssignmentByLvalueName
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Eliminates an assignment if the lvalue is:
* - A field name that's a strip name
* - A qualified name that begins with a strip type
*
* @param t The traversal
* @param n An ASSIGN node
* @param parent {@code n}'s parent
*/
void maybeEliminateAssignmentByLvalueName(NodeTraversal t, Node n,
Node parent) {
// ASSIGN
// lvalue
// rvalue
Node lvalue = n.getFirstChild();
if (nameEndsWithFieldNameToStrip(lvalue) ||
qualifiedNameBeginsWithStripType(lvalue)) {
if (NodeUtil.isExpressionNode(parent)) {
Node gramps = parent.getParent();
replaceWithEmpty(parent, gramps);
} else {
replaceWithEmpty(n, parent);
}
compiler.reportCodeChange();
}
}
示例8: updateObjLitOrFunctionDeclarationAtVarNode
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Updates the first initialization (a.k.a "declaration") of a global name
* that occurs at a VAR node. See comment for
* {@link #updateObjLitOrFunctionDeclaration}.
*
* @param n An object representing a global name (e.g. "a")
*/
private void updateObjLitOrFunctionDeclarationAtVarNode(Name n) {
Ref ref = n.declaration;
String name = ref.node.getString();
Node rvalue = ref.node.getFirstChild();
Node varNode = ref.node.getParent();
Node gramps = varNode.getParent();
boolean isObjLit = rvalue.getType() == Token.OBJECTLIT;
int numChanges = 0;
if (isObjLit) {
boolean discardKeys = n.aliasingGets == 0;
numChanges += declareVarsForObjLitValues(
n, name, rvalue, varNode, gramps.getChildBefore(varNode),
gramps, discardKeys);
}
numChanges += addStubsForUndeclaredProperties(n, name, gramps, varNode);
if (isObjLit && n.canEliminate()) {
varNode.removeChild(ref.node);
if (!varNode.hasChildren()) {
gramps.removeChild(varNode);
}
numChanges++;
// Clear out the object reference, since we've eliminated it from the
// parse tree.
ref.node = null;
}
if (numChanges > 0) {
compiler.reportCodeChange();
}
}
示例9: getAssignedValue
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* For an assignment or variable declaration get the assigned value.
* @return The value node representing the new value.
*/
static Node getAssignedValue(Node n) {
Preconditions.checkState(isName(n));
Node parent = n.getParent();
if (isVar(parent)) {
return n.getFirstChild();
} else if (isAssign(parent) && parent.getFirstChild() == n) {
return n.getNext();
} else {
return null;
}
}
示例10: tryRemoveRepeatedStatements
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Try to remove duplicate statements from IF blocks. For example:
*
* if (a) {
* x = 1;
* return true;
* } else {
* x = 2;
* return true;
* }
*
* becomes:
*
* if (a) {
* x = 1;
* } else {
* x = 2;
* }
* return true;
*
* @param n The IF node to examine.
*/
private void tryRemoveRepeatedStatements(NodeTraversal t, Node n) {
Preconditions.checkState(n.getType() == Token.IF);
Node parent = n.getParent();
if (!NodeUtil.isStatementBlock(parent)) {
// If the immediate parent is something like a label, we
// can't move the statement, so bail.
return;
}
Node cond = n.getFirstChild();
Node trueBranch = cond.getNext();
Node falseBranch = trueBranch.getNext();
Preconditions.checkNotNull(trueBranch);
Preconditions.checkNotNull(falseBranch);
while (true) {
Node lastTrue = trueBranch.getLastChild();
Node lastFalse = falseBranch.getLastChild();
if (lastTrue == null || lastFalse == null
|| !compiler.areNodesEqualForInlining(lastTrue, lastFalse)) {
break;
}
lastTrue.detachFromParent();
lastFalse.detachFromParent();
parent.addChildAfter(lastTrue, n);
t.getCompiler().reportCodeChange();
}
}
示例11: remove
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void remove() {
Node var = name.getParent();
Preconditions.checkState(var.getFirstChild() == var.getLastChild(),
"AST should be normalized first");
Node parent = var.getParent();
Node rValue = name.removeFirstChild();
Preconditions.checkState(parent.getType() != Token.FOR);
parent.replaceChild(var, NodeUtil.newExpr(rValue));
}
示例12: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
public void visit(Node n) {
if (n.getType() == Token.NAME) {
Node parent = n.getParent();
if (parent != null && parent.getType() == Token.VAR) {
String name = n.getString();
if (!vars.containsKey(name)) {
vars.put(name, n);
}
}
}
}
示例13: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
JSDocInfo docInfo = n.getJSDocInfo();
if (docInfo != null && docInfo.isExport()) {
String export = null;
GenerateNodeContext context = null;
switch (n.getType()) {
case Token.FUNCTION:
if (parent.getType() == Token.SCRIPT) {
export = NodeUtil.getFunctionName(n, parent);
context = new GenerateNodeContext(n, parent, n);
}
break;
case Token.ASSIGN:
Node grandparent = parent.getParent();
if (grandparent != null && grandparent.getType() == Token.SCRIPT &&
parent.getType() == Token.EXPR_RESULT &&
n.getLastChild().getType() != Token.ASSIGN) {
export = n.getFirstChild().getQualifiedName();
context = new GenerateNodeContext(n, grandparent, parent);
}
break;
case Token.VAR:
if (parent.getType() == Token.SCRIPT) {
if (n.getFirstChild().hasChildren() &&
n.getFirstChild().getFirstChild().getType() != Token.ASSIGN) {
export = n.getFirstChild().getString();
context = new GenerateNodeContext(n, parent, n);
}
}
}
if (export != null) {
exports.put(export, context);
} else {
compiler.report(JSError.make(t, n, NON_GLOBAL_ERROR));
}
}
}
示例14: canAccessDeprecatedTypes
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Returns whether it's currently ok to access deprecated names and
* properties.
*
* There are 3 exceptions when we're allowed to use a deprecated
* type or property:
* 1) When we're in a deprecated function.
* 2) When we're in a deprecated class.
* 3) When we're in a static method of a deprecated class.
*/
private boolean canAccessDeprecatedTypes(NodeTraversal t) {
Node scopeRoot = t.getScopeRoot();
Node scopeRootParent = scopeRoot.getParent();
return
// Case #1
(deprecatedDepth > 0) ||
// Case #2
(getTypeDeprecationInfo(t.getScope().getTypeOfThis()) != null) ||
// Case #3
(scopeRootParent != null && scopeRootParent.getType() == Token.ASSIGN &&
getTypeDeprecationInfo(
getClassOfMethod(scopeRoot, scopeRootParent)) != null);
}
示例15: isLhsOfForInExpression
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
private static boolean isLhsOfForInExpression(Node n) {
Node parent = n.getParent();
if (parent.getType() == Token.VAR) {
return isLhsOfForInExpression(parent);
}
return NodeUtil.isForIn(parent) && parent.getFirstChild() == n;
}