Search in sources :

Example 11 with ObjectTypeI

use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.

the class AccessControlUtils method getEffectiveVisibilityForNonOverriddenProperty.

/**
 * Returns the effective visibility of the given non-overridden property.
 * Non-overridden properties without an explicit visibility annotation
 * receive the default visibility declared in the file's {@code @fileoverview}
 * block, if one exists.
 */
private static Visibility getEffectiveVisibilityForNonOverriddenProperty(Node getprop, ObjectTypeI objectType, @Nullable Visibility fileOverviewVisibility, @Nullable CodingConvention codingConvention) {
    String propertyName = getprop.getLastChild().getString();
    if (codingConvention != null && codingConvention.isPrivate(propertyName)) {
        return Visibility.PRIVATE;
    }
    Visibility raw = Visibility.INHERITED;
    if (objectType != null) {
        raw = objectType.getOwnPropertyJSDocInfo(propertyName).getVisibility();
    }
    TypeI type = getprop.getTypeI();
    boolean createdFromGoogProvide = (type != null && type.isLiteralObject());
    // every a.b.* namespace effectively package-private.
    return (raw != Visibility.INHERITED || fileOverviewVisibility == null || createdFromGoogProvide) ? raw : fileOverviewVisibility;
}
Also used : TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Visibility(com.google.javascript.rhino.JSDocInfo.Visibility)

Example 12 with ObjectTypeI

use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.

the class DisambiguateProperties method getTypeWithProperty.

/**
 * Returns the type in the chain from the given type that contains the given
 * field or null if it is not found anywhere.
 * Can return a subtype of the input type.
 */
private ObjectTypeI getTypeWithProperty(String field, TypeI type) {
    if (type == null) {
        return null;
    }
    ObjectTypeI foundType = gtwpCacheGet(field, type);
    if (foundType != null) {
        return foundType.equals(BOTTOM_OBJECT) ? null : foundType;
    }
    if (type.isEnumElement()) {
        foundType = getTypeWithProperty(field, type.getEnumeratedTypeOfEnumElement());
        gtwpCachePut(field, type, foundType == null ? BOTTOM_OBJECT : foundType);
        return foundType;
    }
    if (!type.isObjectType()) {
        if (type.isBoxableScalar()) {
            foundType = getTypeWithProperty(field, type.autobox());
            gtwpCachePut(field, type, foundType == null ? BOTTOM_OBJECT : foundType);
            return foundType;
        } else {
            gtwpCachePut(field, type, BOTTOM_OBJECT);
            return null;
        }
    }
    // Ignore the prototype itself at all times.
    if ("prototype".equals(field)) {
        gtwpCachePut(field, type, BOTTOM_OBJECT);
        return null;
    }
    // We look up the prototype chain to find the highest place (if any) that
    // this appears.  This will make references to overridden properties look
    // like references to the initial property, so they are renamed alike.
    ObjectTypeI objType = type.toMaybeObjectType();
    if (objType != null && objType.getConstructor() != null && objType.getConstructor().isInterface()) {
        ObjectTypeI topInterface = objType.getTopDefiningInterface(field);
        if (topInterface != null && topInterface.getConstructor() != null) {
            foundType = topInterface.getPrototypeObject();
        }
    } else {
        while (objType != null && !Objects.equals(objType.getPrototypeObject(), objType)) {
            if (objType.hasOwnProperty(field)) {
                foundType = objType;
            }
            objType = objType.getPrototypeObject();
        }
    }
    // type is an object type, see if any subtype has the property.
    if (foundType == null) {
        TypeI subtypeWithProp = type.getGreatestSubtypeWithProperty(field);
        ObjectTypeI maybeType = subtypeWithProp == null ? null : subtypeWithProp.toMaybeObjectType();
        // so we have to double check.
        if (maybeType != null && maybeType.hasOwnProperty(field)) {
            foundType = maybeType;
        }
    }
    // Unwrap templatized types, they are not unique at runtime.
    if (foundType != null && foundType.isGenericObjectType()) {
        foundType = foundType.getRawType();
    }
    // so that the returned type has the correct name.
    if (foundType != null && foundType.isLegacyNamedType()) {
        foundType = foundType.getLegacyResolvedType().toMaybeObjectType();
    }
    gtwpCachePut(field, type, foundType == null ? BOTTOM_OBJECT : foundType);
    return foundType;
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

Example 13 with ObjectTypeI

use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.

the class DisambiguateProperties method getTypesToSkipForTypeNonUnion.

private Set<TypeI> getTypesToSkipForTypeNonUnion(TypeI type) {
    Set<TypeI> types = new HashSet<>();
    TypeI skipType = type;
    while (skipType != null) {
        types.add(skipType);
        ObjectTypeI objSkipType = skipType.toMaybeObjectType();
        if (objSkipType != null) {
            skipType = objSkipType.getPrototypeObject();
        } else {
            break;
        }
    }
    return types;
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) HashSet(java.util.HashSet)

Example 14 with ObjectTypeI

use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.

the class DisambiguateProperties method getConstructor.

private FunctionTypeI getConstructor(TypeI type) {
    ObjectTypeI objType = type.toMaybeObjectType();
    if (objType == null) {
        return null;
    }
    FunctionTypeI constructor = null;
    if (objType.isFunctionType()) {
        constructor = objType.toMaybeFunctionType();
    } else if (objType.isPrototypeObject()) {
        constructor = objType.getOwnerFunction();
    } else {
        constructor = objType.getConstructor();
    }
    return constructor;
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 15 with ObjectTypeI

use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.

the class TypedCodeGenerator method appendInterfaceAnnotations.

private void appendInterfaceAnnotations(StringBuilder sb, FunctionTypeI funType) {
    Set<String> interfaces = new TreeSet<>();
    for (ObjectTypeI interfaceType : funType.getAncestorInterfaces()) {
        interfaces.add(interfaceType.toAnnotationString(Nullability.IMPLICIT));
    }
    for (String interfaze : interfaces) {
        sb.append(" * ");
        appendAnnotation(sb, "extends", interfaze);
        sb.append("\n");
    }
    if (funType.isStructuralInterface()) {
        sb.append(" * @record\n");
    } else {
        sb.append(" * @interface\n");
    }
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) TreeSet(java.util.TreeSet)

Aggregations

ObjectTypeI (com.google.javascript.rhino.ObjectTypeI)22 TypeI (com.google.javascript.rhino.TypeI)10 FunctionTypeI (com.google.javascript.rhino.FunctionTypeI)8 JSDocInfo (com.google.javascript.rhino.JSDocInfo)4 Node (com.google.javascript.rhino.Node)4 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)3 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)2 TreeSet (java.util.TreeSet)2 Nullable (javax.annotation.Nullable)2 Token (com.google.javascript.rhino.Token)1 TypeIRegistry (com.google.javascript.rhino.TypeIRegistry)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1