private Set<String> getEmittablePropertyNames(final ObjectType type) { return sorted( Sets.filter( type.getOwnPropertyNames(), propName -> isEmittableProperty(type, propName) && !isTypeCheckSuppressedProperty(type, propName))); }
/** * 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()); }
private 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 getObjectType(); } ImmutableMap.Builder<String, JSType> props = new ImmutableMap.Builder<String, JSType>(); // Otherwise collect the properties and build a record type for (String propName : propNames) { props.put(propName, objType.getPropertyType(propName)); } return createRecordType(props.build()); }
&& propertyNames.size() <= MAX_PRETTY_PRINTED_PROPERTIES; current = current.getImplicitPrototype()) { propertyNames.addAll(current.getOwnPropertyNames());
private static void updatePropertyTypeMap( FunctionType type, Map<String, JSType> propTypeMap, HashSet<FunctionType> cache) { if (type == null) { return; } // retrieve all property types on the prototype of this class ObjectType prototype = type.getPrototype(); if (prototype != null) { Set<String> propNames = prototype.getOwnPropertyNames(); for (String name : propNames) { if (!propTypeMap.containsKey(name)) { JSType propType = prototype.getPropertyType(name); propTypeMap.put(name, propType); } } } // retrieve all property types from its super class Iterable<ObjectType> iterable = type.getExtendedInterfaces(); if (iterable != null) { for (ObjectType interfaceType : iterable) { FunctionType superConstructor = interfaceType.getConstructor(); if (superConstructor == null || cache.contains(superConstructor)) { continue; } cache.add(superConstructor); updatePropertyTypeMap(superConstructor, propTypeMap, cache); cache.remove(superConstructor); } } }
private static void updatePropertyTypeMap( FunctionType type, Map<String, JSType> propTypeMap, HashSet<FunctionType> cache) { if (type == null) { return; } // retrieve all property types on the prototype of this class ObjectType prototype = type.getPrototype(); if (prototype != null) { Set<String> propNames = prototype.getOwnPropertyNames(); for (String name : propNames) { if (!propTypeMap.containsKey(name)) { JSType propType = prototype.getPropertyType(name); propTypeMap.put(name, propType); } } } // retrieve all property types from its super class Iterable<ObjectType> iterable = type.getExtendedInterfaces(); if (iterable != null) { for (ObjectType interfaceType : iterable) { FunctionType superConstructor = interfaceType.getConstructor(); if (superConstructor == null || cache.contains(superConstructor)) { continue; } cache.add(superConstructor); updatePropertyTypeMap(superConstructor, propTypeMap, cache); cache.remove(superConstructor); } } }
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); } } } }
@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; }
private static ConformanceResult checkCtorProperties(ObjectType type) { for (String propertyName : type.getOwnPropertyNames()) { Property prop = type.getOwnSlot(propertyName); JSDocInfo docInfo = prop.getJSDocInfo(); Node scriptNode = getScriptNode(prop.getNode()); ConformanceResult result = visibilityAtDeclarationOrFileoverview( docInfo, scriptNode); if (result != ConformanceResult.CONFORMANCE) { return result; } } return ConformanceResult.CONFORMANCE; }
@Override public JSType caseObjectType(ObjectType objType) { if (!visitProperties || objType.isNominalType() || objType instanceof ProxyObjectType || !objType.isRecordType()) { return objType; } if (seenTypes.contains(objType)) { return objType; } seenTypes.add(objType); boolean changed = false; RecordTypeBuilder builder = new RecordTypeBuilder(registry); for (String prop : objType.getOwnPropertyNames()) { Node propertyNode = objType.getPropertyNode(prop); JSType beforeType = objType.getPropertyType(prop); JSType afterType = beforeType.visit(this); if (beforeType != afterType) { changed = true; } builder.addProperty(prop, afterType, propertyNode); } seenTypes.remove(objType); if (changed) { return builder.build(); } return objType; }
@Override public JSType caseObjectType(ObjectType objType) { if (!visitProperties || objType.isNominalType() || objType instanceof ProxyObjectType || !objType.isRecordType()) { return objType; } if (seenTypes.contains(objType)) { return objType; } seenTypes.add(objType); boolean changed = false; RecordTypeBuilder builder = new RecordTypeBuilder(registry); for (String prop : objType.getOwnPropertyNames()) { Node propertyNode = objType.getPropertyNode(prop); JSType beforeType = objType.getPropertyType(prop); JSType afterType = beforeType.visit(this); if (beforeType != afterType) { changed = true; } builder.addProperty(prop, afterType, propertyNode); } seenTypes.remove(objType); if (changed) { return builder.build(); } return objType; }
/** * 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); } } } }
public void matchRecordTypeConstraint(ObjectType constraintObj) { for (String prop : constraintObj.getOwnPropertyNames()) { JSType propType = constraintObj.getPropertyType(prop); if (!isPropertyTypeDeclared(prop)) { JSType typeToInfer = propType; if (!hasProperty(prop)) { typeToInfer = getNativeType(JSTypeNative.VOID_TYPE) .getLeastSupertype(propType); } defineInferredProperty(prop, typeToInfer, null); } } }
/** * 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); } } } }
public void matchRecordTypeConstraint(ObjectType constraintObj) { for (String prop : constraintObj.getOwnPropertyNames()) { JSType propType = constraintObj.getPropertyType(prop); if (!isPropertyTypeDeclared(prop)) { JSType typeToInfer = propType; if (!hasProperty(prop)) { typeToInfer = getNativeType(JSTypeNative.VOID_TYPE) .getLeastSupertype(propType); } defineInferredProperty(prop, typeToInfer, null); } } }
/** * Creates the name in the graph if it does not already exist. Also puts all * the properties and prototype properties of this name in the graph. */ private Name recordClassConstructorOrInterface( String name, FunctionType type, @Nullable Node n, @Nullable Node rhs) { Preconditions.checkArgument(type.isConstructor() || type.isInterface()); Name symbol = graph.defineNameIfNotExists(name, isExtern); if (rhs != null) { // TODO(user): record the definition. symbol.setType(getType(rhs)); if (n.isAssign()) { symbol.addAssignmentDeclaration(n); } else { symbol.addFunctionDeclaration(n); } } ObjectType prototype = type.getPrototype(); for (String prop : prototype.getOwnPropertyNames()) { graph.defineNameIfNotExists( name + ".prototype." + prop, isExtern); } return symbol; } }
ObjectType superType = currSuperCtor.getInstanceType(); for (String prop : currSuperCtor.getInstanceType().getImplicitPrototype().getOwnPropertyNames()) { FunctionType maybeAbstractMethod = superType.findPropertyType(prop).toMaybeFunctionType(); if (maybeAbstractMethod != null
private boolean isDefaultExport(TypedVar symbol) { if (symbol.getType() == null) return true; ObjectType otype = symbol.getType().toMaybeObjectType(); if (otype != null && otype.getOwnPropertyNames().size() == 0) return true; return !symbol.getType().isObject() || symbol.getType().isInterface() || symbol.getType().isInstanceType() || symbol.getType().isEnumType() || symbol.getType().isFunctionType() || isTypedef(symbol.getType()); }
for (String propertyName : patternObjectType.getOwnPropertyNames()) { if (!otherKeys.contains(propertyName)) { builder.addProperty(