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

The following examples show how to use com.google.javascript.rhino.Node#removeChildAfter() . 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: AstParallelizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void split(Node n) {
  Node c = n.getFirstChild();
  Node before = null;
  while (c != null) {
    Node next = c.getNext();
    if (shouldSplit.apply(c)) {
      Node placeHolder = placeHolderProvider.get();
      if (before == null) {
        forest.add(n.removeFirstChild());
        n.addChildToFront(placeHolder);
      } else {
        n.addChildAfter(placeHolder, c);
        n.removeChildAfter(before);
        forest.add(c);
      }
      recordSplitPoint(placeHolder, before, c);
      before = placeHolder;
    } else {
      split(c);
      before = c;
    }
    c = next;
  }
}
 
Example 2
Source File: Closure_102_Normalize_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Move all the functions that are valid at the execution of the first
 * statement of the function to the beginning of the function definition.
 */
private void moveNamedFunctions(Node functionBody) {
  Preconditions.checkState(
      functionBody.getParent().getType() == Token.FUNCTION);
  Node previous = null;
  Node current = functionBody.getFirstChild();
  // Skip any declarations at the beginning of the function body, they
  // are already in the right place.
  while (current != null && NodeUtil.isFunctionDeclaration(current)) {
    previous = current;
    current = current.getNext();
  }

  // Find any remaining declarations and move them.
  Node insertAfter = previous;
  while (current != null) {
    // Save off the next node as the current node maybe removed.
    Node next = current.getNext();
    if (NodeUtil.isFunctionDeclaration(current)) {
      // Remove the declaration from the body.
      Preconditions.checkNotNull(previous);
      functionBody.removeChildAfter(previous);

      // Readd the function at the top of the function body (after any
      // previous declarations).
      insertAfter = addToFront(functionBody, current, insertAfter);
      compiler.reportCodeChange();
    } else {
      // Update the previous only if the current node hasn't been moved.
      previous = current;
    }
    current = next;
  }
}
 
Example 3
Source File: Closure_102_Normalize_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Move all the functions that are valid at the execution of the first
 * statement of the function to the beginning of the function definition.
 */
private void moveNamedFunctions(Node functionBody) {
  Preconditions.checkState(
      functionBody.getParent().getType() == Token.FUNCTION);
  Node previous = null;
  Node current = functionBody.getFirstChild();
  // Skip any declarations at the beginning of the function body, they
  // are already in the right place.
  while (current != null && NodeUtil.isFunctionDeclaration(current)) {
    previous = current;
    current = current.getNext();
  }

  // Find any remaining declarations and move them.
  Node insertAfter = previous;
  while (current != null) {
    // Save off the next node as the current node maybe removed.
    Node next = current.getNext();
    if (NodeUtil.isFunctionDeclaration(current)) {
      // Remove the declaration from the body.
      Preconditions.checkNotNull(previous);
      functionBody.removeChildAfter(previous);

      // Readd the function at the top of the function body (after any
      // previous declarations).
      insertAfter = addToFront(functionBody, current, insertAfter);
      compiler.reportCodeChange();
    } else {
      // Update the previous only if the current node hasn't been moved.
      previous = current;
    }
    current = next;
  }
}
 
Example 4
Source File: Closure_79_Normalize_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Move all the functions that are valid at the execution of the first
 * statement of the function to the beginning of the function definition.
 */
private void moveNamedFunctions(Node functionBody) {
  Preconditions.checkState(
      functionBody.getParent().getType() == Token.FUNCTION);
  Node previous = null;
  Node current = functionBody.getFirstChild();
  // Skip any declarations at the beginning of the function body, they
  // are already in the right place.
  while (current != null && NodeUtil.isFunctionDeclaration(current)) {
    previous = current;
    current = current.getNext();
  }

  // Find any remaining declarations and move them.
  Node insertAfter = previous;
  while (current != null) {
    // Save off the next node as the current node maybe removed.
    Node next = current.getNext();
    if (NodeUtil.isFunctionDeclaration(current)) {
      // Remove the declaration from the body.
      Preconditions.checkNotNull(previous);
      functionBody.removeChildAfter(previous);

      // Readd the function at the top of the function body (after any
      // previous declarations).
      insertAfter = addToFront(functionBody, current, insertAfter);
      reportCodeChange("Move function declaration not at top of function");
    } else {
      // Update the previous only if the current node hasn't been moved.
      previous = current;
    }
    current = next;
  }
}
 
