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

The following examples show how to use com.google.javascript.rhino.Node#hasOneChild() . 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_75_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param nameNode A name node
 * @return The JSDocInfo for the name node
 */
static JSDocInfo getInfoForNameNode(Node nameNode) {
  JSDocInfo info = null;
  Node parent = null;
  if (nameNode != null) {
    info = nameNode.getJSDocInfo();
    parent = nameNode.getParent();
  }

  if (info == null && parent != null &&
      ((parent.getType() == Token.VAR && parent.hasOneChild()) ||
        parent.getType() == Token.FUNCTION)) {
    info = parent.getJSDocInfo();
  }
  return info;
}
 
Example 2
Source File: Closure_87_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the node is a block with a single statement that is
 *     an expression.
 */
private boolean isFoldableExpressBlock(Node n) {
  if (n.getType() == Token.BLOCK) {
    if (n.hasOneChild()) {
      Node maybeExpr = n.getFirstChild();
        // IE has a bug where event handlers behave differently when
        // their return value is used vs. when their return value is in
        // an EXPR_RESULT. It's pretty freaking weird. See:
        // http://code.google.com/p/closure-compiler/issues/detail?id=291
        // We try to detect this case, and not fold EXPR_RESULTs
        // into other expressions.

          // We only have to worry about methods with an implicit 'this'
          // param, or this doesn't happen.

        return NodeUtil.isExpressionNode(maybeExpr);
    }
  }

  return false;
}
 
Example 3
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visit a node in the global scope, and add anything it declares to the
 * global symbol table.
 *
 * @param t The current traversal.
 * @param n The node being visited.
 * @param parent The parent of n
 */
@Override public void visit(NodeTraversal t, Node n, Node parent) {
  super.visit(t, n, parent);

  switch (n.getType()) {

    case Token.VAR:
      // Handle typedefs.
      if (n.hasOneChild()) {
        checkForTypedef(t, n.getFirstChild(), n.getJSDocInfo());
      }
      break;
  }
}
 
Example 4
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node is a single return statement.
 */
private boolean isReturnExpression(Node n) {
  if (n.isReturn()) {
    return n.hasOneChild();
  }
  return false;
}
 
Example 5
Source File: FunctionRewriter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return function return value node if function body contains a
 * single return statement.  Otherwise, null.
 */
protected final Node maybeGetSingleReturnRValue(Node functionNode) {
  Node body = functionNode.getLastChild();
  if (!body.hasOneChild()) {
    return null;
  }

  Node statement = body.getFirstChild();
  if (statement.isReturn()) {
    return statement.getFirstChild();
  }
  return null;
}
 
Example 6
Source File: FunctionRewriter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks if the function matches the pattern:
 *   function(<value>, <rest>) {this.<name> = <value>}
 * and returns <name> if a match is found.
 *
 * @return STRING node that is the RHS of a this property get; or null.
 */
private Node getSetPropertyName(Node functionNode) {
  Node body = functionNode.getLastChild();
  if (!body.hasOneChild()) {
    return null;
  }

  Node argList = functionNode.getFirstChild().getNext();
  Node paramNode = argList.getFirstChild();
  if (paramNode == null) {
    return null;
  }

  Node statement = body.getFirstChild();
  if (!NodeUtil.isExprAssign(statement)) {
    return null;
  }

  Node assign = statement.getFirstChild();
  Node lhs = assign.getFirstChild();
  if (lhs.isGetProp() && lhs.getFirstChild().isThis()) {
    Node rhs = assign.getLastChild();
    if (rhs.isName() &&
        rhs.getString().equals(paramNode.getString())) {
      Node propertyName = lhs.getLastChild();
      return propertyName;
    }
  }
  return null;
}
 
Example 7
Source File: Nopol2017_0027_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visit a node in the global scope, and add anything it declares to the
 * global symbol table.
 *
 * @param t The current traversal.
 * @param n The node being visited.
 * @param parent The parent of n
 */
@Override public void visit(NodeTraversal t, Node n, Node parent) {
  super.visit(t, n, parent);

  switch (n.getType()) {

    case Token.VAR:
      // Handle typedefs.
      if (n.hasOneChild()) {
        checkForTypedef(t, n.getFirstChild(), n.getJSDocInfo());
      }
      break;
  }
}
 
Example 8
Source File: Closure_55_FunctionRewriter_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Checks if the function matches the pattern:
 *   function(<value>, <rest>) {this.<name> = <value>}
 * and returns <name> if a match is found.
 *
 * @return STRING node that is the rhs of a this property get; or null.
 */
