Java Code Examples for com.google.javascript.rhino.jstype.ObjectType#getImplicitPrototype()

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#getImplicitPrototype() . 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: TypedScopeCreatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testAbstractMethod() {
  testSame(
      "/** @type {!Function} */ var abstractMethod;" +
      "/** @constructor */ function Foo() {}" +
      "/** @param {number} x */ Foo.prototype.bar = abstractMethod;");
  assertEquals(
      "Function", findNameType("abstractMethod", globalScope).toString());

  FunctionType ctor = (FunctionType) findNameType("Foo", globalScope);
  ObjectType instance = ctor.getInstanceType();
  assertEquals("Foo", instance.toString());

  ObjectType proto = instance.getImplicitPrototype();
  assertEquals("Foo.prototype", proto.toString());

  assertEquals(
      "function (this:Foo, number): ?",
      proto.getPropertyType("bar").toString());
}
 
Example 2
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Invalidates the given type, so that no properties on it will be renamed.
 */
private void addInvalidatingType(JSType type) {
  type = type.restrictByNotNullOrUndefined();
  if (type instanceof UnionType) {
    for (JSType alt : ((UnionType) type).getAlternates()) {
      addInvalidatingType(alt);
    }
    return;
  }

  typeSystem.addInvalidatingType(type);
  ObjectType objType = ObjectType.cast(type);
  if (objType != null && objType.getImplicitPrototype() != null) {
    typeSystem.addInvalidatingType(objType.getImplicitPrototype());
  }
}
 
Example 3
Source File: Closure_41_FunctionTypeBuilder_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Check whether a type is resolvable in the future
 * If this has a supertype that hasn't been resolved yet, then we can assume
 * this type will be ok once the super type resolves.
 * @param objectType
 * @return true if objectType is resolvable in the future
 */
private static boolean hasMoreTagsToResolve(ObjectType objectType) {
  Preconditions.checkArgument(objectType.isUnknownType());
  if (objectType.getImplicitPrototype() != null) {
    // constructor extends class
    if (objectType.getImplicitPrototype().isResolved()) {
      return false;
    } else {
      return true;
    }
  } else {
    // interface extends interfaces
    FunctionType ctor = objectType.getConstructor();
    if (ctor != null) {
      for (ObjectType interfaceType : ctor.getExtendedInterfaces()) {
        if (!interfaceType.isResolved()) {
          return true;
        }
      }
    }
    return false;
  }
}
 
Example 4
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 property if it is marked
 * as being deprecated. Returns empty string if the property is deprecated
 * but no reason was given. Returns null if the property is not deprecated.
 */
private static String getPropertyDeprecationInfo(ObjectType type,
                                                 String prop) {
  JSDocInfo info = type.getOwnPropertyJSDocInfo(prop);
  if (info != null && info.isDeprecated()) {
    if (info.getDeprecationReason() != null) {
      return info.getDeprecationReason();
    }

    return "";
  }
  ObjectType implicitProto = type.getImplicitPrototype();
  if (implicitProto != null) {
    return getPropertyDeprecationInfo(implicitProto, prop);
  }
  return null;
}
 
Example 5
Source File: Closure_90_FunctionTypeBuilder_t.java    From coming with MIT License 6 votes vote down vote up
@Override
public boolean apply(JSType type) {
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null) {
    reportWarning(EXTENDS_NON_OBJECT, fnName, type.toString());
  } else if (objectType.isUnknownType() &&
      // If this has a supertype that hasn't been resolved yet,
      // then we can assume this type will be ok once the super
      // type resolves.
      (objectType.getImplicitPrototype() == null ||
       objectType.getImplicitPrototype().isResolved())) {
    reportWarning(RESOLVED_TAG_EMPTY, "@extends", fnName);
  } else {
    return true;
  }
  return false;
}
 
Example 6
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implicitCast annotation for
 * the given property.
 */
private boolean propertyIsImplicitCast(ObjectType type, String prop) {
  for (; type != null; type = type.getImplicitPrototype()) {
    JSDocInfo docInfo = type.getOwnPropertyJSDocInfo(prop);
    if (docInfo != null && docInfo.isImplicitCast()) {
      return true;
    }
  }
  return false;
}
 
Example 7
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that all properties on interfaces that this type implements are
 * implemented and correctly typed.
 */
void expectAllInterfaceProperties(NodeTraversal t, Node n,
    FunctionType type) {
  ObjectType instance = type.getInstanceType();
  for (ObjectType implemented : type.getAllImplementedInterfaces()) {
    if (implemented.getImplicitPrototype() != null) {
      for (String prop :
           implemented.getImplicitPrototype().getOwnPropertyNames()) {
        expectInterfaceProperty(t, n, instance, implemented, prop);
      }
    }
  }
}
 
Example 8
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
  if (implicitProto == null) {
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = ImmutableSet.of();
  } else {
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  }
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 9
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
  if (implicitProto == null) {
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = ImmutableSet.of();
  } else {
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  }
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 10
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implicitCast annotation for
 * the given property.
 */
