@Override JSType resolveInternal(ErrorReporter reporter) { primitiveType = primitiveType.resolve(reporter); primitiveObjectType = ObjectType.cast(primitiveType); return this; } }
/** * Dereferences the given type to an object, or returns null. */ private static ObjectType dereferenceToObject(JSType type) { return ObjectType.cast(type == null ? null : type.dereference()); }
@Override JSType resolveInternal(ErrorReporter t, StaticTypedScope<JSType> scope) { primitiveType = primitiveType.resolve(t, scope); primitiveObjectType = ObjectType.cast(primitiveType); return this; } }
/** * Returns true if a field reference on this type will invalidate all * references to that field as candidates for renaming. This is true if the * type is unknown or all-inclusive, as variables with such a type could be * references to any object. */ private boolean isInvalidatingType(JSType type) { if (type == null || invalidatingTypes.contains(type) || type.isUnknownType() /* unresolved types */) { return true; } ObjectType objType = ObjectType.cast(type); return objType != null && !objType.hasReferenceName(); }
/** * Infers the type of {@code this}. * @param type The type of this if the info is missing. */ FunctionTypeBuilder inferThisType(JSDocInfo info, JSType type) { // Look at the @this annotation first. inferThisType(info); if (thisType == null) { ObjectType objType = ObjectType.cast(type); if (objType != null && (info == null || !info.hasType())) { thisType = objType; } } return this; }
/** * Infers the type of {@code this}. * @param type The type of this if the info is missing. */ FunctionTypeBuilder inferThisType(JSDocInfo info, JSType type) { // Look at the @this annotation first. inferThisType(info); if (thisType == null) { ObjectType objType = ObjectType.cast(type); if (objType != null && (info == null || !info.hasType())) { thisType = objType; } } return this; }
/** * Coerces this type to an Object type, then gets the type of the property * whose name is given. * * Unlike {@link ObjectType#getPropertyType}, returns null if the property * is not found. * * @return The property's type. {@code null} if the current type cannot * have properties, or if the type is not found. */ public JSType findPropertyType(String propertyName) { ObjectType autoboxObjType = ObjectType.cast(autoboxesTo()); if (autoboxObjType != null) { return autoboxObjType.findPropertyType(propertyName); } return null; }
@Override public boolean apply(JSType type) { ObjectType objectType = ObjectType.cast(type); if (objectType == null) { reportError(BAD_IMPLEMENTED_TYPE, fnName); return false; } else if (objectType.isEmptyType()) { reportWarning(RESOLVED_TAG_EMPTY, "@implements", fnName); return false; } else if (objectType.isUnknownType()) { if (hasMoreTagsToResolve(objectType)) { return true; } else { reportWarning(RESOLVED_TAG_EMPTY, "@implements", fnName); return false; } } else { return true; } } }
/** * Looks up a property on this type, but without properly replacing any templates in the result. * * <p>Subclasses can override this if they need more complicated logic for property lookup than * just autoboxing to an object. * * <p>This is only for use by {@code findPropertyType(JSType)}. Call that method instead if you * need to lookup a property on a random JSType */ @ForOverride @Nullable protected JSType findPropertyTypeWithoutConsideringTemplateTypes(String propertyName) { ObjectType autoboxObjType = ObjectType.cast(autoboxesTo()); if (autoboxObjType != null) { return autoboxObjType.findPropertyType(propertyName); } return null; }
/** * Find the ObjectType associated with the given slot. * @param slotName The name of the slot to find the type in. * @return An object type, or null if this slot does not contain an object. */ private ObjectType getObjectSlot(String slotName) { TypedVar ownerVar = currentScope.getVar(slotName); if (ownerVar != null) { JSType ownerVarType = ownerVar.getType(); return ObjectType.cast( ownerVarType == null ? null : ownerVarType.restrictByNotNullOrUndefined()); } return null; }
/** * Find the ObjectType associated with the given slot. * @param slotName The name of the slot to find the type in. * @return An object type, or null if this slot does not contain an object. */ private ObjectType getObjectSlot(String slotName) { TypedVar ownerVar = scope.getVar(slotName); if (ownerVar != null) { JSType ownerVarType = ownerVar.getType(); return ObjectType.cast(ownerVarType == null ? null : ownerVarType.restrictByNotNullOrUndefined()); } return null; }
@Override public JSType caseTemplatizedType(TemplatizedType type) { boolean changed = false; ObjectType beforeBaseType = type.getReferencedType(); ObjectType afterBaseType = ObjectType.cast(beforeBaseType.visit(this)); if (beforeBaseType != afterBaseType) { changed = true; } ImmutableList.Builder<JSType> builder = ImmutableList.builder(); for (JSType beforeTemplateType : type.getTemplateTypes()) { JSType afterTemplateType = beforeTemplateType.visit(this); if (beforeTemplateType != afterTemplateType) { changed = true; } builder.add(afterTemplateType); } if (changed) { type = registry.createTemplatizedType(afterBaseType, builder.build()); } return type; }
private static JSType getTypeOfThisForScope(NodeTraversal t) { JSType typeOfThis = t.getScopeRoot().getJSType(); if (typeOfThis == null) { return null; } ObjectType objectType = ObjectType.cast(dereference(typeOfThis)); return objectType.getTypeOfThis(); }
/** * Defines a declared property if it has not been defined yet. * * This handles the case where a property is declared on an object where * the object type is inferred, and so the object type will not * be known in {@code TypedScopeCreator}. */ private void ensurePropertyDeclared(Node getprop) { ObjectType ownerType = ObjectType.cast( getJSType(getprop.getFirstChild()).restrictByNotNullOrUndefined()); if (ownerType != null) { ensurePropertyDeclaredHelper(getprop, ownerType); } }
/** * Invalidates the given type, so that no properties on it will be renamed. */ private void addInvalidatingType(JSType type) { type = type.restrictByNotNullOrUndefined(); if (type.isUnionType()) { for (JSType alt : type.toMaybeUnionType().getAlternatesWithoutStructuralTyping()) { addInvalidatingType(alt); } } invalidatingTypes.add(type); ObjectType objType = ObjectType.cast(type); if (objType != null && objType.isInstanceType()) { invalidatingTypes.add(objType.getImplicitPrototype()); } }
/** * Invalidates the given type, so that no properties on it will be renamed. */ private void addInvalidatingType(JSType type) { type = type.restrictByNotNullOrUndefined(); if (type.isUnionType()) { for (JSType alt : type.toMaybeUnionType().getAlternatesWithoutStructuralTyping()) { addInvalidatingType(alt); } } invalidatingTypes.add(type); ObjectType objType = ObjectType.cast(type); if (objType != null && objType.isInstanceType()) { invalidatingTypes.add(objType.getImplicitPrototype()); } }
/** * Gets the type of {@code this} in the current scope. */ @Override public JSType getTypeOfThis() { if (isGlobal()) { return ObjectType.cast(rootNode.getJSType()); } Preconditions.checkState(rootNode.isFunction()); JSType nodeType = rootNode.getJSType(); if (nodeType != null && nodeType.isFunctionType()) { return nodeType.toMaybeFunctionType().getTypeOfThis(); } else { // Executed when the current scope has not been typechecked. return null; } }
private boolean needsPropertyScope(Symbol sym) { ObjectType type = ObjectType.cast(getType(sym)); if (type == null) { return false; } // Anonymous objects if (type.getReferenceName() == null) { return true; } // Constructors/prototypes // Should this check for // (type.isNominalConstructor() || type.isFunctionPrototypeType()) // ? if (sym.getName().equals(type.getReferenceName())) { return true; } // Enums return type.isEnumType() && sym.getName().equals(type.toMaybeEnumType().getElementsType().getReferenceName()); }
private FunctionType getConstructor(JSType type) { ObjectType objType = ObjectType.cast(type); if (objType == null) { return null; } FunctionType constructor = null; if (objType.isFunctionType()) { constructor = objType.toMaybeFunctionType(); } else if (objType.isFunctionPrototypeType()) { constructor = objType.getOwnerFunction(); } else { constructor = objType.getConstructor(); } return constructor != null && constructor.isConstructor() ? constructor : null; } }
/** Returns the AST node associated with the definition, if any. */ private Node getDefinitionNode(QualifiedName qname) { if (qname.isSimple()) { TypedVar var = currentScope.getVar(qname.getComponent()); return var != null ? var.getNameNode() : null; } ObjectType parent = ObjectType.cast(lookupQualifiedName(qname.getOwner())); return parent != null ? parent.getPropertyDefSite(qname.getComponent()) : null; }