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

The following examples show how to use com.google.javascript.rhino.Node#hasMoreThanOneChild() . 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_112_TypeInference_t.java    From coming with MIT License 6 votes vote down vote up
private Map<TemplateType, JSType> inferTemplateTypesFromParameters(
    FunctionType fnType, Node call) {
  if (fnType.getTemplateTypeMap().getTemplateKeys().isEmpty()) {
    return Collections.emptyMap();
  }

  Map<TemplateType, JSType> resolvedTypes = Maps.newIdentityHashMap();

  Node callTarget = call.getFirstChild();
  if (NodeUtil.isGet(callTarget)) {
    Node obj = callTarget.getFirstChild();
    maybeResolveTemplatedType(
        fnType.getTypeOfThis(),
        getJSType(obj),
        resolvedTypes);
  }

  if (call.hasMoreThanOneChild()) {
    maybeResolveTemplateTypeFromNodes(
        fnType.getParameters(),
        call.getChildAtIndex(1).siblings(),
        resolvedTypes);
  }
  return resolvedTypes;
}
 
Example 2
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 6 votes vote down vote up
private Map<TemplateType, JSType> inferTemplateTypesFromParameters(
    FunctionType fnType, Node call) {
  if (fnType.getTemplateTypeMap().getTemplateKeys().isEmpty()) {
    return Collections.emptyMap();
  }

  Map<TemplateType, JSType> resolvedTypes = Maps.newIdentityHashMap();

  Node callTarget = call.getFirstChild();
  if (NodeUtil.isGet(callTarget)) {
    Node obj = callTarget.getFirstChild();
    maybeResolveTemplatedType(
        fnType.getTypeOfThis(),
        getJSType(obj),
        resolvedTypes);
  }

  if (call.hasMoreThanOneChild()) {
    maybeResolveTemplateTypeFromNodes(
        fnType.getParameters(),
        call.getChildAtIndex(1).siblings(),
        resolvedTypes);
  }
  return resolvedTypes;
}
 
Example 3
Source File: AstValidator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void validateMinimumChildCount(Node n, int i) {
  boolean valid = false;
  if (i == 1) {
    valid = n.hasChildren();
  } else if (i == 2) {
    valid = n.hasMoreThanOneChild();
  } else {
    valid = n.getChildCount() >= i;
  }

  if (!valid) {
    violation(
        "Expected at least " + i + " children, but was "
            + n.getChildCount(), n);
  }
}
 
Example 4
Source File: TypeInference.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private Map<TemplateType, JSType> inferTemplateTypesFromParameters(
    FunctionType fnType, Node call) {
  if (fnType.getTemplateKeys().isEmpty()) {
    return Collections.emptyMap();
  }

  Map<TemplateType, JSType> resolvedTypes = Maps.newIdentityHashMap();

  Node callTarget = call.getFirstChild();
  if (NodeUtil.isGet(callTarget)) {
    Node obj = callTarget.getFirstChild();
    maybeResolveTemplatedType(
        fnType.getTypeOfThis(),
        getJSType(obj),
        resolvedTypes);
  }

  if (call.hasMoreThanOneChild()) {
    maybeResolveTemplateTypeFromNodes(
        fnType.getParameters(),
        call.getChildAtIndex(1).siblings(),
        resolvedTypes);
  }
  return resolvedTypes;
}
 
Example 5
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 6
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 7
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 8
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 9
Source File: StatementFusion.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void fuseIntoOneStatement(Node block) {
  Node cur = block.removeFirstChild();

  // Starts building a tree.
  Node commaTree = cur.removeFirstChild();


  while (block.hasMoreThanOneChild()) {
    Node next = block.removeFirstChild().removeFirstChild();
    commaTree = fuseExpressionIntoExpression(commaTree, next);
  }

  Preconditions.checkState(block.hasOneChild());
  Node last = block.getLastChild();

  // Now we are just left with two statements. The comma tree of the first
  // n - 1 statements (which can be used in an expression) and the last
  // statement. We perform specific fusion based on the last statement's type.
  switch(last.getType()) {
    case Token.IF:
    case Token.RETURN:
    case Token.THROW:
    case Token.SWITCH:
    case Token.EXPR_RESULT:
      fuseExpresssonIntoFirstChild(commaTree, last);
      return;
    case Token.FOR:
      if (NodeUtil.isForIn(last)) {
        fuseExpresssonIntoSecondChild(commaTree, last);
      }
      return ;
    default:
      throw new IllegalStateException("Statement fusion missing.");
  }
}
 
Example 10
Source File: AstValidator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void validateMaximumChildCount(Node n, int i) {
  boolean valid = false;
  if (i == 1) {
    valid = !n.hasMoreThanOneChild();
  } else {
    valid = n.getChildCount() <= i;
  }
  if (!valid) {
    violation(
        "Expected no more than " + i + " children, but was "
            + n.getChildCount(), n);
  }
}