Example 5
Source File: Closure_79_Normalize_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Move all the functions that are valid at the execution of the first
 * statement of the function to the beginning of the function definition.
 */
private void moveNamedFunctions(Node functionBody) {
  Preconditions.checkState(
      functionBody.getParent().getType() == Token.FUNCTION);
  Node previous = null;
  Node current = functionBody.getFirstChild();
  // Skip any declarations at the beginning of the function body, they
  // are already in the right place.
  while (current != null && NodeUtil.isFunctionDeclaration(current)) {
    previous = current;
    current = current.getNext();
  }

  // Find any remaining declarations and move them.
  Node insertAfter = previous;
  while (current != null) {
    // Save off the next node as the current node maybe removed.
    Node next = current.getNext();
    if (NodeUtil.isFunctionDeclaration(current)) {
      // Remove the declaration from the body.
      Preconditions.checkNotNull(previous);
      functionBody.removeChildAfter(previous);

      // Readd the function at the top of the function body (after any
      // previous declarations).
      insertAfter = addToFront(functionBody, current, insertAfter);
      reportCodeChange("Move function declaration not at top of function");
    } else {
      // Update the previous only if the current node hasn't been moved.
      previous = current;
    }
    current = next;
  }
}
 
Example 6
Source File: CreateSyntheticBlocks.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Like removeChildAfter, the firstChild is removed
 */
private Node removeChildAfter(Node parent, @Nullable Node siblingBefore) {
  if (siblingBefore == null) {
    return parent.removeFirstChild();
  } else {
    return parent.removeChildAfter(siblingBefore);
  }
}
 
Example 7
Source File: Normalize.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Move all the functions that are valid at the execution of the first
 * statement of the function to the beginning of the function definition.
 */
private void moveNamedFunctions(Node functionBody) {
  Preconditions.checkState(
      functionBody.getParent().isFunction());
  Node previous = null;
  Node current = functionBody.getFirstChild();
  // Skip any declarations at the beginning of the function body, they
  // are already in the right place.
  while (current != null && NodeUtil.isFunctionDeclaration(current)) {
    previous = current;
    current = current.getNext();
  }

  // Find any remaining declarations and move them.
  Node insertAfter = previous;
  while (current != null) {
    // Save off the next node as the current node maybe removed.
    Node next = current.getNext();
    if (NodeUtil.isFunctionDeclaration(current)) {
      // Remove the declaration from the body.
      Preconditions.checkNotNull(previous);
      functionBody.removeChildAfter(previous);

      // Read the function at the top of the function body (after any
      // previous declarations).
      insertAfter = addToFront(functionBody, current, insertAfter);
      reportCodeChange("Move function declaration not at top of function");
    } else {
      // Update the previous only if the current node hasn't been moved.
      previous = current;
    }
    current = next;
  }
}
 
Example 8
Source File: ExpandJqueryAliases.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void replaceOriginalJqueryEachCall(Node n, Node expandedBlock) {
  // Check to see if the return value of the original jQuery.expandedEach
  // call is used. If so, we need to wrap each loop expansion in an anonymous
  // function and return the original objectToLoopOver.
  if (n.getParent().isExprResult()) {
    Node parent = n.getParent();
    Node grandparent = parent.getParent();
    Node insertAfter = parent;
    while (expandedBlock.hasChildren()) {
      Node child = expandedBlock.getFirstChild().detachFromParent();
      grandparent.addChildAfter(child, insertAfter);
      insertAfter = child;
    }
    grandparent.removeChild(parent);
  } else {
    // Return the original object
    Node callTarget = n.getFirstChild();
    Node objectToLoopOver = callTarget.getNext();

    objectToLoopOver.detachFromParent();
    Node ret = IR.returnNode(objectToLoopOver).srcref(callTarget);
    expandedBlock.addChildToBack(ret);

    // Wrap all of the expanded loop calls in a new anonymous function
    Node fnc = IR.function(IR.name("").srcref(callTarget),
        IR.paramList().srcref(callTarget),
        expandedBlock);
    n.replaceChild(callTarget, fnc);
    n.putBooleanProp(Node.FREE_CALL, true);

    // remove any other pre-existing call arguments
    while(fnc.getNext() != null) {
      n.removeChildAfter(fnc);
    }
  }
  compiler.reportCodeChange();
}
 