private Node getSetPropertyName(Node functionNode) {
  Node body = functionNode.getLastChild();
  if (!body.hasOneChild()) {
    return null;
  }

  Node argList = functionNode.getFirstChild().getNext();
  Node paramNode = argList.getFirstChild();
  if (paramNode == null) {
    return null;
  }

  Node statement = body.getFirstChild();
  if (!NodeUtil.isExprAssign(statement)) {
    return null;
  }

  Node assign = statement.getFirstChild();
  Node lhs = assign.getFirstChild();
  if (NodeUtil.isGetProp(lhs) && NodeUtil.isThis(lhs.getFirstChild())) {
    Node rhs = assign.getLastChild();
    if (NodeUtil.isName(rhs) &&
        rhs.getString().equals(paramNode.getString())) {
      Node propertyName = lhs.getLastChild();
      return propertyName;
    }
  }
  return null;
}
 
Example 9
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a VAR node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitVar(NodeTraversal t, Node n) {
  // TODO(nicksantos): Fix this so that the doc info always shows up
  // on the NAME node. We probably want to wait for the parser
  // merge to fix this.
  JSDocInfo varInfo = n.hasOneChild() ? n.getJSDocInfo() : null;
  for (Node name : n.children()) {
    Node value = name.getFirstChild();
    // A null var would indicate a bug in the scope creation logic.
    Var var = t.getScope().getVar(name.getString());

    if (value != null) {
      JSType valueType = getJSType(value);
      JSType nameType = var.getType();
      nameType = (nameType == null) ? getNativeType(UNKNOWN_TYPE) : nameType;

      JSDocInfo info = name.getJSDocInfo();
      if (info == null) {
        info = varInfo;
      }
      if (info != null && info.hasEnumParameterType()) {
        // var.getType() can never be null, this would indicate a bug in the
        // scope creation logic.
        checkEnumInitializer(
            t, value,
            info.getEnumParameterType().evaluate(t.getScope(), typeRegistry));
      } else if (var.isTypeInferred()) {
        ensureTyped(t, name, valueType);
      } else {
        validator.expectCanAssignTo(
            t, value, valueType, nameType, "initializing variable");
      }
    }
  }
}
 
Example 10
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return Whether the node is a single return statement.
 */
private boolean isReturnExpression(Node n) {
  if (n.isReturn()) {
    return n.hasOneChild();
  }
  return false;
}
 
Example 11
Source File: Closure_67_AnalyzePrototypeProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public void remove() {
  Node parent = nameNode.getParent();
  if (parent.getType() == Token.FUNCTION || parent.hasOneChild()) {
    NodeUtil.removeChild(parent.getParent(), parent);
  } else {
    Preconditions.checkState(parent.getType() == Token.VAR);
    parent.removeChild(nameNode);
  }
}
 
Example 12
Source File: Closure_55_FunctionRewriter_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Checks if the function matches the pattern:
 *   function(<value>, <rest>) {this.<name> = <value>}
 * and returns <name> if a match is found.
 *
 * @return STRING node that is the rhs of a this property get; or null.
 */
private Node getSetPropertyName(Node functionNode) {
  Node body = functionNode.getLastChild();
  if (!body.hasOneChild()) {
    return null;
  }

  Node argList = functionNode.getFirstChild().getNext();
  Node paramNode = argList.getFirstChild();
  if (paramNode == null) {
    return null;
  }

  Node statement = body.getFirstChild();
  if (!NodeUtil.isExprAssign(statement)) {
    return null;
  }

  Node assign = statement.getFirstChild();
  Node lhs = assign.getFirstChild();
  if (NodeUtil.isGetProp(lhs) && NodeUtil.isThis(lhs.getFirstChild())) {
    Node rhs = assign.getLastChild();
    if (NodeUtil.isName(rhs) &&
        rhs.getString().equals(paramNode.getString())) {
      Node propertyName = lhs.getLastChild();
      return propertyName;
    }
  }
  return null;
}
 
Example 13
Source File: Closure_105_FoldConstants_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node is a block with a single statement that is
 *     an return.
 */
private boolean isReturnExpressBlock(Node n) {
  if (n.getType() == Token.BLOCK) {
    if (n.hasOneChild()) {
      Node first = n.getFirstChild();
      if (first.getType() == Token.RETURN) {
        return first.hasOneChild();
      }
    }
  }

  return false;
}
 
Example 14
Source File: Cardumen_00200_s.java    From coming with MIT License 5 votes vote down vote up
/** Find the best JSDoc for the given node. */
static JSDocInfo getBestJSDocInfo(Node n) {
  JSDocInfo info = n.getJSDocInfo();
  if (info == null) {
    Node parent = n.getParent();
    if (parent == null) {
      return null;
    }

    if (parent.isName()) {
      return getBestJSDocInfo(parent);
    } else if (parent.isAssign()) {
      return parent.getJSDocInfo();
    } else if (isObjectLitKey(parent, parent.getParent())) {
      return parent.getJSDocInfo();
    } else if (parent.isFunction()) {
      return parent.getJSDocInfo();
    } else if (parent.isVar() && parent.hasOneChild()) {
      return parent.getJSDocInfo();
    } else if ((parent.isHook() && parent.getFirstChild() != n) ||
               parent.isOr() ||
               parent.isAnd() ||
               (parent.isComma() && parent.getFirstChild() != n)) {
      return getBestJSDocInfo(parent);
    }
  }
  return info;
}
 
