本文整理汇总了Java中com.google.javascript.rhino.Node.removeChild方法的典型用法代码示例。如果您正苦于以下问题:Java Node.removeChild方法的具体用法?Java Node.removeChild怎么用?Java Node.removeChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.Node
的用法示例。
在下文中一共展示了Node.removeChild方法的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: moveAllFollowing
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Move all the child nodes following start in srcParent to the end of
* destParent's child list.
* @param start The start point in the srcParent child list.
* @param srcParent The parent node of start.
* @param destParent The destination node.
*/
static private void moveAllFollowing(
Node start, Node srcParent, Node destParent) {
for (Node n = start.getNext(); n != null; n = start.getNext()) {
boolean isFunctionDeclaration =
NodeUtil.isFunctionDeclaration(n);
srcParent.removeChild(n);
if (isFunctionDeclaration) {
destParent.addChildToFront(n);
} else {
destParent.addChildToBack(n);
}
}
}
示例3: removeVarDeclarationsByNameOrRvalue
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Removes declarations of any variables whose names are strip names or
* whose whose rvalues are static method calls on strip types. Builds a set
* of removed variables so that all references to them can be removed.
*
* @param t The traversal
* @param n A VAR node
* @param parent {@code n}'s parent
*/
void removeVarDeclarationsByNameOrRvalue(NodeTraversal t, Node n,
Node parent) {
for (Node nameNode = n.getFirstChild(); nameNode != null;
nameNode = nameNode.getNext()) {
String name = nameNode.getString();
if (isStripName(name) ||
isCallWhoseReturnValueShouldBeStripped(nameNode.getFirstChild())) {
// Remove the NAME.
Scope scope = t.getScope();
varsToRemove.add(scope.getVar(name));
n.removeChild(nameNode);
compiler.reportCodeChange();
}
}
if (!n.hasChildren()) {
// Must also remove the VAR.
replaceWithEmpty(n, parent);
compiler.reportCodeChange();
}
}
示例4: eliminateKeysWithStripNamesFromObjLit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Eliminates any object literal keys in an object literal declaration that
* have strip names.
*
* @param t The traversal
* @param n An OBJLIT node
*/
void eliminateKeysWithStripNamesFromObjLit(NodeTraversal t, Node n) {
// OBJLIT
// key1
// value1
// ...
Node key = n.getFirstChild();
while (key != null) {
if (key.getType() == Token.STRING &&
isStripName(key.getString())) {
Node value = key.getNext();
Node next = value.getNext();
n.removeChild(key);
n.removeChild(value);
key = next;
compiler.reportCodeChange();
} else {
key = key.getNext().getNext();
}
}
}
示例5: removeUnreferencedFunctionArgs
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Removes unreferenced arguments from a function declaration.
*
* @param function The FUNCTION node
* @param fnScope The scope inside the function
*/
private void removeUnreferencedFunctionArgs(Node function, Scope fnScope) {
// Strip unreferenced args off the end of the function declaration.
Node argList = function.getFirstChild().getNext();
Node lastArg;
while ((lastArg = argList.getLastChild()) != null) {
Var var = fnScope.getVar(lastArg.getString());
if (!referenced.contains(var)) {
argList.removeChild(lastArg);
fnScope.undeclare(var);
numRemoved_++;
} else {
break;
}
}
}
示例6: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/** {@inheritDoc} */
public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getType()) {
case Token.GETELEM:
Node left = n.getFirstChild();
Node right = left.getNext();
if (right.getType() == Token.STRING &&
NodeUtil.isValidPropertyName(right.getString())) {
n.removeChild(left);
n.removeChild(right);
parent.replaceChild(n, new Node(Token.GETPROP, left, right));
compiler.reportCodeChange();
}
break;
}
}
示例7: replaceAccessor
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
private void replaceAccessor(Node getPropNode) {
/*
* BEFORE
getprop
NODE...
string length
AFTER
getelem
NODE...
name PROP_length
*/
Node propNameNode = getPropNode.getLastChild();
String propName = propNameNode.getString();
if (props.get(propName).aliasAccessor) {
Node propSrc = getPropNode.getFirstChild();
getPropNode.removeChild(propSrc);
Node newNameNode =
Node.newString(Token.NAME, getArrayNotationNameFor(propName));
Node elemNode = new Node(Token.GETELEM, propSrc, newNameNode);
replaceNode(getPropNode.getParent(), getPropNode, elemNode);
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: removeChild
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/** Safely remove children while maintaining a valid node structure. */
static void removeChild(Node parent, Node node) {
// Node parent = node.getParent();
if (isStatementBlock(parent)
|| isSwitchCase(node)
|| isTryFinallyNode(parent, node)) {
// A statement in a block can simply be removed.
parent.removeChild(node);
} else if (parent.getType() == Token.VAR) {
if (parent.hasMoreThanOneChild()) {
parent.removeChild(node);
} else {
// Remove the node from the parent, so it can be reused.
parent.removeChild(node);
// This would leave an empty VAR, remove the VAR itself.
removeChild(parent.getParent(), parent);
}
} else if (node.getType() == Token.BLOCK) {
// Simply empty the block. This maintains source location and
// "synthetic"-ness.
node.detachChildren();
} else if (parent.getType() == Token.LABEL
&& node == parent.getLastChild()) {
// Remove the node from the parent, so it can be reused.
parent.removeChild(node);
// A LABEL without children can not be referred to, remove it.
removeChild(parent.getParent(), parent);
} else if (parent.getType() == Token.FOR
&& parent.getChildCount() == 4) {
// Only Token.FOR can have an Token.EMPTY other control structure
// need something for the condition. Others need to be replaced
// or the structure removed.
parent.replaceChild(node, new Node(Token.EMPTY));
} else {
throw new IllegalStateException("Invalid attempt to remove node: " +
node.toString() + " of "+ parent.toString());
}
}
示例10: removeDeclaration
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Remove the given VAR declaration.
*/
private void removeDeclaration(Reference declaration) {
Node varNode = declaration.getParent();
varNode.removeChild(declaration.getNameNode());
// Remove var node if empty
if (!varNode.hasChildren()) {
Preconditions.checkState(varNode.getType() == Token.VAR);
Node grandparent = declaration.getGrandparent();
NodeUtil.removeChild(grandparent, varNode);
}
compiler.reportCodeChange();
}
示例11: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.getType() != Token.VAR) {
return;
}
// It is only safe to collapse anonymous functions that appear
// at top-level blocks. In other cases the difference between
// variable and function declarations can lead to problems or
// expose subtle bugs in browser implementation as function
// definitions are added to scopes before the start of execution.
Node grandparent = parent.getParent();
if (!(parent.getType() == Token.SCRIPT ||
grandparent != null &&
grandparent.getType() == Token.FUNCTION &&
parent.getType() == Token.BLOCK)) {
return;
}
// Need to store the next name in case the current name is removed from
// the linked list.
Preconditions.checkState(n.hasOneChild());
Node name = n.getFirstChild();
Node value = name.getFirstChild();
if (value != null &&
value.getType() == Token.FUNCTION &&
!isRecursiveFunction(value)) {
Node fnName = value.getFirstChild();
fnName.setString(name.getString());
NodeUtil.copyNameAnnotations(name, fnName);
name.removeChild(value);
parent.replaceChild(n, value);
compiler.reportCodeChange();
}
}
示例12: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (NodeUtil.isVarDeclaration(n)) {
if (removable.contains(n.getString())) {
parent.removeChild(n);
if (!parent.hasChildren()) {
parent.getParent().removeChild(parent);
}
}
}
}
示例13: 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.
}
}
示例14: visitLabel
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Rename or remove labels.
* @param node The label node.
* @param parent The parent of the label node.
*/
private void visitLabel(Node node, Node parent) {
Node nameNode = node.getFirstChild();
Preconditions.checkState(nameNode != null);
String name = nameNode.getString();
LabelInfo li = getLabelInfo(name);
// This is a label...
if (li.referenced) {
String newName = getNameForId(li.id);
if (!name.equals(newName)) {
// ... and it is used, give it the short name.
nameNode.setString(newName);
compiler.reportCodeChange();
}
} else {
// ... and it is not referenced, just remove it.
Node newChild = node.getLastChild();
node.removeChild(newChild);
parent.replaceChild(node, newChild);
if (newChild.getType() == Token.BLOCK) {
NodeUtil.tryMergeBlock(newChild);
}
compiler.reportCodeChange();
}
// Remove the label from the current stack of labels.
namespaceStack.peek().renameMap.remove(name);
}
示例15: replaceMutator
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Changes a.prop = b to SETPROP_prop(a, b);
*/
private void replaceMutator(Node getPropNode) {
/*
BEFORE
exprstmt 1
assign 128
getprop
NodeTree A
string prop
NODE TREE B
AFTER
exprstmt 1
call
name SETPROP_prop
NodeTree A
NODE TREE B
*/
Node propNameNode = getPropNode.getLastChild();
Node parentNode = getPropNode.getParent();
Property prop = props.get(propNameNode.getString());
if (prop.aliasMutator) {
Node propSrc = getPropNode.getFirstChild();
Node propDest = parentNode.getLastChild();
// Remove the orphaned children
getPropNode.removeChild(propSrc);
getPropNode.removeChild(propNameNode);
parentNode.removeChild(propDest);
// Create the call GETPROP_prop() node, using the old propSrc as the
// one paremeter to GETPROP_prop() call.
Node callName = Node.newString(Token.NAME,
getMutatorFor(propNameNode.getString()));
Node call = new Node(Token.CALL, callName, propSrc, propDest);
// And replace the assign statement with the new call
replaceNode(parentNode.getParent(), parentNode, call);
compiler.reportCodeChange();
}
}