本文整理汇总了Java中com.google.javascript.rhino.Node.addChildBefore方法的典型用法代码示例。如果您正苦于以下问题:Java Node.addChildBefore方法的具体用法?Java Node.addChildBefore怎么用?Java Node.addChildBefore使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.Node
的用法示例。
在下文中一共展示了Node.addChildBefore方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: moveExpression
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Extract the specified expression from its parent expression.
* @see #canExposeExpression
*/
void moveExpression(Node expression) {
String resultName = getTempValueName(); // Should this be constant?
Node injectionPoint = findInjectionPoint(expression);
Preconditions.checkNotNull(injectionPoint);
Node injectionPointParent = injectionPoint.getParent();
Preconditions.checkNotNull(injectionPointParent);
Preconditions.checkState(NodeUtil.isStatementBlock(injectionPointParent));
// Replace the expression with a reference to the new name.
Node expressionParent = expression.getParent();
expressionParent.replaceChild(
expression, Node.newString(Token.NAME, resultName));
// Re-add the expression at the appropriate place.
Node newExpressionRoot = NodeUtil.newVarNode(resultName, expression);
injectionPointParent.addChildBefore(newExpressionRoot, injectionPoint);
compiler.reportCodeChange();
}
示例2: splitVarDeclarations
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Split a var node such as:
* var a, b;
* into individual statements:
* var a;
* var b;
* @param n The whose children we should inspect.
*/
private void splitVarDeclarations(Node n) {
for (Node next, c = n.getFirstChild(); c != null; c = next) {
next = c.getNext();
if (c.getType() == Token.VAR) {
if (assertOnChange && !c.hasChildren()) {
throw new IllegalStateException("Empty VAR node.");
}
while (c.getFirstChild() != c.getLastChild()) {
Node name = c.getFirstChild();
c.removeChild(name);
Node newVar = new Node(
Token.VAR, name, n.getLineno(), n.getCharno());
n.addChildBefore(newVar, c);
reportCodeChange("VAR with multiple children");
}
}
}
}
示例3: extractExpression
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @param expr The expression to extract.
* @param injectionPoint The node before which to added the extracted
* expression.
* @return The extract statement node.
*/
private Node extractExpression(Node expr, Node injectionPoint) {
Node parent = expr.getParent();
// The temp value is known to be constant.
String tempName = getTempConstantValueName();
// Replace the expression with the temporary name.
Node replacementValueNode = Node.newString(Token.NAME, tempName);
parent.replaceChild(expr, replacementValueNode);
// Re-add the expression in the declaration of the temporary name.
Node tempNameNode = Node.newString(Token.NAME, tempName);
tempNameNode.addChildrenToBack(expr);
Node tempVarNode = new Node(Token.VAR, tempNameNode);
Node injectionPointParent = injectionPoint.getParent();
injectionPointParent.addChildBefore(tempVarNode, injectionPoint);
// If it is ASSIGN_XXX we need to assign it back to the original value.
// Note that calling the temp constant is a lie in this case, but we do know
// that it is not modified until after the exposed expression.
if (NodeUtil.isAssignmentOp(parent) && !NodeUtil.isAssign(parent)) {
Node gParent = parent.getParent();
Node assignBack = new Node(Token.ASSIGN,
expr.cloneTree(),
tempNameNode.cloneNode());
if (NodeUtil.isExpressionNode(gParent)) {
gParent.getParent().addChildAfter(
NodeUtil.newExpr(assignBack), gParent);
} else {
// TODO(user): Use comma here sucks. We might close some accuracy
// in flow sensitive passes but as far as I know it is unavoidable.
Node comma = new Node(Token.COMMA);
gParent.replaceChild(parent, comma);
comma.addChildrenToFront(assignBack);
comma.addChildrenToFront(parent);
}
}
return tempVarNode;
}
示例4: replaceReturnWithBreak
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Replace the 'return' statement with its child expression.
* "return foo()" becomes "{foo(); break;}" or
* "{resultName = foo(); break;}"
* "return" becomes {break;} or "{resultName = void 0;break;}".
*/
private static Node replaceReturnWithBreak(Node current, Node parent,
String resultName, String labelName) {
if (current.getType() == Token.FUNCTION
|| current.getType() == Token.EXPR_RESULT) {
// Don't recurse into functions definitions, and expressions can't
// contain RETURN nodes.
return current;
}
if (current.getType() == Token.RETURN) {
Preconditions.checkState(NodeUtil.isStatementBlock(parent));
Node resultNode = getReplacementReturnStatement(current, resultName);
Node name = Node.newString(Token.NAME, labelName);
Node breakNode = new Node(Token.BREAK, name);
// Replace the node in parent, and reset current to the first new child.
parent.replaceChild(current, breakNode);
if (resultNode != null) {
parent.addChildBefore(resultNode, breakNode);
}
current = breakNode;
} else {
for (Node c = current.getFirstChild(); c != null; c = c.getNext()) {
// c may be replaced.
c = replaceReturnWithBreak(c, current, resultName, labelName);
}
}
return current;
}
示例5: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getType()) {
case Token.WHILE:
if (CONVERT_WHILE_TO_FOR) {
Node expr = n.getFirstChild();
n.setType(Token.FOR);
n.addChildBefore(new Node(Token.EMPTY), expr);
n.addChildAfter(new Node(Token.EMPTY), expr);
reportCodeChange("WHILE node");
}
break;
}
}
示例6: extractForInitializer
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Bring the initializers out of FOR loops. These need to be placed
* before any associated LABEL nodes. This needs to be done from the top
* level label first so this is called as a pre-order callback (from
* shouldTraverse).
*
* @param n The node to inspect.
* @param before The node to insert the initializer before.
* @param beforeParent The parent of the node before which the initializer
* will be inserted.
*/
private void extractForInitializer(
Node n, Node before, Node beforeParent) {
for (Node next, c = n.getFirstChild(); c != null; c = next) {
next = c.getNext();
Node insertBefore = (before == null) ? c : before;
Node insertBeforeParent = (before == null) ? n : beforeParent;
switch (c.getType()) {
case Token.LABEL:
extractForInitializer(c, insertBefore, insertBeforeParent);
break;
case Token.FOR:
if (!NodeUtil.isForIn(c)
&& c.getFirstChild().getType() != Token.EMPTY) {
Node init = c.getFirstChild();
c.replaceChild(init, new Node(Token.EMPTY));
Node newStatement;
// Only VAR statements, and expressions are allowed,
// but are handled differently.
if (init.getType() == Token.VAR) {
newStatement = init;
} else {
newStatement = NodeUtil.newExpr(init);
}
insertBeforeParent.addChildBefore(newStatement, insertBefore);
reportCodeChange("FOR initializer");
}
break;
}
}
}
示例7: updateSimpleDeclaration
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Updates the initial assignment to a collapsible property at global scope
* by changing it to a variable declaration (e.g. a.b = 1 -> var a$b = 1).
* The property's value may either be a primitive or an object literal or
* function whose properties aren't collapsible.
*
* @param alias The flattened property name (e.g. "a$b")
* @param refName The name for the reference being updated.
* @param ref An object containing information about the assignment getting
* updated
*/
private void updateSimpleDeclaration(String alias, Name refName, Ref ref) {
Node rvalue = ref.node.getNext();
Node parent = ref.node.getParent();
Node gramps = parent.getParent();
Node greatGramps = gramps.getParent();
Node greatGreatGramps = greatGramps.getParent();
// Create the new alias node.
Node nameNode = NodeUtil.newName(alias, gramps.getFirstChild(),
refName.fullName());
NodeUtil.copyNameAnnotations(ref.node.getLastChild(), nameNode);
if (gramps.getType() == Token.EXPR_RESULT) {
// BEFORE: a.b.c = ...;
// exprstmt
// assign
// getprop
// getprop
// name a
// string b
// string c
// NODE
// AFTER: var a$b$c = ...;
// var
// name a$b$c
// NODE
// Remove the rvalue (NODE).
parent.removeChild(rvalue);
nameNode.addChildToFront(rvalue);
Node varNode = new Node(Token.VAR, nameNode);
greatGramps.replaceChild(gramps, varNode);
} else {
// This must be a complex assignment.
Preconditions.checkNotNull(ref.getTwin());
// BEFORE:
// ... (x.y = 3);
//
// AFTER:
// var x$y;
// ... (x$y = 3);
Node current = gramps;
Node currentParent = gramps.getParent();
for (; currentParent.getType() != Token.SCRIPT &&
currentParent.getType() != Token.BLOCK;
current = currentParent,
currentParent = currentParent.getParent()) {}
// Create a stub variable declaration right
// before the current statement.
Node stubVar = new Node(Token.VAR, nameNode.cloneTree());
currentParent.addChildBefore(stubVar, current);
parent.replaceChild(ref.node, nameNode);
}
compiler.reportCodeChange();
}