private static boolean propertyIsImplicitCast(ObjectType type, String prop) {
  for (; type != null; type = type.getImplicitPrototype()) {
    JSDocInfo docInfo = type.getOwnPropertyJSDocInfo(prop);
    if (docInfo != null && docInfo.isImplicitCast()) {
      return true;
    }
  }
  return false;
}
 
Example 11
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implicitCast annotation for
 * the given property.
 */
private boolean propertyIsImplicitCast(ObjectType type, String prop) {
  for (; type != null; type = type.getImplicitPrototype()) {
    JSDocInfo docInfo = type.getOwnPropertyJSDocInfo(prop);
    if (docInfo != null && docInfo.isImplicitCast()) {
      return true;
    }
  }
  return false;
}
 
Example 12
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implictCast annotation for
 * the given property.
 */
private boolean propertyIsImplicitCast(ObjectType type, String prop) {
  for (; type != null; type = type.getImplicitPrototype()) {
    JSDocInfo docInfo = type.getOwnPropertyJSDocInfo(prop);
    if (docInfo != null && docInfo.isImplicitCast()) {
      return true;
    }
  }
  return false;
}
 
Example 13
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Set<JSType> getTypesToSkipForTypeNonUnion(JSType type) {
  Set<JSType> types = Sets.newHashSet();
  JSType skipType = type;
  while (skipType != null) {
    types.add(skipType);

    ObjectType objSkipType = skipType.toObjectType();
    if (objSkipType != null) {
      skipType = objSkipType.getImplicitPrototype();
    } else {
      break;
    }
  }
  return types;
}
 
Example 14
Source File: Nopol2017_0029_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Check whether there's any property conflict for for a particular super
 * interface
 * @param t The node traversal object that supplies context
 * @param n The node being visited
 * @param functionName The function name being checked
 * @param properties The property names in the super interfaces that have
 * been visited
 * @param currentProperties The property names in the super interface
 * that have been visited
 * @param interfaceType The super interface that is being visited
 */
private void checkInterfaceConflictProperties(NodeTraversal t, Node n,
    String functionName, HashMap<String, ObjectType> properties,
    HashMap<String, ObjectType> currentProperties,
    ObjectType interfaceType) {
  ObjectType implicitProto = interfaceType.getImplicitPrototype();
  Set<String> currentPropertyNames;
    // This can be the case if interfaceType is proxy to a non-existent
    // object (which is a bad type annotation, but shouldn't crash).
    currentPropertyNames = implicitProto.getOwnPropertyNames();
  for (String name : currentPropertyNames) {
    ObjectType oType = properties.get(name);
    if (oType != null) {
      if (!interfaceType.getPropertyType(name).isEquivalentTo(
          oType.getPropertyType(name))) {
        compiler.report(
            t.makeError(n, INCOMPATIBLE_EXTENDED_PROPERTY_TYPE,
                functionName, name, oType.toString(),
                interfaceType.toString()));
      }
    }
    currentProperties.put(name, interfaceType);
  }
  for (ObjectType iType : interfaceType.getCtorExtendedInterfaces()) {
    checkInterfaceConflictProperties(t, n, functionName, properties,
        currentProperties, iType);
  }
}
 
Example 15
Source File: Closure_11_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if any type in the chain has an implicitCast annotation for
 * the given property.
 */
private boolean propertyIsImplicitCast(ObjectType type, String prop) {
  for (; type != null; type = type.getImplicitPrototype()) {
    JSDocInfo docInfo = type.getOwnPropertyJSDocInfo(prop);
    if (docInfo != null && docInfo.isImplicitCast()) {
      return true;
    }
  }
  return false;
}
 
Example 16
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
@Override public ObjectType getTypeWithProperty(String field, JSType type) {
  if (!(type instanceof ObjectType)) {
    if (type.autoboxesTo() != null) {
      type = type.autoboxesTo();
    } else {
      return null;
    }
  }

  // Ignore the prototype itself at all times.
  if ("prototype".equals(field)) {
    return null;
  }

  // We look up the prototype chain to find the highest place (if any) that
  // this appears.  This will make references to overriden properties look
  // like references to the initial property, so they are renamed alike.
  ObjectType foundType = null;
  ObjectType objType = ObjectType.cast(type);
  while (objType != null && objType.getImplicitPrototype() != objType) {
    if (objType.hasOwnProperty(field)) {
      foundType = objType;
    }
    objType = objType.getImplicitPrototype();
  }
  // If the property does not exist on the referenced type but the original
  // type is an object type, see if any subtype has the property.
  if (foundType == null) {
    ObjectType maybeType = ObjectType.cast(
        registry.getGreatestSubtypeWithProperty(type, field));
    // getGreatestSubtypeWithProperty does not guarantee that the property
    // is defined on the returned type, it just indicates that it might be,
    // so we have to double check.
    if (maybeType != null && maybeType.hasOwnProperty(field)) {
      foundType = maybeType;
    }
  }
  return foundType;
}
 