Example 15
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a VAR node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitVar(NodeTraversal t, Node n) {
  // TODO(nicksantos): Fix this so that the doc info always shows up
  // on the NAME node. We probably want to wait for the parser
  // merge to fix this.
  JSDocInfo varInfo = n.hasOneChild() ? n.getJSDocInfo() : null;
  for (Node name : n.children()) {
    Node value = name.getFirstChild();
    // A null var would indicate a bug in the scope creation logic.
    Var var = t.getScope().getVar(name.getString());

    if (value != null) {
      JSType valueType = getJSType(value);
      JSType nameType = var.getType();
      nameType = (nameType == null) ? getNativeType(UNKNOWN_TYPE) : nameType;

      JSDocInfo info = name.getJSDocInfo();
      if (info == null) {
        info = varInfo;
      }

      checkEnumAlias(t, info, value);
      if (var.isTypeInferred()) {
        ensureTyped(t, name, valueType);
      } else {
        validator.expectCanAssignTo(
            t, value, valueType, nameType, "initializing variable");
      }
    }
  }
}
 
Example 16
Source File: TypeConversionPass.java    From clutz with MIT License 5 votes vote down vote up
private boolean containsObject(Node typedefNode) {
  Token typedefToken = typedefNode.getToken();
  if (typedefToken == Token.LC) {
    return true;
  }

  if ((typedefToken == Token.QMARK || typedefToken == Token.BANG)
      && typedefNode.hasOneChild()) {
    // child Node is a simple type or a LC
    return containsObject(typedefNode.getFirstChild());
  }

  return false;
}
 
Example 17
Source File: Closure_67_AnalyzePrototypeProperties_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public void remove() {
  Node parent = nameNode.getParent();
  if (parent.getType() == Token.FUNCTION || parent.hasOneChild()) {
    NodeUtil.removeChild(parent.getParent(), parent);
  } else {
    Preconditions.checkState(parent.getType() == Token.VAR);
    parent.removeChild(nameNode);
  }
}
 
Example 18
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node is a block with a single statement that is
 *     an expression.
 */
private boolean isFoldableExpressBlock(Node n) {
  if (n.isBlock()) {
    if (n.hasOneChild()) {
      Node maybeExpr = n.getFirstChild();
      if (maybeExpr.isExprResult()) {
        // IE has a bug where event handlers behave differently when
        // their return value is used vs. when their return value is in
        // an EXPR_RESULT. It's pretty freaking weird. See:
        // http://code.google.com/p/closure-compiler/issues/detail?id=291
        // We try to detect this case, and not fold EXPR_RESULTs
        // into other expressions.
        if (maybeExpr.getFirstChild().isCall()) {
          Node calledFn = maybeExpr.getFirstChild().getFirstChild();

          // We only have to worry about methods with an implicit 'this'
          // param, or this doesn't happen.
          if (calledFn.isGetElem()) {
            return false;
          } else if (calledFn.isGetProp() &&
                     calledFn.getLastChild().getString().startsWith("on")) {
            return false;
          }
        }

        return true;
      }
      return false;
    }
  }

  return false;
}
 
Example 19
Source File: Closure_54_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visit a node in the global scope, and add anything it declares to the
 * global symbol table.
 *
 * @param t The current traversal.
 * @param n The node being visited.
 * @param parent The parent of n
 */
@Override public void visit(NodeTraversal t, Node n, Node parent) {
  super.visit(t, n, parent);

  switch (n.getType()) {

    case Token.VAR:
      // Handle typedefs.
      if (n.hasOneChild()) {
        checkForTypedef(t, n.getFirstChild(), n.getJSDocInfo());
      }
      break;
  }
}
 
Example 20
Source File: Nopol2017_0029_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a VAR node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitVar(NodeTraversal t, Node n) {
  // TODO(nicksantos): Fix this so that the doc info always shows up
  // on the NAME node. We probably want to wait for the parser
  // merge to fix this.
  JSDocInfo varInfo = n.hasOneChild() ? n.getJSDocInfo() : null;
  for (Node name : n.children()) {
    Node value = name.getFirstChild();
    // A null var would indicate a bug in the scope creation logic.
    Var var = t.getScope().getVar(name.getString());

    if (value != null) {
      JSType valueType = getJSType(value);
      JSType nameType = var.getType();
      nameType = (nameType == null) ? getNativeType(UNKNOWN_TYPE) : nameType;

      JSDocInfo info = name.getJSDocInfo();
      if (info == null) {
        info = varInfo;
      }

      checkEnumAlias(t, info, value);
      if (var.isTypeInferred()) {
        ensureTyped(t, name, valueType);
      } else {
        validator.expectCanAssignTo(
            t, value, valueType, nameType, "initializing variable");
      }
    }
  }
}