/** Returns this object cast to ObjectType or throws an exception if it isn't an ObjectType. */ public ObjectType assertObjectType() { ObjectType result = checkNotNull(toMaybeObjectType(), "Not an ObjectType: %s", this); return result; }
@Nullable private static ObjectType castToObject(@Nullable JSType type) { return type == null ? null : type.toMaybeObjectType(); }
private Set<JSType> getTypesToSkipForTypeNonUnion(JSType type) { Set<JSType> types = new HashSet<>(); JSType skipType = type; while (skipType != null) { types.add(skipType); ObjectType objSkipType = skipType.toMaybeObjectType(); if (objSkipType != null) { skipType = objSkipType.getImplicitPrototype(); } else { break; } } return types; }
private boolean isType(JSType jsType, String fileName) { if (jsType == null) { return false; } jsType = jsType.restrictByNotNullOrUndefined(); if (jsType.toMaybeObjectType() == null) { return false; } String sourceName = getSourceName(jsType); return sourceName != null && sourceName.endsWith(fileName); }
private boolean matchesPrototype(JSType type, JSType maybePrototype) { ObjectType methodClassObjectType = type.toMaybeObjectType(); if (methodClassObjectType != null) { if (methodClassObjectType.getImplicitPrototype().isEquivalentTo(maybePrototype)) { return true; } } return false; }
private boolean matchesPrototype(JSType type, JSType maybePrototype) { ObjectType methodClassObjectType = type.toMaybeObjectType(); if (methodClassObjectType != null) { if (methodClassObjectType.getImplicitPrototype().isEquivalentTo( maybePrototype)) { return true; } } return false; }
/** Returns an integer that uniquely identifies a JSType. */ private int getIntForType(JSType type) { // Templatized types don't exist at runtime, so collapse to raw type if (type != null && type.isGenericObjectType()) { type = type.toMaybeObjectType().getRawType(); } if (intForType.containsKey(type)) { return intForType.get(type).intValue(); } int newInt = intForType.size() + 1; intForType.put(type, newInt); return newInt; }
private static boolean isInstanceOfObject(JSType type) { // Some type whose class is Object ObjectType obj = type.toMaybeObjectType(); if (obj != null && obj.isNativeObjectType() && "Object".equals(obj.getReferenceName())) { return true; } return type.isRecordType() || type.isLiteralObject(); }
private String getSourceName(JSType jsType) { FunctionType constructor = jsType.toMaybeObjectType().getConstructor(); if (constructor == null) { return ""; } return NodeUtil.getSourceName(constructor.getSource()); }
private static JSType removeNullUndefinedAndTemplates(JSType t) { JSType result = t.restrictByNotNullOrUndefined(); ObjectType obj = result.toMaybeObjectType(); if (obj != null && obj.isGenericObjectType()) { return obj.instantiateGenericsWithUnknown(); } return result; }
private JSType evalTemplateTypeOf(Node ttlAst, NameResolver nameResolver) { ImmutableList<Node> params = getCallParams(ttlAst); JSType type = evalInternal(params.get(0), nameResolver); if (!type.isGenericObjectType()) { reportWarning(ttlAst, TEMPTYPE_INVALID, "templateTypeOf", type.toString()); return getUnknownType(); } int index = (int) params.get(1).getDouble(); ImmutableList<? extends JSType> templateTypes = type.toMaybeObjectType().getTemplateTypes(); if (index >= templateTypes.size()) { reportWarning(ttlAst, INDEX_OUTOFBOUNDS, Integer.toString(templateTypes.size()), Integer.toString(index)); return getUnknownType(); } return templateTypes.get(index); }
private JSType evalTemplatizedType(Node ttlAst, NameResolver nameResolver) { ImmutableList<Node> params = getCallParams(ttlAst); JSType firstParam = evalInternal(params.get(0), nameResolver); if (firstParam.isFullyInstantiated()) { reportWarning(ttlAst, BASETYPE_INVALID, firstParam.toString()); return getUnknownType(); } // TODO(lpino): Check that the number of parameters correspond with the // number of template types that the base type can take when creating // a templatized type. For instance, if the base type is Array then there // must be just one parameter. JSType[] templatizedTypes = new JSType[params.size() - 1]; for (int i = 0; i < templatizedTypes.length; i++) { templatizedTypes[i] = evalInternal(params.get(i + 1), nameResolver); } ObjectType baseType = firstParam.toMaybeObjectType(); return createTemplatizedType(baseType, templatizedTypes); }
private ImmutableCollection<String> getTagNames(Node tag) { if (tag.isString()) { return ImmutableSet.of(tag.getString().toLowerCase()); } else if (tag.isGetProp() && tag.getFirstChild().matchesQualifiedName("goog.dom.TagName")) { return ImmutableSet.of(tag.getLastChild().getString().toLowerCase()); } // TODO(jakubvrana): Support union, e.g. {!TagName<!HTMLDivElement>|!TagName<!HTMLBRElement>}. JSType type = tag.getJSType(); if (type == null || !type.isGenericObjectType()) { return null; } ObjectType typeAsObj = type.toMaybeObjectType(); if (typeAsObj.getRawType().getDisplayName().equals("goog.dom.TagName")) { JSType tagType = Iterables.getOnlyElement(typeAsObj.getTemplateTypes()); return ELEMENT_TAG_NAMES.get(tagType.getDisplayName()); } return null; }
private JSType evalRawTypeOf(Node ttlAst, NameResolver nameResolver) { ImmutableList<Node> params = getCallParams(ttlAst); JSType type = evalInternal(params.get(0), nameResolver); if (!type.isGenericObjectType()) { reportWarning(ttlAst, TEMPTYPE_INVALID, "rawTypeOf", type.toString()); return getUnknownType(); } return type.toMaybeObjectType().getRawType(); }
/** * 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(); }
private FunctionType getConstructor(JSType type) { ObjectType objType = type.toMaybeObjectType(); 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; } }
private boolean isClassType(Node n) { ObjectType type = n.getJSType().restrictByNotNullOrUndefined().toMaybeObjectType(); if (type != null && type.isInstanceType()) { FunctionType ctor = type.getConstructor(); if (ctor != null) { JSDocInfo info = ctor.getJSDocInfo(); if (info != null && info.isConstructorOrInterface()) { return true; } } } return false; }
private JSType evalPropType(Node ttlAst, NameResolver nameResolver) { JSType type = evalInternal(getCallArgument(ttlAst, 1), nameResolver); ObjectType objType = type.toMaybeObjectType(); if (objType == null) { reportWarning(ttlAst, PROPTYPE_INVALID, type.toString()); return getUnknownType(); } JSType propType = objType.getPropertyType(getCallArgument(ttlAst, 0).getString()); return firstNonNull(propType, getUnknownType()); }
private boolean isClassType(Node n) { ObjectType type = n.getJSType().restrictByNotNullOrUndefined().toMaybeObjectType(); if (type != null && type.isInstanceType()) { FunctionType ctor = type.getConstructor(); if (ctor != null) { JSDocInfo info = ctor.getJSDocInfo(); if (info != null && info.isConstructorOrInterface()) { return true; } } } return false; }
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()); }