Example 17
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
private ObjectType getSuperType(FunctionType type) {
  ObjectType proto = type.getPrototype();
  if (proto == null) return null;
  ObjectType implicitProto = proto.getImplicitPrototype();
  if (implicitProto == null) return null;
  return "Object".equals(implicitProto.getDisplayName()) ? null : implicitProto;
}
 
Example 18
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();
  }
}
 
Example 19
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determines whether the given property with @const tag got reassigned
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkConstantProperty(NodeTraversal t,
    Node getprop) {
  // Check whether the property is modified
  Node parent = getprop.getParent();
  boolean isDelete = parent.isDelProp();
  if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop)
      && !parent.isInc() && !parent.isDec()
      && !isDelete) {
    return;
  }

  ObjectType objectType =
    ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  boolean isConstant = isPropertyDeclaredConstant(objectType, propertyName);

  // Check whether constant properties are reassigned
  if (isConstant) {
    if (isDelete) {
      compiler.report(
          t.makeError(getprop, CONST_PROPERTY_DELETED, propertyName));
      return;
    }

    ObjectType oType = objectType;
    while (oType != null) {
      if (oType.hasReferenceName()) {
        if (initializedConstantProperties.containsEntry(
                oType.getReferenceName(), propertyName)) {
          compiler.report(
              t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE,
                  propertyName));
          break;
        }
      }
      oType = oType.getImplicitPrototype();
    }

    Preconditions.checkState(objectType.hasReferenceName());
    initializedConstantProperties.put(objectType.getReferenceName(),
        propertyName);

    // Add the prototype when we're looking at an instance object
    if (objectType.isInstanceType()) {
      ObjectType prototype = objectType.getImplicitPrototype();
      if (prototype != null) {
        if (prototype.hasProperty(propertyName)
            && prototype.hasReferenceName()) {
          initializedConstantProperties.put(prototype.getReferenceName(),
              propertyName);
        }
      }
    }
  }
}
 
Example 20
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property is visible in the current context.
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkPropertyVisibility(NodeTraversal t,
    Node getprop, Node parent) {
  ObjectType objectType =
      ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  if (objectType != null) {
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = t.inGlobalScope() &&
        parent.getType() == Token.ASSIGN &&
        parent.getFirstChild() == getprop;

    // Find the lowest property defined on a class with visibility
    // information.
    if (isOverride) {
      objectType = objectType.getImplicitPrototype();
    }
    JSDocInfo docInfo = null;
    for (; objectType != null;
         objectType = objectType.getImplicitPrototype()) {
      docInfo = objectType.getOwnPropertyJSDocInfo(propertyName);
      if (docInfo != null &&
          docInfo.getVisibility() != Visibility.INHERITED) {
        break;
      }
    }

    if (objectType == null) {
      // We couldn't find a visibility modifier; assume it's public.
      return;
    }

    boolean sameInput =
        t.getInput().getName().equals(docInfo.getSourceName());
    Visibility visibility = docInfo.getVisibility();
    JSType ownerType = normalizeClassType(objectType);
    if (isOverride) {
      // Check an ASSIGN statement that's trying to override a property
      // on a superclass.
      JSDocInfo overridingInfo = parent.getJSDocInfo();
      Visibility overridingVisibility = overridingInfo == null ?
          Visibility.INHERITED : overridingInfo.getVisibility();

      // Check that (a) the property *can* be overridden, and
      // (b) that the visibility of the override is the same as the
      // visibility of the original property.
      if (visibility == Visibility.PRIVATE && !sameInput) {
        compiler.report(
            t.makeError(getprop, PRIVATE_OVERRIDE,
                objectType.toString()));
      } else if (overridingVisibility != Visibility.INHERITED &&
          overridingVisibility != visibility) {
        compiler.report(
            t.makeError(getprop, VISIBILITY_MISMATCH,
                visibility.name(), objectType.toString(),
                overridingVisibility.name()));
      }
    } else {
      if (sameInput) {
        // private access is always allowed in the same file.
        return;
      } else if (visibility == Visibility.PRIVATE &&
          (currentClass == null || ownerType.differsFrom(currentClass))) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        // private access is not allowed outside the file from a different
        // enclosing class.
        compiler.report(
            t.makeError(getprop,
                BAD_PRIVATE_PROPERTY_ACCESS,
                propertyName,
                validator.getReadableJSTypeName(
                    getprop.getFirstChild(), true)));
      } else if (visibility == Visibility.PROTECTED) {
        // There are 3 types of legal accesses of a protected property:
        // 1) Accesses in the same file
        // 2) Overriding the property in a subclass
        // 3) Accessing the property from inside a subclass
        // The first two have already been checked for.
        if (currentClass == null || !currentClass.isSubtype(ownerType)) {
          compiler.report(
              t.makeError(getprop,  BAD_PROTECTED_PROPERTY_ACCESS,
                  propertyName,
                  validator.getReadableJSTypeName(
                      getprop.getFirstChild(), true)));
        }
      }
    }
  }
}