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

The following examples show how to use com.google.javascript.rhino.Node#toString() . 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_86_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/** 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());
  }
}
 
Example 2
Source File: Closure_86_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** 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());
  }
}
 
Example 3
Source File: Closure_37_NodeTraversal_t.java    From coming with MIT License 5 votes vote down vote up
private String formatNodeContext(String label, Node n) {
  if (n == null) {
    return "  " + label + ": NULL";
  }
  return "  " + label + "(" + n.toString(false, false, false) + "): "
      + formatNodePosition(n);
}
 
Example 4
Source File: Closure_37_NodeTraversal_s.java    From coming with MIT License 5 votes vote down vote up
private String formatNodeContext(String label, Node n) {
  if (n == null) {
    return "  " + label + ": NULL";
  }
  return "  " + label + "(" + n.toString(false, false, false) + "): "
      + formatNodePosition(n);
}
 
Example 5
Source File: Closure_94_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/** 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());
  }
}
 
Example 6
Source File: Closure_94_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** 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());
  }
}
 
Example 7
Source File: NodeTraversal.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private String formatNodeContext(String label, Node n) {
  if (n == null) {
    return "  " + label + ": NULL";
  }
  return "  " + label + "(" + n.toString(false, false, false) + "): "
      + formatNodePosition(n);
}