@Override public final FunctionType getConstructor() { return referencedObjType == null ? null : referencedObjType.getConstructor(); }
/** * We treat this as the unknown type if any of its implicit prototype * properties is unknown. */ @Override public boolean isUnknownType() { // If the object is unknown now, check the supertype again, // because it might have been resolved since the last check. if (unknown) { ObjectType implicitProto = getImplicitPrototype(); if (implicitProto == null || implicitProto.isNativeObjectType()) { unknown = false; for (ObjectType interfaceType : getCtorExtendedInterfaces()) { if (interfaceType.isUnknownType()) { unknown = true; break; } } } else { unknown = implicitProto.isUnknownType(); } } return unknown; }
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; }
public FunctionType getSuperClassConstructor() { ObjectType iproto = getImplicitPrototype(); if (iproto == null) { return null; } iproto = iproto.getImplicitPrototype(); return iproto == null ? null : iproto.getConstructor(); }
/** Returns the direct parent of this property map. */ PropertyMap getPrimaryParent() { if (parentSource == null) { return null; } ObjectType iProto = parentSource.getImplicitPrototype(); return iProto == null ? null : iProto.getPropertyMap(); }
public final ObjectType getTopDefiningInterface(String propertyName) { ObjectType foundType = null; if (hasProperty(propertyName)) { foundType = this; } for (ObjectType interfaceType : getCtorExtendedInterfaces()) { if (interfaceType.hasProperty(propertyName)) { foundType = interfaceType.getTopDefiningInterface(propertyName); } } return foundType; }
/** * Returns true iff {@code this} can be a {@code dict}. * UnionType overrides the method, assume {@code this} is not a union here. */ public boolean isDict() { if (isObject()) { ObjectType objType = toObjectType(); ObjectType iproto = objType.getImplicitPrototype(); // For the case when a @dict constructor is assigned to a function's // prototype property if (iproto != null && iproto.isDict()) { return true; } FunctionType ctor = objType.getConstructor(); // This test is true for object literals if (ctor == null) { JSDocInfo info = objType.getJSDocInfo(); return info != null && info.makesDicts(); } else { return ctor.makesDicts(); } } return false; }
/** * Returns true iff {@code this} can be a {@code struct}. * UnionType overrides the method, assume {@code this} is not a union here. */ public boolean isStruct() { if (isObject()) { ObjectType objType = toObjectType(); ObjectType iproto = objType.getImplicitPrototype(); // For the case when a @struct constructor is assigned to a function's // prototype property if (iproto != null && iproto.isStruct()) { return true; } FunctionType ctor = objType.getConstructor(); // This test is true for object literals if (ctor == null) { JSDocInfo info = objType.getJSDocInfo(); return info != null && info.makesStructs(); } else { return ctor.makesStructs(); } } return false; }
if (objType == null || objType.getJSDocInfo() != null) { return; if (owningType != null) { String propName = n.getString(); if (owningType.hasOwnProperty(propName)) { owningType.setPropertyJSDocInfo(propName, docInfo); if (lhsType.hasOwnProperty(propName)) { lhsType.setPropertyJSDocInfo(propName, docInfo); dereferenceToObject(lhsType.getPropertyType(propName)); if (propType != null) { attachJSDocInfoToNominalTypeOrShape(
/** * 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; }
public Void emitObjectType( ObjectType type, boolean extendingInstanceClass, boolean inExtendsImplementsPosition) { if (!type.getTemplateTypeMap().isEmpty() && !typeRegistry.getNativeType(OBJECT_TYPE).equals(type)) { return emitTemplatizedType( typeRegistry.createTemplatizedType(type), inExtendsImplementsPosition); String maybeGlobalName = maybeRenameGlobalType(type.getDisplayName()); if (maybeGlobalName != null) { emit(maybeGlobalName); } else if (type.isDict()) { emit("{[key: string]: any}"); } else if (type.getReferenceName() != null) { String name = getAbsoluteName(type); if (!type.getDisplayName().equals("Object")) { typesUsed.add(type.getDisplayName());
@Override public Set<String> caseObjectType(ObjectType type) { // Record types. // {a: T1, b: T2}. if (type.isRecordType()) { for (String propertyName : type.getOwnPropertyNames()) { // After type inference it is possible that some nodes in externs // can have types which are defined in non-extern code. To avoid // bleeding property names of such types into externs we check that // the node for each property was defined in externs. if (type.getPropertyNode(propertyName).isFromExterns()) { externProperties.add(propertyName); visitOnce(type.getPropertyType(propertyName)); } } } return externProperties; }
ObjectType.cast(dereference(n.getFirstFirstChild() .getJSType())); String propertyName = n.getFirstChild().getLastChild().getString(); objectType = objectType.getImplicitPrototype(); if (objectType == null) { break; if (objectType.getDisplayName().endsWith("prototype")) { continue; di = objectType.getOwnPropertyJSDocInfo(propertyName); if (di != null) { if (fieldIsPrivate || di.getVisibility() == Visibility.PRIVATE) {
/** * Look up the visibility of the overridden property recursively. In Closure, a child class can * override an implicit public property with tighter visibility, but it is not allowed in * TypeScript. So clutz ignores and emits it as a public property. */ private boolean isProtectedProperty(ObjectType prototype, final String propName) { Visibility visibility = Visibility.INHERITED; while (prototype != null) { if (prototype.hasOwnProperty(propName)) { final JSDocInfo jsDocInfo = prototype.getOwnPropertyJSDocInfo(propName); if (jsDocInfo != null) { visibility = jsDocInfo.getVisibility(); } } prototype = prototype.getImplicitPrototype(); } return visibility == Visibility.PROTECTED; }
private void aggregateFieldsFromClass(Set<String> fields, ObjectType superType) { // visit instance properties. for (String field : superType.getOwnPropertyNames()) { if (!superType.getPropertyType(field).isFunctionType()) { fields.add(field); } } // visit prototype properties. if (superType.getConstructor() != null && superType.getConstructor().getPrototype() != null && superType.getConstructor().getPrototype().getOwnPropertyNames() != null) { for (String field : superType.getConstructor().getPrototype().getOwnPropertyNames()) { // getPropertyType works with non-owned property names, i.e. names from the prototype // chain. if (!superType.getPropertyType(field).isFunctionType()) { fields.add(field); } } } }
if (isExtern && isStatic && isLikelyNamespace(type.getOwnPropertyJSDocInfo(propName))) { return false; String qualifiedName = type.getDisplayName() + "." + propName; if (provides.contains(qualifiedName)) { return false; JSType propertyType = type.getPropertyType(propName); if (isDefiningType(propertyType)) {
private void addRelatedInterfaces(ObjectType instance, Set<ObjectType> interfaces) { FunctionType constructor = instance.getConstructor(); if (constructor != null && constructor.isInterface() && !interfaces.contains(instance)) { interfaces.add(instance); for (ObjectType interfaceType : instance.getCtorExtendedInterfaces()) { addRelatedInterfaces(interfaceType, interfaces); } } }
/** * Merges a list of record types. * Example * {r:{s:string, n:number}} and {a:boolean} * is transformed into {r:{s:string, n:number}, a:boolean} */ private JSType joinRecordTypes(ImmutableList<ObjectType> recTypes) { Map<String, JSType> props = new LinkedHashMap<>(); for (ObjectType recType : recTypes) { for (String newPropName : recType.getOwnPropertyNames()) { JSType newPropValue = recType.getPropertyType(newPropName); // Put the new property depending if it already exists in the map putNewPropInPropertyMap(props, newPropName, newPropValue); } } return createRecordType(ImmutableMap.copyOf(props)); }
public JSType buildRecordTypeFromObject(ObjectType objType) { RecordType recType = objType.toMaybeRecordType(); // If it can be casted to a record type then return if (recType != null) { return recType; } // TODO(lpino): Handle inherited properties Set<String> propNames = objType.getOwnPropertyNames(); // If the type has no properties then return Object if (propNames.isEmpty()) { return getNativeType(JSTypeNative.OBJECT_TYPE); } ImmutableMap.Builder<String, JSType> props = new ImmutableMap.Builder<>(); // Otherwise collect the properties and build a record type for (String propName : propNames) { props.put(propName, objType.getPropertyType(propName)); } return createRecordType(props.build()); }
/** * Detects cycles in either the implicit prototype chain, or the implemented/extended * interfaces.<p> * * @return True iff a cycle was detected. */ final boolean detectInheritanceCycle() { // TODO(dimvar): This should get moved to preventing cycles in FunctionTypeBuilder // rather than removing them here after they have been created. // Also, this doesn't do the right thing for extended interfaces, though that is // masked by another bug. return detectImplicitPrototypeCycle() || Iterables.contains(this.getCtorImplementedInterfaces(), this) || Iterables.contains(this.getCtorExtendedInterfaces(), this); }