Search in sources :

Example 6 with ObjectTypeI

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

the class AmbiguateProperties method addRelatedInstance.

/**
 * Adds the instance of the given constructor, its implicit prototype and all
 * its related types to the given bit set.
 */
private void addRelatedInstance(FunctionTypeI constructor, JSTypeBitSet related) {
    checkArgument(constructor.hasInstanceType(), "Constructor %s without instance type.", constructor);
    ObjectTypeI instanceType = constructor.getInstanceType();
    related.set(getIntForType(instanceType.getPrototypeObject()));
    computeRelatedTypes(instanceType);
    related.or(relatedBitsets.get(instanceType));
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

Example 7 with ObjectTypeI

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

the class DisambiguateProperties method recordInterfaces.

/**
 * Records that this property could be referenced from any interface that
 * this type inherits from.
 *
 * If the property p is defined only on a subtype of constructor, then this
 * method has no effect. But we tried modifying getTypeWithProperty to tell us
 * when the returned type is a subtype, and then skip those calls to
 * recordInterface, and there was no speed-up.
 * And it made the code harder to understand, so we don't do it.
 */
private void recordInterfaces(FunctionTypeI constructor, TypeI relatedType, Property p) {
    Iterable<ObjectTypeI> interfaces = ancestorInterfaces.get(constructor);
    if (interfaces == null) {
        interfaces = constructor.getAncestorInterfaces();
        ancestorInterfaces.put(constructor, interfaces);
    }
    for (ObjectTypeI itype : interfaces) {
        TypeI top = getTypeWithProperty(p.name, itype);
        if (top != null) {
            p.addType(itype, relatedType);
        }
        // If this interface invalidated this property, return now.
        if (p.skipRenaming) {
            return;
        }
    }
}
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 8 with ObjectTypeI

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

the class DisambiguateProperties method getTypeAlternatives.

/**
 * Returns the alternatives if this is a type that represents multiple
 * types, and null if not. Union and interface types can correspond to
 * multiple other types.
 */
private Iterable<? extends TypeI> getTypeAlternatives(TypeI type) {
    if (type.isUnionType()) {
        return type.getUnionMembers();
    } else {
        ObjectTypeI objType = type.toMaybeObjectType();
        FunctionTypeI constructor = objType != null ? objType.getConstructor() : null;
        if (constructor != null && constructor.isInterface()) {
            List<TypeI> list = new ArrayList<>();
            for (FunctionTypeI impl : constructor.getDirectSubTypes()) {
                list.add(impl.getInstanceType());
            }
            return list.isEmpty() ? null : list;
        } else {
            return null;
        }
    }
}
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) ArrayList(java.util.ArrayList) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 9 with ObjectTypeI

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

the class CheckAccessControls method checkConstantProperty.

/**
 * Determines whether the given constant property got reassigned
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkConstantProperty(NodeTraversal t, Node getprop) {
    // Check whether the property is modified
    Node parent = getprop.getParent();
    boolean isDelete = parent.isDelProp();
    if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop) && !parent.isInc() && !parent.isDec() && !isDelete) {
        return;
    }
    ObjectTypeI objectType = castToObject(dereference(getprop.getFirstChild().getTypeI()));
    String propertyName = getprop.getLastChild().getString();
    boolean isConstant = isPropertyDeclaredConstant(objectType, propertyName);
    // Check whether constant properties are reassigned
    if (isConstant) {
        JSDocInfo info = parent.getJSDocInfo();
        if (info != null && info.getSuppressions().contains("const")) {
            return;
        }
        if (isDelete) {
            compiler.report(t.makeError(getprop, CONST_PROPERTY_DELETED, propertyName));
            return;
        }
        // the type is being inspected incorrectly.
        if (objectType == null || (objectType.isFunctionType() && !objectType.toMaybeFunctionType().isConstructor())) {
            return;
        }
        ObjectTypeI oType = objectType;
        while (oType != null) {
            if (initializedConstantProperties.containsEntry(oType, propertyName) || initializedConstantProperties.containsEntry(getCanonicalInstance(oType), propertyName)) {
                compiler.report(t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE, propertyName));
                break;
            }
            oType = oType.getPrototypeObject();
        }
        initializedConstantProperties.put(objectType, propertyName);
        // Add the prototype when we're looking at an instance object
        if (objectType.isInstanceType()) {
            ObjectTypeI prototype = objectType.getPrototypeObject();
            if (prototype != null && prototype.hasProperty(propertyName)) {
                initializedConstantProperties.put(prototype, propertyName);
            }
        }
    }
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Node(com.google.javascript.rhino.Node) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 10 with ObjectTypeI

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

the class Es6ToEs3Util method createGenericType.

/**
 * Returns the TypeI as specified by the typeName and instantiated by the typeArg.
 * Returns null if shouldCreate is false.
 */
static TypeI createGenericType(boolean shouldCreate, TypeIRegistry registry, JSTypeNative typeName, TypeI typeArg) {
    if (!shouldCreate) {
        return null;
    }
    ObjectTypeI genericType = (ObjectTypeI) (registry.getNativeType(typeName));
    ObjectTypeI uninstantiated = genericType.getRawType();
    return registry.instantiateGenericType(uninstantiated, ImmutableList.of(typeArg));
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

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