com.google.javascript.rhino.jstype.NamedType Java Examples

The following examples show how to use com.google.javascript.rhino.jstype.NamedType. 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: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
private boolean isPrivate(JSType type) {
  // Due to https://github.com/google/closure-compiler/issues/1975 we cannot obtain the JSDoc
  // for a typedef. Assume non-private as it is more common.
  // Closure creates a NamedType when the typedef is used in an union, eg: T | null.
  // Dereference the named type before checking if it is a typedef.
  NamedType nType = type.toMaybeNamedType();
  if (typedefs.containsKey(type)
      || (nType != null && typedefs.containsKey(nType.getReferencedType()))) {
    return false;
  }

  // For unknown reasons, enum types do not keep their defining jsdoc info.
  if (type.isEnumType() || type.isEnumElementType()) {
    return isPrivate(type.getDisplayName());
  } else {
    return isPrivate(type.getJSDocInfo());
  }
}
 
Example #2
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Override
public Void caseNamedType(NamedType type) {
  String name = type.getReferenceName();

  com.github.jsdossier.proto.NamedType.Builder namedType = createNamedType(name);
  if (SafeUrls.fromProto(namedType.getLink().getHref()).getSafeUrlString().isEmpty()) {
    // If there is no href, we were not able to resolve the type, so assume it is
    // nullable by default.
    currentExpression()
        .getUnionTypeBuilder()
        .addType(TypeExpression.newBuilder().setNamedType(namedType))
        .addType(NULL_TYPE);
  } else {
    currentExpression().setNamedType(namedType);
  }
  return null;
}
 
Example #3
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Override
public Void caseTemplatizedType(TemplatizedType type) {
  type.getReferencedType().visit(this);
  Iterator<JSType> types = type.getTemplateTypes().iterator();

  if (currentExpression().getNamedType() == null) {
    throw new IllegalStateException("unexpected templatized type structure");
  }
  com.github.jsdossier.proto.NamedType.Builder namedType =
      currentExpression().getNamedTypeBuilder();

  while (types.hasNext()) {
    JSType templateType = types.next();
    expressions.addLast(namedType.addTemplateTypeBuilder());
    templateType.visit(this);
    expressions.removeLast();
  }
  return null;
}
 
Example #4
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Nullable
@CheckReturnValue
private com.github.jsdossier.proto.NamedType.Builder createNamedType(JSType type) {
  NominalType ntype = resolve(type);
  if (ntype == null) {
    return null;
  }
  return createNamedType(ntype);
}
 
Example #5
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Override
public Void caseEnumElementType(EnumElementType type) {
  List<NominalType> types = typeRegistry.getTypes(type.getEnumType());
  if (types.isEmpty()) {
    type.getEnumType().visit(this);
  } else {
    com.github.jsdossier.proto.NamedType link = linkFactory.createTypeReference(types.get(0));
    currentExpression().setNamedType(link);
  }
  return null;
}
 
Example #6
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private void caseInstanceType(String displayName, ObjectType type) {
  TypeExpression.Builder expression = currentExpression();

  com.github.jsdossier.proto.UnionType.Builder unionType = null;
  if (type.isNullable()) {
    unionType = expression.getUnionTypeBuilder();
    expression = unionType.addTypeBuilder();
  }

  com.github.jsdossier.proto.NamedType.Builder namedType =
      createNamedType(type.getConstructor());
  if (namedType == null) {
    com.github.jsdossier.proto.NamedType link =
        linkFactory.createNativeExternLink(type.getReferenceName());
    if (link == null) {
      expression.setNamedType(linkFactory.resolveTypeReference(displayName));
    } else {
      expression.setNamedType(link);
    }
  } else {
    expression.setNamedType(namedType);
  }

  if (unionType != null && type.isNullable()) {
    unionType.addTypeBuilder().setNullType(true);
  }
}
 
Example #7
Source File: AbstractNoOpVisitor.java    From jsinterop-generator with Apache License 2.0 4 votes vote down vote up
@Override
public T caseNamedType(NamedType type) {
  return null;
}
 
Example #8
Source File: ClosureTypeRegistry.java    From jsinterop-generator with Apache License 2.0 4 votes vote down vote up
@Override
public TypeReference caseNamedType(NamedType type) {
  return resolveTypeReference(type.getReferencedType());
}
 
Example #9
Source File: DeclarationGenerator.java    From clutz with MIT License 4 votes vote down vote up
@Override
public Void caseNamedType(NamedType type) {
  return null;
}
 
Example #10
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
@Override
public Object caseNamedType(NamedType type) {
  return null;
}
 
Example #11
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private com.github.jsdossier.proto.NamedType.Builder createNamedType(NominalType type) {
  return linkFactory.createTypeReference(type).toBuilder();
}
 
Example #12
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private void appendNativeType(String type) {
  com.github.jsdossier.proto.NamedType link =
      checkNotNull(linkFactory.createNativeExternLink(type));
  currentExpression().setNamedType(link);
}
 
Example #13
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
@Override
public Void caseFunctionType(FunctionType type) {
  if ("Function".equals(type.getReferenceName())) {
    currentExpression().getNamedTypeBuilder().setName("Function");
    return null;
  }

  com.github.jsdossier.proto.FunctionType.Builder functionType =
      currentExpression().getFunctionTypeBuilder();

  if (type.isConstructor()) {
    functionType.setIsConstructor(true);
    expressions.addLast(functionType.getInstanceTypeBuilder());
    type.getTypeOfThis().visit(this);
    expressions.removeLast();

  } else if (!type.getTypeOfThis().isUnknownType()
      || type.getTypeOfThis() instanceof NamedType) {
    expressions.addLast(functionType.getInstanceTypeBuilder());
    type.getTypeOfThis().visit(this);
    expressions.removeLast();
  }

  for (Node node : type.getParameters()) {
    TypeExpression.Builder parameterType = functionType.addParameterBuilder();
    expressions.addLast(parameterType);

    if (node.isVarArgs()) {
      parameterType.setIsVarargs(true);
    }

    if (node.getJSType() != null) {
      if (node.getJSType().isUnionType()) {
        caseUnionType((UnionType) node.getJSType(), node.isOptionalArg());
      } else {
        node.getJSType().visit(this);
      }
    }

    if (node.isOptionalArg()) {
      // Not sure if this is possible, but varargs implies optional and we only permit one
      // bit to be set.
      if (!parameterType.getIsVarargs()) {
        parameterType.setIsOptional(true);
      }
    }

    expressions.removeLast();
  }

  if (type.getReturnType() != null && !type.isConstructor()) {
    expressions.addLast(functionType.getReturnTypeBuilder());
    type.getReturnType().visit(this);
    expressions.removeLast();
  }
  return null;
}