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

The following examples show how to use com.google.javascript.rhino.JSDocInfo#getReturnType() . 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: TypeExpressionParserTest.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Test
public void parseExpressionWithTemplatizedType() {
  util.compile(
      createSourceFile(
          fs.getPath("source/global.js"),
          "/** @template T */",
          "class Container {}",
          "class Person {",
          "  /** @return {!Container<string>} . */",
          "  name() { return new Container; }",
          "}"));

  NominalType type = typeRegistry.getType("Person");
  JSDocInfo info =
      type.getType().toMaybeFunctionType().getPrototype().getOwnPropertyJSDocInfo("name");
  JSTypeExpression jsExpression = info.getReturnType();
  JSType jsType = util.evaluate(jsExpression);

  TypeExpressionParser parser =
      parserFactory.create(linkFactoryBuilder.create(type).withTypeContext(type));
  TypeExpression expression = parser.parse(jsType);
  assertThat(expression)
      .isEqualTo(
          TypeExpression.newBuilder()
              .setNamedType(
                  namedType("Container", "Container.html")
                      .toBuilder()
                      .addTemplateType(stringType()))
              .build());
}
 
Example 2
Source File: TypeExpressionParserTest.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Test
public void parseExpressionWithTemplatizedTypeFromAnotherModule() {
  util.compile(
      createSourceFile(
          fs.getPath("source/modules/one.js"), "/** @template T */", "export class Container {}"),
      createSourceFile(
          fs.getPath("source/modules/two.js"),
          "import {Container} from './one';",
          "export class Person {",
          "  /** @return {!Container<string>} . */",
          "  name() { return new Container; }",
          "}"));

  NominalType type = typeRegistry.getType("module$source$modules$two.Person");
  JSDocInfo info =
      type.getType().toMaybeFunctionType().getPrototype().getOwnPropertyJSDocInfo("name");
  JSTypeExpression jsExpression = info.getReturnType();
  JSType jsType = util.evaluate(jsExpression);

  TypeExpressionParser parser =
      parserFactory.create(linkFactoryBuilder.create(type).withTypeContext(type));
  TypeExpression expression = parser.parse(jsType);
  assertThat(expression)
      .isEqualTo(
          TypeExpression.newBuilder()
              .setNamedType(
                  namedType("Container", "one.Container", "one_exports_Container.html")
                      .toBuilder()
                      .addTemplateType(stringType()))
              .build());
}
 
Example 3
Source File: DeclarationGenerator.java    From clutz with MIT License 4 votes vote down vote up
/**
 * Closure has an experimental feature - Type Transformation Expression (TTE) - used to type
 * functions like Promise.then and Promise.all. The feature is rarely used and impossible to
 * translate to TS, so we just hard code some type signature. Yuk!
 *
 * <p>This is a hack and won't scale, but I hope TTE will have very limited usage.
 *
 * <p>Returns whether this was a TTE function and handled specially.
 */
private boolean handleSpecialTTEFunctions(
    JSType type, String propName, boolean isStatic, List<String> classTemplateTypeNames) {
  FunctionType ftype = type.toMaybeFunctionType();
  if (ftype == null) return false;

  boolean hasTTE = false;
  for (TemplateType templateKey : ftype.getTemplateTypeMap().getTemplateKeys()) {
    if (templateKey.getTypeTransformation() != null) {
      hasTTE = true;
      break;
    }
  }

  // Horrible hack.  goog.async.Deferred has an @override of a TTE function, but because we
  // run with partial inputs we can't see that.  Identify it by grabbing:
  // 1) functions named .then()
  // 2) that use @override
  // 3) that have no declared parameters/return type.
  boolean horribleHackForOverrides = false;
  JSDocInfo info = type.getJSDocInfo();
  if (info != null) {
    boolean isUntypedOverride =
        info.isOverride() && info.getParameterCount() == 0 && info.getReturnType() == null;
    if (isUntypedOverride && propName.equals("then")) {
      horribleHackForOverrides = true;
    }
  }

  if (!horribleHackForOverrides && !hasTTE) return false;

  // The same signature can be found in a number of classes - es6 Promise, angular.$q.Promise,
  // custom Thenable classes, etc. While the class names differ the implementations are close
  // enough that we use the same signature for all of them.
  // Only customization needed is plugging in the correct class name.
  String templateTypeSig =
      isStatic
          ? getSignatureForStaticTTEFn(propName, ftype)
          : getSignatureForInstanceTTEFn(propName, classTemplateTypeNames, ftype);
  if (templateTypeSig == null) {
    emit("/* function had TTE, but not a known translation. Emitted type is likely wrong. */");
    emitBreak();
    return false;
  }
  emit(templateTypeSig);
  emitBreak();
  return true;
}