Java Code Examples for com.google.javascript.rhino.Node#cloneNode()

The following examples show how to use com.google.javascript.rhino.Node#cloneNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Closure_79_Normalize_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Rewrite the function declaration from:
 *   function x() {}
 *   FUNCTION
 *     NAME
 *     LP
 *     BLOCK
 * to:
 *   var x = function() {};
 *   VAR
 *     NAME
 *       FUNCTION
 *         NAME (w/ empty string)
 *         LP
 *         BLOCK
 */
private void rewriteFunctionDeclaration(Node n) {
  // Prepare a spot for the function.
  Node oldNameNode = n.getFirstChild();
  Node fnNameNode = oldNameNode.cloneNode();
  Node var = new Node(Token.VAR, fnNameNode, n.getLineno(), n.getCharno());
  var.copyInformationFrom(n);

  // Prepare the function
  oldNameNode.setString("");

  // Move the function
  Node parent = n.getParent();
  parent.replaceChild(n, var);
  fnNameNode.addChildToFront(n);

  reportCodeChange("Function declaration");
}
 
Example 2
Source File: Closure_79_Normalize_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Rewrite the function declaration from:
 *   function x() {}
 *   FUNCTION
 *     NAME
 *     LP
 *     BLOCK
 * to:
 *   var x = function() {};
 *   VAR
 *     NAME
 *       FUNCTION
 *         NAME (w/ empty string)
 *         LP
 *         BLOCK
 */
private void rewriteFunctionDeclaration(Node n) {
  // Prepare a spot for the function.
  Node oldNameNode = n.getFirstChild();
  Node fnNameNode = oldNameNode.cloneNode();
  Node var = new Node(Token.VAR, fnNameNode, n.getLineno(), n.getCharno());
  var.copyInformationFrom(n);

  // Prepare the function
  oldNameNode.setString("");

  // Move the function
  Node parent = n.getParent();
  parent.replaceChild(n, var);
  fnNameNode.addChildToFront(n);

  reportCodeChange("Function declaration");
}
 
Example 3
Source File: Normalize.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Rewrite the function declaration from:
 *   function x() {}
 *   FUNCTION
 *     NAME
 *     LP
 *     BLOCK
 * to:
 *   var x = function() {};
 *   VAR
 *     NAME
 *       FUNCTION
 *         NAME (w/ empty string)
 *         LP
 *         BLOCK
 */
private void rewriteFunctionDeclaration(Node n) {
  // Prepare a spot for the function.
  Node oldNameNode = n.getFirstChild();
  Node fnNameNode = oldNameNode.cloneNode();
  Node var = IR.var(fnNameNode).srcref(n);

  // Prepare the function
  oldNameNode.setString("");

  // Move the function
  Node parent = n.getParent();
  parent.replaceChild(n, var);
  fnNameNode.addChildToFront(n);

  reportCodeChange("Function declaration");
}
 
Example 4
Source File: GroupVariableDeclarations.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Attempts to collapse groupVar. This can only happen if groupVar has at most
 * one variable initialization (it may have multiple variable declarations).
 * If successful, then detaches groupVar's children and appends them to
 * firstVar
 *
 * @param firstVar The first VAR {@code Node} in that scope. This is the node
 *                 that we want to collapse groupVar into
 * @param groupVar The VAR {@code Node} that we want to try collapsing
 *                 into the first VAR node of that scope
 */
private void applyGroupingToVar(Node firstVar, Node groupVar) {
  Node child = groupVar.getFirstChild();
  // if some variable is initialized, then the corresponding NAME node will be
  // stored here
  Node initializedName = null;
  while (child != null) {
    if (child.hasChildren()) {
      // check that no more than one var is initialized
      if (initializedName != null) {
        return;
      }
      initializedName = child;
    }
    child = child.getNext();
  }

  // we will be modifying the groupVar subtree so get its parent
  Node groupVarParent = groupVar.getParent();


  if (initializedName != null) {
    if (NodeUtil.isForIn(groupVarParent)) {
      // The target of the for-in expression must be an assignable expression.
      return;
    }

    // we have an initialized var in the VAR node. We will replace the
    // VAR node with an assignment.

    // first create a detached childless clone of initializedName.
    Node clone = initializedName.cloneNode();
    // replace
    groupVar.replaceChild(initializedName, clone);
    // add the assignment now.
    Node initializedVal = initializedName.removeFirstChild();
    Node assignmentNode = IR.assign(initializedName, initializedVal);
    if (groupVarParent.isFor()) {
      // Handle For and For-In Loops specially. For these, we do not need
      // to construct an EXPR_RESULT node.
      groupVarParent.replaceChild(groupVar, assignmentNode);
    } else {
      Node exprNode = NodeUtil.newExpr(assignmentNode);
      groupVarParent.replaceChild(groupVar, exprNode);
    }
  } else {
    // There is no initialized var. But we need to handle FOR and
    // FOR-IN loops specially
    if (groupVarParent.isFor()) {
      if (NodeUtil.isForIn(groupVarParent)) {
        // In For-In loop, we replace the VAR node with a NAME node
        Node nameNodeClone = groupVar.getFirstChild().cloneNode();
        groupVarParent.replaceChild(groupVar, nameNodeClone);
      } else {
        // In For loop, we replace the VAR node with an EMPTY node
        Node emptyNode = IR.empty();
        groupVarParent.replaceChild(groupVar, emptyNode);
      }
    } else {
      // we can safely remove the VAR node
      groupVarParent.removeChild(groupVar);
    }
  }

  Node children = groupVar.removeChildren();
  firstVar.addChildrenToBack(children);

  compiler.reportCodeChange();
}