/** * @return A detached clone of the Node, specifically excluding its children. */ public Node cloneNode() { return cloneNode(false); }
/** * @return A detached clone of the Node, specifically excluding its children. */ @CheckReturnValue public final Node cloneNode() { return cloneNode(false); }
private Node cloneWithType(Node node) { Node clone = node.cloneNode(); if (shouldAddTypesOnNewAstNodes) { clone.setJSType(node.getJSType()); } return clone; }
RecordTypePropertyDefinition(Node name) { super(IR.getprop(IR.objectlit(), name.cloneNode()), /** isExtern */ true); checkArgument(name.isString()); }
RecordTypePropertyDefinition(Node name) { super(IR.getprop(IR.objectlit(), name.cloneNode()), /** isExtern */ true); Preconditions.checkArgument(name.isString()); }
public Node cloneTree(boolean cloneTypeExprs) { Node result = cloneNode(cloneTypeExprs); for (Node n2 = getFirstChild(); n2 != null; n2 = n2.getNext()) { Node n2clone = n2.cloneTree(cloneTypeExprs); n2clone.parent = result; if (result.last != null) { result.last.next = n2clone; } if (result.first == null) { result.first = n2clone; } result.last = n2clone; } return result; }
/** * Creates a JS object that holds a map of tweakId -> default value override. */ private Node createCompilerDefaultValueOverridesVarNode( Node sourceInformationNode) { Node objNode = IR.objectlit().srcref(sourceInformationNode); for (Entry<String, Node> entry : compilerDefaultValueOverrides.entrySet()) { Node objKeyNode = IR.stringKey(entry.getKey()) .useSourceInfoIfMissingFrom(sourceInformationNode); Node objValueNode = entry.getValue().cloneNode() .useSourceInfoIfMissingFrom(sourceInformationNode); objKeyNode.addChildToBack(objValueNode); objNode.addChildToBack(objKeyNode); } return objNode; }
/** * Creates a JS object that holds a map of tweakId -> default value override. */ private Node createCompilerDefaultValueOverridesVarNode( Node sourceInformationNode) { Node objNode = IR.objectlit().srcref(sourceInformationNode); for (Entry<String, Node> entry : compilerDefaultValueOverrides.entrySet()) { Node objKeyNode = IR.stringKey(entry.getKey()) .useSourceInfoIfMissingFrom(sourceInformationNode); Node objValueNode = entry.getValue().cloneNode() .useSourceInfoIfMissingFrom(sourceInformationNode); objKeyNode.addChildToBack(objValueNode); objNode.addChildToBack(objKeyNode); } return objNode; }
/** 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; } }
/** * Replaces reference to <code>arguments</code> with <code>$jscomp$generator$arguments * </code>. */ void visitArguments(Node n) { Node newArguments = context.getScopedName(GENERATOR_ARGUMENTS).useSourceInfoFrom(n); n.replaceWith(newArguments); if (!argumentsReferenceFound) { Node var = IR.var(newArguments.cloneNode(), n).useSourceInfoFrom(newGeneratorHoistBlock); hoistNode(var); argumentsReferenceFound = true; } }
/** Returns an assign replacing the equivalent var or let declaration. */ private static Node varToAssign(Node var) { Node assign = IR.assign(var.getFirstChild().cloneNode(), var.getFirstChild().removeFirstChild()); return IR.exprResult(assign).useSourceInfoIfMissingFromForTree(var); } }
private static List<MemberDefinition> objectLitToList( Node objlit) { List<MemberDefinition> result = new ArrayList<>(); for (Node keyNode : objlit.children()) { Node name = keyNode; // The span of a member function def is the whole function. The NAME node should be the // first-first child, which will have a span for just the name of the function. if (keyNode.isMemberFunctionDef()) { name = keyNode.getFirstFirstChild().cloneNode(); name.setString(keyNode.getString()); } result.add( new MemberDefinition( NodeUtil.getBestJSDocInfo(keyNode), name, keyNode.removeFirstChild())); } objlit.detachChildren(); return result; }
/** * @return An assign replacing the equivalent var or let declaration. */ private static Node varToAssign(Node var) { Node assign = IR.assign( var.getFirstChild().cloneNode(), var.getFirstChild().removeFirstChild()); return IR.exprResult(assign).useSourceInfoFromForTree(var); } }
/** Replaces reference to <code>this</code> with <code>$jscomp$generator$this</code>. */ void visitThis(Node n) { Node newThis = withType(context.getScopedName(GENERATOR_THIS), n.getJSType()); n.replaceWith(newThis); if (!thisReferenceFound) { Node var = IR.var(newThis.cloneNode().useSourceInfoFrom(n), n) .useSourceInfoFrom(newGeneratorHoistBlock); hoistNode(var); thisReferenceFound = true; } }
Node processPropertyNameAssignment(PropertyNameAssignmentTree tree) { Node key = processObjectLitKeyAsString(tree.name); key.setToken(Token.STRING_KEY); if (tree.value != null) { key.addChildToFront(transform(tree.value)); } else { Node value = key.cloneNode(); key.setShorthandProperty(true); value.setToken(Token.NAME); key.addChildToFront(value); } return key; }
private void visitExport(NodeTraversal t, Node n, Node parent) { if (currNamespace != null) { replaceWithNodes(t, n, n.children()); } else if (n.hasMoreThanOneChild()) { Node insertPoint = n; for (Node c = n.getSecondChild(); c != null; c = c.getNext()) { Node toAdd; if (!c.isExprResult()) { toAdd = n.cloneNode(); toAdd.addChildToFront(c.detach()); } else { toAdd = c.detach(); } parent.addChildAfter(toAdd, insertPoint); insertPoint = toAdd; } t.reportCodeChange(); } }
private void visitExportFrom(NodeTraversal t, Node export, Node parent) { // export {x, y as z} from 'moduleIdentifier'; Node moduleIdentifier = export.getLastChild(); Node importNode = IR.importNode(IR.empty(), IR.empty(), moduleIdentifier.cloneNode()); importNode.useSourceInfoFrom(export); parent.addChildBefore(importNode, export); visit(t, importNode, parent); String moduleName = getVarNameOfImport(moduleIdentifier.getString()); for (Node exportSpec : export.getFirstChild().children()) { exportedNameToLocalQName.put( exportSpec.getLastChild().getString(), new LocalQName(moduleName + "." + exportSpec.getFirstChild().getString(), exportSpec)); } parent.removeChild(export); t.reportCodeChange(); }
private void normalizeAssignShorthand(Node shorthand) { if (shorthand.getFirstChild().isName()) { Node name = shorthand.getFirstChild(); shorthand.setToken(NodeUtil.getOpFromAssignmentOp(shorthand)); Node parent = shorthand.getParent(); Node insertPoint = IR.empty(); parent.replaceChild(shorthand, insertPoint); Node assign = IR.assign(name.cloneNode().useSourceInfoFrom(name), shorthand) .useSourceInfoFrom(shorthand); assign.setJSDocInfo(shorthand.getJSDocInfo()); shorthand.setJSDocInfo(null); parent.replaceChild(insertPoint, assign); compiler.reportChangeToEnclosingScope(assign); } }
private void normalizeAssignShorthand(Node shorthand) { if (shorthand.getFirstChild().isName()) { Node name = shorthand.getFirstChild(); shorthand.setType(NodeUtil.getOpFromAssignmentOp(shorthand)); Node parent = shorthand.getParent(); Node insertPoint = IR.empty(); parent.replaceChild(shorthand, insertPoint); Node assign = IR.assign(name.cloneNode().useSourceInfoFrom(name), shorthand) .useSourceInfoFrom(shorthand); assign.setJSDocInfo(shorthand.getJSDocInfo()); shorthand.setJSDocInfo(null); parent.replaceChild(insertPoint, assign); compiler.reportCodeChange(); } }
private void visitExportStar(NodeTraversal t, Node export, Node parent) { // export * from 'moduleIdentifier'; Node moduleIdentifier = export.getLastChild(); // Make an "import 'spec'" from this export node and then visit it to rewrite to a require(). Node importNode = IR.importNode(IR.empty(), IR.empty(), moduleIdentifier.cloneNode()); importNode.useSourceInfoFrom(export); parent.addChildBefore(importNode, export); visit(t, importNode, parent); String moduleName = getVarNameOfImport(moduleIdentifier.getString()); export.replaceWith( IR.exprResult( IR.call( IR.getprop(IR.name("$$module"), IR.string("exportAllFrom")), IR.name(moduleName))) .useSourceInfoFromForTree(export)); t.reportCodeChange(); }