Java Code Examples for com.google.javascript.jscomp.NodeUtil#getBestJSDocInfo()

The following examples show how to use com.google.javascript.jscomp.NodeUtil#getBestJSDocInfo() . 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: TypeConversionPass.java    From clutz with MIT License 6 votes vote down vote up
private void createTypeAlias(Node n, Node parent) {
  JSDocInfo bestJSDocInfo = NodeUtil.getBestJSDocInfo(n);
  if (bestJSDocInfo != null && bestJSDocInfo.hasTypedefType()) {
    String name;
    switch (n.getToken()) {
      case NAME:
        name = n.getString();
        break;
      case GETPROP:
        // Inner typedef
        name = n.getSecondChild().getString();
        break;
      default:
        name = n.getFirstChild().getString();
        break;
    }
    Node typeDef = Node.newString(Token.TYPE_ALIAS, name);
    nodeComments.moveComment(n, typeDef);
    types.put(name, typeDef);
    typeDef.setJSDocInfo(bestJSDocInfo);
    replaceExpressionOrAssignment(n, parent, typeDef);
  }
}
 
Example 2
Source File: CollectModuleMetadata.java    From clutz with MIT License 6 votes vote down vote up
private void maybeAddProvidesExport(Node exportsName) {
  String fullname = exportsName.getQualifiedName();

  if (providesObjectChildren.containsKey(fullname)) {
    googProvideNamespaceToNode.put(fullname, exportsName);
    addExport(fullname, fullname, nameUtil.lastStepOfName(exportsName));

  } else if (exportsName.isGetProp()
      && providesObjectChildren.containsKey(exportsName.getFirstChild().getQualifiedName())) {
    googProvideNamespaceToNode.put(fullname, exportsName);

    // functions declared on functions should be exported.
    // static functions on classes should not be exported.
    String parentName = exportsName.getFirstChild().getQualifiedName();
    @Nullable Node parentNode = googProvideNamespaceToNode.get(parentName);
    JSDocInfo jsDoc = parentNode != null ? NodeUtil.getBestJSDocInfo(parentNode) : null;

    if (providesObjectChildren.containsKey(parentName)
        && (jsDoc == null || !jsDoc.isConstructor())) {
      addExport(fullname, fullname, nameUtil.lastStepOfName(exportsName));
    }
  }
}
 
Example 3
Source File: TypeAnnotationPass.java    From clutz with MIT License 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  JSDocInfo bestJSDocInfo = NodeUtil.getBestJSDocInfo(n);
  if (bestJSDocInfo == null) {
    return;
  }

  // Add visibility for private and protected.
  if (Visibility.PRIVATE.equals(bestJSDocInfo.getVisibility())) {
    n.putProp(Node.ACCESS_MODIFIER, Visibility.PRIVATE);
  } else if (Visibility.PROTECTED.equals(bestJSDocInfo.getVisibility())) {
    n.putProp(Node.ACCESS_MODIFIER, Visibility.PROTECTED);
  }

  // Change variable declarations to constants
  if (bestJSDocInfo.isConstant() && (n.isVar() || n.isLet())) {
    n.setToken(Token.CONST);
  }
}
 
Example 4
Source File: TypeConversionPass.java    From clutz with MIT License 5 votes vote down vote up
private ClassMemberDeclaration(Node n, boolean isStatic, Node classNode, String memberName) {
  this.exprRoot = n;
  this.rhs = getRhs(n);
  this.jsDoc = NodeUtil.getBestJSDocInfo(n);
  this.isStatic = isStatic;
  this.classNode = classNode;
  this.memberName = memberName;
}
 
Example 5
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 6
Source File: NameUtil.java    From clutz with MIT License 5 votes vote down vote up
/**
 * In-place replaces a prefix with a new prefix in a name node. Does nothing if prefix does not
 * exist.
 */
public void replacePrefixInName(Node name, String prefix, String newPrefix) {
  if (name.matchesQualifiedName(prefix)) {
    Node newName = NodeUtil.newQName(compiler, newPrefix);
    JSDocInfo jsdoc = NodeUtil.getBestJSDocInfo(name);
    newName.setJSDocInfo(jsdoc);
    name.replaceWith(newName);
  } else {
    if (name.isGetProp()) {
      replacePrefixInName(name.getFirstChild(), prefix, newPrefix);
    }
  }
}