Java Code Examples for com.google.javascript.rhino.jstype.JSType#getJSDocInfo()

The following examples show how to use com.google.javascript.rhino.jstype.JSType#getJSDocInfo() . 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_71_CheckAccessControls_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Returns the deprecation reason for the type if it is marked
 * as being deprecated. Returns empty string if the type is deprecated
 * but no reason was given. Returns null if the type is not deprecated.
 */
private static String getTypeDeprecationInfo(JSType type) {
  if (type == null) {
    return null;
  }

  JSDocInfo info = type.getJSDocInfo();
  if (info != null && info.isDeprecated()) {
    if (info.getDeprecationReason() != null) {
      return info.getDeprecationReason();
    }
    return "";
  }
  ObjectType objType = ObjectType.cast(type);
  if (objType != null) {
    ObjectType implicitProto = objType.getImplicitPrototype();
    if (implicitProto != null) {
      return getTypeDeprecationInfo(implicitProto);
    }
  }
  return null;
}
 
Example 2
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Returns the deprecation reason for the type if it is marked
 * as being deprecated. Returns empty string if the type is deprecated
 * but no reason was given. Returns null if the type is not deprecated.
 */
private static String getTypeDeprecationInfo(JSType type) {
  if (type == null) {
    return null;
  }

  JSDocInfo info = type.getJSDocInfo();
  if (info != null && info.isDeprecated()) {
    if (info.getDeprecationReason() != null) {
      return info.getDeprecationReason();
    }
    return "";
  }
  ObjectType objType = ObjectType.cast(type);
  if (objType != null) {
    ObjectType implicitProto = objType.getImplicitPrototype();
    if (implicitProto != null) {
      return getTypeDeprecationInfo(implicitProto);
    }
  }
  return null;
}
 
Example 3
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the deprecation reason for the type if it is marked
 * as being deprecated. Returns empty string if the type is deprecated
 * but no reason was given. Returns null if the type is not deprecated.
 */
private static String getTypeDeprecationInfo(JSType type) {
  if (type == null) {
    return null;
  }

  JSDocInfo info = type.getJSDocInfo();
  if (info != null && info.isDeprecated()) {
    if (info.getDeprecationReason() != null) {
      return info.getDeprecationReason();
    }
    return "";
  }
  ObjectType objType = ObjectType.cast(type);
  if (objType != null) {
    ObjectType implicitProto = objType.getImplicitPrototype();
    if (implicitProto != null) {
      return getTypeDeprecationInfo(implicitProto);
    }
  }
  return null;
}
 
Example 4
Source File: NodeModulePassTest.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Test
public void savesOriginalTypeNameInJsDoc() {
  CompilerUtil compiler = createCompiler(path("foo.js"));

  compiler.compile(
      createSourceFile(
          path("foo.js"),
          "/** @constructor */",
          "var Builder = function(){};",
          "/** @return {!Builder} . */",
          "Builder.prototype.returnThis = function() { return this; };",
          "exports.Builder = Builder"));

  TypedScope scope = compiler.getCompiler().getTopScope();
  TypedVar var = scope.getVar("module$exports$module$foo");
  JSType type = var.getInitialValue().getJSType().findPropertyType("Builder");
  assertTrue(type.isConstructor());

  type = type.toObjectType().getTypeOfThis();
  assertEquals("module$exports$module$foo.Builder", type.toString());

  type = type.toObjectType().getPropertyType("returnThis");
  assertTrue(type.toString(), type.isFunctionType());

  JSDocInfo info = type.getJSDocInfo();
  assertNotNull(info);

  Node node = getOnlyElement(info.getTypeNodes());
  assertEquals(Token.BANG, node.getToken());

  node = node.getFirstChild();
  assertTrue(node.isString());
  assertEquals("module$exports$module$foo.Builder", node.getString());
  assertEquals("Builder", node.getProp(Node.ORIGINALNAME_PROP));
}
 
Example 5
Source File: NodeModulePassTest.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Test
public void canUseModuleInternalTypedefsInJsDoc() {
  CompilerUtil compiler = createCompiler(path("foo.js"));

  compiler.compile(
      createSourceFile(
          path("foo.js"),
          "/** @typedef {{x: number}} */",
          "var Variable;",
          "",
          "/**",
          " * @param {Variable} a .",
          " * @param {Variable} b .",
          " * @return {Variable} .",
          " */",
          "exports.add = function(a, b) {",
          "  return {x: a.x + b.x};",
          "};"));

  TypedScope scope = compiler.getCompiler().getTopScope();
  TypedVar var = scope.getVar("module$exports$module$foo");
  JSType type = var.getInitialValue().getJSType().toObjectType().getPropertyType("add");
  assertTrue(type.isFunctionType());

  JSDocInfo info = type.getJSDocInfo();
  Node node = info.getTypeNodes().iterator().next();
  assertTrue(node.isString());
  assertEquals("module$contents$module$foo_Variable", node.getString());
  assertEquals("Variable", node.getProp(Node.ORIGINALNAME_PROP));
}
 
Example 6
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;
}
 
Example 7
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private boolean shouldConvertToConstructor(JSType type) {
  JSDocInfo info = type.getJSDocInfo();
  return info != null && (info.isInterface() || info.isConstructor());
}