本文整理汇总了Java中com.google.javascript.rhino.Node.removeFirstChild方法的典型用法代码示例。如果您正苦于以下问题:Java Node.removeFirstChild方法的具体用法?Java Node.removeFirstChild怎么用?Java Node.removeFirstChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.Node
的用法示例。
在下文中一共展示了Node.removeFirstChild方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: tryReduceReturn
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Reduce "return undefined" or "return void 0" to simply "return".
*/
private void tryReduceReturn(NodeTraversal t, Node n) {
Node result = n.getFirstChild();
if (result != null) {
switch (result.getType()) {
case Token.VOID:
Node operand = result.getFirstChild();
if (!NodeUtil.mayHaveSideEffects(operand)) {
n.removeFirstChild();
t.getCompiler().reportCodeChange();
}
return;
case Token.NAME:
String name = result.getString();
if (name.equals("undefined")) {
n.removeFirstChild();
t.getCompiler().reportCodeChange();
}
return;
}
}
}
示例3: tryFoldDo
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Removes DOs that always evaluate to false. This leaves the
* statements that were in the loop in a BLOCK node.
* The block will be removed in a later pass, if possible.
*/
void tryFoldDo(NodeTraversal t, Node n, Node parent) {
Preconditions.checkArgument(n.getType() == Token.DO);
Node cond = NodeUtil.getConditionExpression(n);
if (!NodeUtil.isLiteralValue(cond) || NodeUtil.getBooleanValue(cond)) {
return;
}
// TODO(johnlenz): The do-while can be turned into a label with
// named breaks and the label optimized away (maybe).
if (hasBreakOrContinue(n)) {
return;
}
Preconditions.checkState(
NodeUtil.isControlStructureCodeBlock(n, n.getFirstChild()));
Node block = n.removeFirstChild();
parent.replaceChild(n, block);
t.getCompiler().reportCodeChange();
}
示例4: inlineReturnValue
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Inline a function that fulfills the requirements of
* canInlineReferenceDirectly into the call site, replacing only the CALL
* node.
*/
private Node inlineReturnValue(Node callNode, Node fnNode) {
Node block = fnNode.getLastChild();
Node callParentNode = callNode.getParent();
// NOTE: As the normalize pass guarantees globals aren't being
// shadowed and an expression can't introduce new names, there is
// no need to check for conflicts.
// Create an argName -> expression map, checking for side effects.
Map<String, Node> argMap =
FunctionArgumentInjector.getFunctionCallParameterMap(
fnNode, callNode, this.safeNameIdSupplier);
Node newExpression;
if (!block.hasChildren()) {
newExpression = NodeUtil.newUndefinedNode();
} else {
Node returnNode = block.getFirstChild();
Preconditions.checkArgument(returnNode.getType() == Token.RETURN);
// Clone the return node first.
Node safeReturnNode = returnNode.cloneTree();
Node inlineResult = FunctionArgumentInjector.inject(
safeReturnNode, null, argMap);
Preconditions.checkArgument(safeReturnNode == inlineResult);
newExpression = safeReturnNode.removeFirstChild();
}
callParentNode.replaceChild(callNode, newExpression);
return newExpression;
}
示例5: tryMinimizeNot
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Try to minimize NOT nodes such as !(x==y).
*/
private boolean tryMinimizeNot(NodeTraversal t, Node n, Node parent) {
Node notChild = n.getFirstChild();
// negative operator of the current one : == -> != for instance.
int complementOperator;
switch (notChild.getType()) {
case Token.EQ:
complementOperator = Token.NE;
break;
case Token.NE:
complementOperator = Token.EQ;
break;
case Token.SHEQ:
complementOperator = Token.SHNE;
break;
case Token.SHNE:
complementOperator = Token.SHEQ;
break;
// GT, GE, LT, LE are not handled in this because !(x<NaN) != x>=NaN.
default:
return false;
}
Node newOperator = n.removeFirstChild();
newOperator.setType(complementOperator);
parent.replaceChild(n, newOperator);
t.getCompiler().reportCodeChange();
return true;
}
示例6: removeVarDeclaration
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Tries to remove variable declaration if the variable has been coalesced
* with another variable that has already been declared.
*/
private void removeVarDeclaration(Node name) {
Node var = name.getParent();
Node parent = var.getParent();
// Special case when we are in FOR-IN loop.
if (NodeUtil.isForIn(parent)) {
var.removeChild(name);
parent.replaceChild(var, name);
} else if (var.getChildCount() == 1) {
// The removal is easy when there is only one variable in the VAR node.
if (name.hasChildren()) {
Node value = name.removeFirstChild();
var.removeChild(name);
Node assign = new Node(Token.ASSIGN, name, value);
// We don't need to wrapped it with EXPR node if it is within a FOR.
if (parent.getType() != Token.FOR) {
assign = NodeUtil.newExpr(assign);
}
parent.replaceChild(var, assign);
} else {
// In a FOR( ; ; ) node, we must replace it with an EMPTY or else it
// becomes a FOR-IN node.
NodeUtil.removeChild(parent, var);
}
} else {
if (!name.hasChildren()) {
var.removeChild(name);
}
// We are going to leave duplicated declaration otherwise.
}
}
示例7: aliasNode
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
/** {@inheritDoc} */
protected void aliasNode(Node throwNode, Node parent) {
Node name = NodeUtil.newName(getAliasName(), throwNode, getAliasName());
Node aliasCall = new Node(Token.CALL, name, throwNode.removeFirstChild());
Node exprResult = new Node(Token.EXPR_RESULT, aliasCall);
parent.replaceChild(throwNode, exprResult);
}
示例8: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.getType() != Token.RETURN) {
return;
}
Node call = newReportFunctionExitNode();
Node returnRhs = n.removeFirstChild();
if (returnRhs != null) {
call.addChildToBack(returnRhs);
}
n.addChildToFront(call);
compiler.reportCodeChange();
}
示例9: parseHelperCode
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Parse helper code needed by a reducer.
*
* @return Helper code root. If parse fails, return null.
*/
public Node parseHelperCode(Reducer reducer) {
Node root = compiler.parseSyntheticCode(
reducer.getClass().toString() + ":helper", reducer.getHelperSource());
return (root != null) ? root.removeFirstChild() : null;
}
示例10: rewriteCallExpression
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Rewrite the call so "this" is preserved.
* a.b(c);
* becomes:
* var temp1 = a;
* var temp0 = temp1.b;
* temp0.call(temp1,c);
*
* @return The replacement node.
*/
private Node rewriteCallExpression(Node call, DecompositionState state) {
Preconditions.checkArgument(call.getType() == Token.CALL);
Node first = call.getFirstChild();
Preconditions.checkArgument(NodeUtil.isGet(first));
// Extracts the expression representing the function to call. For example:
// "a['b'].c" from "a['b'].c()"
Node getVarNode = extractExpression(
first, state.extractBeforeStatement);
state.extractBeforeStatement = getVarNode;
// Extracts the object reference to be used as "this". For example:
// "a['b']" from "a['b'].c"
Node getExprNode = getVarNode.getFirstChild().getFirstChild();
Preconditions.checkArgument(NodeUtil.isGet(getExprNode));
Node thisVarNode = extractExpression(
getExprNode.getFirstChild(), state.extractBeforeStatement);
state.extractBeforeStatement = thisVarNode;
// Rewrite the CALL expression.
Node thisNameNode = thisVarNode.getFirstChild();
Node functionNameNode = getVarNode.getFirstChild();
// CALL
// GETPROP
// functionName
// "call"
// thisName
// original-parameter1
// original-parameter2
// ...
Node newCall = new Node(Token.CALL,
new Node(Token.GETPROP,
functionNameNode.cloneNode(),
Node.newString("call")),
thisNameNode.cloneNode(), call.getLineno(), call.getCharno());
// Throw away the call name
call.removeFirstChild();
if (call.hasChildren()) {
// Add the call parameters to the new call.
newCall.addChildrenToBack(call.removeChildren());
}
// Replace the call.
Node callParent = call.getParent();
callParent.replaceChild(call, newCall);
return newCall;
}