/** * Removes this node from its parent. Equivalent to: * node.getParent().removeChild(); */ public final Node detachFromParent() { return detach(); }
@Override public void keepSubTree(Node original) { if (original.getParent() != null) { original.detach(); } replacements.add(original); }
private static Node maybeDetach(Node node) { if (node != null && node.getParent() != null) { node.detach(); } return node; }
/** Removes the root nodes for all the library files from the source node. */ private static void stripNonCompiledNodes(Node n, Set<String> filesToCompile) { for (Node child : n.children()) { if (!filesToCompile.contains(child.getSourceFileName())) { child.detach(); } } }
@Override public void visit(NodeTraversal t, Node n, Node parent) { if (n.isEmpty() && parent.isClassMembers()) { reportCodeChange("empty member in class", n); n.detach(); } } }
@Override public void performRemove(AbstractCompiler compiler) { compiler.reportChangeToEnclosingScope(function); function.detach(); NodeUtil.markFunctionsDeleted(function, compiler); } }
public void removeDuplicates() { for (Node n : dupDeclNodes) { Node parent = n.getParent(); if (parent != null) { n.detach(); compiler.reportChangeToEnclosingScope(parent); } } } }
private void removeIfUnnamedBreak(Node maybeBreak) { if (maybeBreak != null && maybeBreak.isBreak() && !maybeBreak.hasChildren()) { reportChangeToEnclosingScope(maybeBreak); maybeBreak.detach(); } }
private void maybeSplitMultiVar(Node rhsNode) { Node statementNode = rhsNode.getGrandparent(); if (!statementNode.isVar() || !statementNode.hasMoreThanOneChild()) { return; } Node nameNode = rhsNode.getParent(); nameNode.detach(); rhsNode.detach(); statementNode.getParent().addChildBefore(IR.var(nameNode, rhsNode), statementNode); }
protected static void fuseExpressionIntoSecondChild(Node exp, Node stmt) { Node val = stmt.getSecondChild().detach(); Node comma = fuseExpressionIntoExpression(exp, val); stmt.addChildAfter(comma, stmt.getFirstChild()); } }
private Node replaceWithCharAt(Node n, Node callTarget, Node firstArg) { // TODO(moz): Maybe correct the arity of the function type here. callTarget.getLastChild().setString("charAt"); firstArg.getNext().detach(); reportChangeToEnclosingScope(firstArg); return n; }
/** Removes any keys after the given key, and adds them in order to a new object pattern */ private static Node createNewObjectPatternFromSuccessiveKeys(Node stringKey) { Node newPattern = stringKey.getParent().cloneNode(); // copies the JSType for (Node next = stringKey.getNext(); next != null; ) { Node newKey = next; next = newKey.getNext(); newPattern.addChildToBack(newKey.detach()); } return newPattern; } }
private void visitGoogModuleGet(NodeTraversal t, Node callNode) { String requiredNamespace = callNode.getLastChild().getString(); ModuleMetadata requiredModule = moduleMetadataMap.getModulesByGoogNamespace().get(requiredNamespace); if (requiredModule == null) { // Be fault tolerant. An error should've been reported in the checks. compiler.reportChangeToChangeScope(NodeUtil.getEnclosingChangeScopeRoot(callNode)); NodeUtil.getEnclosingStatement(callNode).detach(); return; } rewriteGoogModuleGet(requiredModule, requiredNamespace, t, callNode); }
private void simplifyNamespace(AbstractCompiler compiler) { if (getRhs().isOr()) { Node objLit = getRhs().getLastChild().detach(); getRhs().replaceWith(objLit); compiler.reportChangeToEnclosingScope(getLhs()); } }
private Node tryRewriteStringValueOfCall(Node n) { Node param = n.getSecondChild(); if (NodeUtil.isUndefined(param)) { return IR.string("null"); } else if (NodeUtil.isDefinedValue(param) && !param.isArrayLit()) { // Generate String(param), let other peephole optimizations handle the rest when safe return NodeUtil.newCallNode(IR.name("String").useSourceInfoFrom(n), param.detach()); } return n; }
/** Converts a member function into a quoted string key to avoid property renaming */ private void rewriteMemberDefInObjLit(Node memberDef, Node objLit) { String name = memberDef.getString(); Node stringKey = IR.stringKey(name, memberDef.getFirstChild().detach()); objLit.replaceChild(memberDef, stringKey); stringKey.setQuotedString(); stringKey.setJSDocInfo(memberDef.getJSDocInfo()); compiler.reportChangeToEnclosingScope(objLit); }
private void handleDeclarationList(Node declarationList, Node parent) { // Normalize: "const i = 0, j = 0;" becomes "/** @const */ var i = 0; /** @const */ var j = 0;" while (declarationList.hasMoreThanOneChild()) { Node name = declarationList.getLastChild(); Node newDeclaration = IR.var(name.detach()).useSourceInfoFrom(declarationList); maybeAddConstJSDoc(declarationList, parent, name, newDeclaration); parent.addChildAfter(newDeclaration, declarationList); compiler.reportChangeToEnclosingScope(parent); } maybeAddConstJSDoc(declarationList, parent, declarationList.getFirstChild(), declarationList); declarationList.setToken(Token.VAR); }