Java Code Examples for com.google.javascript.rhino.JSDocInfo#getParameterType()

The following examples show how to use com.google.javascript.rhino.JSDocInfo#getParameterType() . 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: TypeAnnotationPass.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Attempts to set the type of parameter represented by node by extracting it from @param
 * annotations in the function's jsDoc. Returns true if it succeeds (i.e. if it finds the type).
 */
private boolean setParameterTypeFromFunctionDoc(Node node, Node parent) {
  JSDocInfo parentDocInfo = NodeUtil.getBestJSDocInfo(parent.getParent());
  if (parentDocInfo == null) {
    return false;
  }
  String parameterName =
      node.isObjectPattern()
          ? parentDocInfo.getParameterNameAt(parent.getIndexOfChild(node))
          : node.getString();
  JSTypeExpression parameterType = parentDocInfo.getParameterType(parameterName);
  if (parameterType == null) {
    return false;
  }
  TypeDeclarationNode parameterTypeNode = convertTypeNodeAST(parameterType.getRoot());
  // Parameter is declared using verbose @param syntax before the function definition.
  Node attachTypeExpr = node;
  // Modify the primary AST to represent a function parameter as a
  // REST node, if the type indicates it is a rest parameter.
  if (parameterType.getRoot().getToken() == Token.ITER_REST) {
    attachTypeExpr = IR.iterRest(IR.name(node.getString()));
    nodeComments.replaceWithComment(node, attachTypeExpr);
  }
  // Modify the AST to represent an optional parameter
  if (parameterType.getRoot().getToken() == Token.EQUALS) {
    attachTypeExpr = IR.name(node.getString());
    if (!node.getParent().isDefaultValue()) {
      attachTypeExpr.putBooleanProp(Node.OPT_ES6_TYPED, true);
    } else if (node.getParent().getSecondChild().isName()
        && node.getParent().getSecondChild().getString().equals("undefined")) {
      // if default value is "undefined" add undefined to the type
      parameterTypeNode = flatUnionType(ImmutableList.of(parameterTypeNode, undefinedType()));
    }
    nodeComments.replaceWithComment(node, attachTypeExpr);
  }
  setTypeExpression(attachTypeExpr, parameterTypeNode);
  return true;
}
 
Example 2
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private List<Parameter> extractParameters(JSDocInfo info) {
  List<Parameter> parameters = Lists.newArrayListWithExpectedSize(info.getParameterCount());
  for (int i = 0; i < info.getParameterCount(); i++) {
    String name = info.getParameterNameAt(i);
    JSTypeExpression expression = info.getParameterType(name);
    String description = info.getDescriptionForParameter(name);
    parameters.add(new Parameter(name, expression, description));
  }
  return parameters;
}