Example 9
Source File: StatementFusion.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private static void fuseExpresssonIntoSecondChild(Node exp, Node stmt) {
  Node val = stmt.removeChildAfter(stmt.getFirstChild());
  Node comma = fuseExpressionIntoExpression(exp, val);
  stmt.addChildAfter(comma, stmt.getFirstChild());
}
 
Example 10
Source File: ExpandJqueryAliases.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Expand jQuery.extend (and derivative) calls into direct object assignments
 * Example: jQuery.extend(obj1, {prop1: val1, prop2: val2}) ->
 *   obj1.prop1 = val1;
 *   obj1.prop2 = val2;
 */
private void maybeExpandJqueryExtendCall(Node n) {
  Node callTarget = n.getFirstChild();
  Node objectToExtend = callTarget.getNext(); // first argument
  Node extendArg = objectToExtend.getNext(); // second argument
  boolean ensureObjectDefined = true;

  if (extendArg == null) {
    // Only one argument was specified, so extend jQuery namespace
    extendArg = objectToExtend;
    objectToExtend = callTarget.getFirstChild();
    ensureObjectDefined = false;
  } else if (objectToExtend.isGetProp() &&
        (objectToExtend.getLastChild().getString().equals("prototype") ||
        convention.isPrototypeAlias(objectToExtend))) {
    ensureObjectDefined = false;
  }

  // Check for an empty object literal
  if (!extendArg.hasChildren()) {
    return;
  }

  // Since we are expanding jQuery.extend calls into multiple statements,
  // encapsulate the new statements in a new block.
  Node fncBlock = IR.block().srcref(n);

  if (ensureObjectDefined) {
    Node assignVal = IR.or(objectToExtend.cloneTree(),
        IR.objectlit().srcref(n)).srcref(n);
    Node assign = IR.assign(objectToExtend.cloneTree(), assignVal).srcref(n);
    fncBlock.addChildrenToFront(IR.exprResult(assign).srcref(n));
  }

  while (extendArg.hasChildren()) {
    Node currentProp = extendArg.removeFirstChild();
    currentProp.setType(Token.STRING);

    Node propValue = currentProp.removeFirstChild();

    Node newProp;
    if(currentProp.isQuotedString()) {
      newProp = IR.getelem(objectToExtend.cloneTree(),
          currentProp).srcref(currentProp);
    } else {
      newProp = IR.getprop(objectToExtend.cloneTree(),
          currentProp).srcref(currentProp);
    }

    Node assignNode = IR.assign(newProp, propValue).srcref(currentProp);
    fncBlock.addChildToBack(IR.exprResult(assignNode).srcref(currentProp));
  }

  // Check to see if the return value is used. If not, replace the original
  // call with new block. Otherwise, wrap the statements in an
  // immediately-called anonymous function.
  if (n.getParent().isExprResult()) {
    Node parent = n.getParent();
    parent.getParent().replaceChild(parent, fncBlock);
  } else {
    Node targetVal;
    if ("jQuery.prototype".equals(objectToExtend.getQualifiedName())) {
      // When extending the jQuery prototype, return the jQuery namespace.
      // This is not commonly used.
      targetVal = objectToExtend.removeFirstChild();
    } else {
      targetVal = objectToExtend.detachFromParent();
    }
    fncBlock.addChildToBack(IR.returnNode(targetVal).srcref(targetVal));

    Node fnc = IR.function(IR.name("").srcref(n),
        IR.paramList().srcref(n),
        fncBlock);
    n.replaceChild(callTarget, fnc);
    n.putBooleanProp(Node.FREE_CALL, true);

    // remove any other pre-existing call arguments
    while(fnc.getNext() != null) {
      n.removeChildAfter(fnc);
    }
  }
  compiler.reportCodeChange();
}