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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isFunctionPrototypeType() . 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_117_TypeValidator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Expect that the first type can be assigned to a symbol of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param rightType The type on the RHS of the assign.
 * @param leftType The type of the symbol on the LHS of the assign.
 * @param owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.isSubtype(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 2
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Expect that the first type can be assigned to a symbol of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param rightType The type on the RHS of the assign.
 * @param leftType The type of the symbol on the LHS of the assign.
 * @param owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.isSubtype(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 3
Source File: TypedCodeGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private String getTypeAnnotation(Node node) {
  // Only add annotations for things with JSDoc, or function literals.
  JSDocInfo jsdoc = NodeUtil.getBestJSDocInfo(node);
  if (jsdoc == null && !node.isFunction()) {
    return "";
  }

  JSType type = node.getJSType();
  if (type == null) {
    return "";
  } else if (type.isFunctionType()) {
    return getFunctionAnnotation(node);
  } else if (type.isEnumType()) {
    return "/** @enum {" +
        type.toMaybeEnumType().getElementsType().toAnnotationString() +
        "} */\n";
  } else if (!type.isUnknownType()
      && !type.isEmptyType()
      && !type.isVoidType()
      && !type.isFunctionPrototypeType()) {
    return "/** @type {" + node.getJSType().toAnnotationString() + "} */\n";
  } else {
    return "";
  }
}
 
Example 4
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Expect that the first type can be assigned to a symbol of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param rightType The type on the RHS of the assign.
 * @param leftType The type of the symbol on the LHS of the assign.
 * @param owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.isSubtype(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 5
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Expect that the first type can be assigned to a symbol of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param rightType The type on the RHS of the assign.
 * @param leftType The type of the symbol on the LHS of the assign.
 * @param owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.canAssignTo(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 6
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override public JSType getInstanceFromPrototype(JSType type) {
  if (type.isFunctionPrototypeType()) {
    ObjectType prototype = (ObjectType) type;
    FunctionType owner = prototype.getOwnerFunction();
    if (owner.isConstructor() || owner.isInterface()) {
      return prototype.getOwnerFunction().getInstanceType();
    }
  }
  return null;
}
 
Example 7
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Returns true for types that are class like, i.e. that define a constructor or interface, but
 * excludes typedefs and the built-in constructor functions such as {@code Function}.
 */
private boolean isClassLike(JSType propType) {
  // Confusingly, the typedef type returns true on isConstructor checks, so we need to filter
  // the NoType through this utility method.
  return !isTypedef(propType)
      && (propType.isConstructor() || propType.isInterface())
      // "Function" is a constructor, but does not define a new type for our purposes.
      && !propType.toMaybeObjectType().isNativeObjectType()
      && !propType.isFunctionPrototypeType();
}
 
Example 8
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Normalize the type of a constructor, its instance, and its prototype
 * all down to the same type (the instance type).
 */
private JSType normalizeClassType(JSType type) {
  if (type == null || type.isUnknownType()) {
    return type;
  } else if (type.isNominalConstructor()) {
    return (type.toMaybeFunctionType()).getInstanceType();
  } else if (type.isFunctionPrototypeType()) {
    FunctionType owner = ((ObjectType) type).getOwnerFunction();
    if (owner.isConstructor()) {
      return owner.getInstanceType();
    }
  }
  return type;
}
 
Example 9
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Normalize the type of a constructor, its instance, and its prototype
 * all down to the same type (the instance type).
 */
private JSType normalizeClassType(JSType type) {
  if (type == null || type.isUnknownType()) {
    return type;
  } else if (type.isConstructor()) {
    return ((FunctionType) type).getInstanceType();
  } else if (type.isFunctionPrototypeType()) {
    FunctionType owner = ((FunctionPrototypeType) type).getOwnerFunction();
    if (owner.isConstructor()) {
      return owner.getInstanceType();
    }
  }
  return type;
}
 
Example 10
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Normalize the type of a constructor, its instance, and its prototype
 * all down to the same type (the instance type).
 */
private JSType normalizeClassType(JSType type) {
  if (type == null || type.isUnknownType()) {
    return type;
  } else if (type.isConstructor()) {
    return ((FunctionType) type).getInstanceType();
  } else if (type.isFunctionPrototypeType()) {
    FunctionType owner = ((FunctionPrototypeType) type).getOwnerFunction();
    if (owner.isConstructor()) {
      return owner.getInstanceType();
    }
  }
  return type;
}
 
Example 11
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the first type can be assigned to a symbol of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param rightType The type on the RHS of the assign.
 * @param leftType The type of the symbol on the LHS of the assign.
 * @param owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.canAssignTo(leftType)) {
    if ((leftType.isConstructor() || leftType.isEnumType()) && (rightType.isConstructor() || rightType.isEnumType())) {
      registerMismatch(rightType, leftType, null);
    } else {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    }
    return false;
  }
  return true;
}
 
Example 12
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override public JSType getInstanceFromPrototype(JSType type) {
  if (type.isFunctionPrototypeType()) {
    ObjectType prototype = (ObjectType) type;
    FunctionType owner = prototype.getOwnerFunction();
    if (owner.isConstructor() || owner.isInterface()) {
      return prototype.getOwnerFunction().getInstanceType();
    }
  }
  return null;
}
 
Example 13
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
@Override public JSType getInstanceFromPrototype(JSType type) {
  if (type.isFunctionPrototypeType()) {
    ObjectType prototype = (ObjectType) type;
    FunctionType owner = prototype.getOwnerFunction();
    if (owner.isConstructor() || owner.isInterface()) {
      return prototype.getOwnerFunction().getInstanceType();
    }
  }
  return null;
}
 
Example 14
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
@Override public JSType getInstanceFromPrototype(JSType type) {
  if (type.isFunctionPrototypeType()) {
    FunctionPrototypeType prototype = (FunctionPrototypeType) type;
    FunctionType owner = prototype.getOwnerFunction();
    if (owner.isConstructor() || owner.isInterface()) {
      return ((FunctionPrototypeType) type).getOwnerFunction()
          .getInstanceType();
    }
  }
  return null;
}
 
Example 15
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override public JSType getInstanceFromPrototype(JSType type) {
  if (type.isFunctionPrototypeType()) {
    FunctionPrototypeType prototype = (FunctionPrototypeType) type;
    FunctionType owner = prototype.getOwnerFunction();
    if (owner.isConstructor() || owner.isInterface()) {
      return ((FunctionPrototypeType) type).getOwnerFunction()
          .getInstanceType();
    }
  }
  return null;
}
 
Example 16
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  } else if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 17
Source File: AmbiguateProperties.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds subtypes - and implementors, in the case of interfaces - of the type
 * to its JSTypeBitSet of related types. Union types are decomposed into their
 * alternative types.
 *
 * <p>The 'is related to' relationship is best understood graphically. Draw an
 * arrow from each instance type to the prototype of each of its
 * subclass. Draw an arrow from each prototype to its instance type. Draw an
 * arrow from each interface to its implementors. A type is related to another
 * if there is a directed path in the graph from the type to other. Thus, the
 * 'is related to' relationship is reflexive and transitive.
 *
 * <p>Example with Foo extends Bar which extends Baz and Bar implements I:
 * <pre>
 * Foo -> Bar.prototype -> Bar -> Baz.prototype -> Baz
 *                          ^
 *                          |
 *                          I
 * </pre>
 *
 * <p>Note that we don't need to correctly handle the relationships between
 * functions, because the function type is invalidating (i.e. its properties
 * won't be ambiguated).
 */
private void computeRelatedTypes(JSType type) {
  if (type.isUnionType()) {
    type = type.restrictByNotNullOrUndefined();
    if (type.isUnionType()) {
      for (JSType alt : type.toMaybeUnionType().getAlternates()) {
        computeRelatedTypes(alt);
      }
      return;
    }
  }

  if (relatedBitsets.containsKey(type)) {
    // We only need to generate the bit set once.
    return;
  }

  JSTypeBitSet related = new JSTypeBitSet(intForType.size());
  relatedBitsets.put(type, related);
  related.set(getIntForType(type));

  // A prototype is related to its instance.
  if (type.isFunctionPrototypeType()) {
    addRelatedInstance(((ObjectType) type).getOwnerFunction(), related);
    return;
  }

  // An instance is related to its subclasses.
  FunctionType constructor = type.toObjectType().getConstructor();
  if (constructor != null && constructor.getSubTypes() != null) {
    for (FunctionType subType : constructor.getSubTypes()) {
      addRelatedInstance(subType, related);
    }
  }

  // An interface is related to its implementors.
  for (FunctionType implementor : compiler.getTypeRegistry()
      .getDirectImplementors(type.toObjectType())) {
    addRelatedInstance(implementor, related);
  }
}
 
Example 18
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  } else if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 19
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {

  // The best type name is the actual type name.

  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  }
  String qualifiedName = n.getQualifiedName();
  if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 20
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  // The best type name is the actual type name.
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  }

  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}