Search in sources :

Example 16 with ObjectTypeI

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

the class TypeTransformation method joinRecordTypes.

/**
 * 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 TypeI joinRecordTypes(ImmutableList<ObjectTypeI> recTypes) {
    Map<String, TypeI> props = new LinkedHashMap<>();
    for (ObjectTypeI recType : recTypes) {
        for (String newPropName : recType.getOwnPropertyNames()) {
            TypeI 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));
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) LinkedHashMap(java.util.LinkedHashMap)

Example 17 with ObjectTypeI

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

the class TypeMismatch method removeNullUndefinedAndTemplates.

private static TypeI removeNullUndefinedAndTemplates(TypeI t) {
    TypeI result = t.restrictByNotNullOrUndefined();
    ObjectTypeI obj = result.toMaybeObjectType();
    if (obj != null && obj.isGenericObjectType()) {
        return obj.instantiateGenericsWithUnknown();
    }
    return result;
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 18 with ObjectTypeI

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

the class CheckAccessControls method getSuperClassInstanceIfFinal.

/**
 * If the superclass is final, this method returns an instance of the superclass.
 */
@Nullable
private static ObjectTypeI getSuperClassInstanceIfFinal(@Nullable TypeI type) {
    if (type != null) {
        ObjectTypeI obj = castToObject(type);
        FunctionTypeI ctor = obj == null ? null : obj.getSuperClassConstructor();
        JSDocInfo doc = ctor == null ? null : ctor.getJSDocInfo();
        if (doc != null && doc.isFinal()) {
            return ctor.getInstanceType();
        }
    }
    return null;
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Nullable(javax.annotation.Nullable)

Example 19 with ObjectTypeI

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

the class CheckAccessControls method getPropertyDeprecationInfo.

/**
 * Returns the deprecation reason for the property if it is marked
 * as being deprecated. Returns empty string if the property is deprecated
 * but no reason was given. Returns null if the property is not deprecated.
 */
@Nullable
private static String getPropertyDeprecationInfo(ObjectTypeI type, String prop) {
    String depReason = getDeprecationReason(type.getOwnPropertyJSDocInfo(prop));
    if (depReason != null) {
        return depReason;
    }
    ObjectTypeI implicitProto = type.getPrototypeObject();
    if (implicitProto != null) {
        return getPropertyDeprecationInfo(implicitProto, prop);
    }
    return null;
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Nullable(javax.annotation.Nullable)

Example 20 with ObjectTypeI

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

the class Es6ForOfConverter method visitForOf.

private void visitForOf(NodeTraversal t, Node node, Node parent) {
    Node variable = node.removeFirstChild();
    Node iterable = node.removeFirstChild();
    Node body = node.removeFirstChild();
    TypeI typeParam = unknownType;
    if (addTypes) {
        // TODO(sdh): This is going to be null if the iterable is nullable or unknown. We might want
        // to consider some way of unifying rather than simply looking at the nominal type.
        ObjectTypeI iterableType = iterable.getTypeI().autobox().toMaybeObjectType();
        if (iterableType != null) {
            TypeIRegistry registry = compiler.getTypeIRegistry();
            TypeI iterableBaseType = registry.getNativeType(JSTypeNative.ITERABLE_TYPE);
            typeParam = iterableType.getInstantiatedTypeArgument(iterableBaseType);
        }
    }
    TypeI iteratorType = createGenericType(JSTypeNative.ITERATOR_TYPE, typeParam);
    TypeI iIterableResultType = createGenericType(JSTypeNative.I_ITERABLE_RESULT_TYPE, typeParam);
    TypeI iteratorNextType = addTypes ? iteratorType.toMaybeObjectType().getPropertyType("next") : null;
    JSDocInfo varJSDocInfo = variable.getJSDocInfo();
    Node iterName = withType(IR.name(ITER_BASE + compiler.getUniqueNameIdSupplier().get()), iteratorType);
    iterName.makeNonIndexable();
    Node getNext = withType(IR.call(withType(IR.getprop(iterName.cloneTree(), withStringType(IR.string("next"))), iteratorNextType)), iIterableResultType);
    String variableName;
    Token declType;
    if (variable.isName()) {
        declType = Token.NAME;
        variableName = variable.getQualifiedName();
    } else {
        Preconditions.checkState(NodeUtil.isNameDeclaration(variable), "Expected var, let, or const. Got %s", variable);
        declType = variable.getToken();
        variableName = variable.getFirstChild().getQualifiedName();
    }
    Node iterResult = withType(IR.name(ITER_RESULT + variableName), iIterableResultType);
    iterResult.makeNonIndexable();
    Node call = Es6ToEs3Util.makeIterator(compiler, iterable);
    if (addTypes) {
        TypeI jscompType = t.getScope().getVar("$jscomp").getNode().getTypeI();
        TypeI makeIteratorType = jscompType.toMaybeObjectType().getPropertyType("makeIterator");
        call.getFirstChild().setTypeI(makeIteratorType);
        call.getFirstFirstChild().setTypeI(jscompType);
    }
    Node init = IR.var(iterName.cloneTree(), withType(call, iteratorType));
    Node initIterResult = iterResult.cloneTree();
    initIterResult.addChildToFront(getNext.cloneTree());
    init.addChildToBack(initIterResult);
    Node cond = withBooleanType(IR.not(withBooleanType(IR.getprop(iterResult.cloneTree(), withStringType(IR.string("done"))))));
    Node incr = withType(IR.assign(iterResult.cloneTree(), getNext.cloneTree()), iIterableResultType);
    Node declarationOrAssign;
    if (declType == Token.NAME) {
        declarationOrAssign = withType(IR.assign(withType(IR.name(variableName).useSourceInfoFrom(variable), typeParam), withType(IR.getprop(iterResult.cloneTree(), withStringType(IR.string("value"))), typeParam)), typeParam);
        declarationOrAssign.setJSDocInfo(varJSDocInfo);
        declarationOrAssign = IR.exprResult(declarationOrAssign);
    } else {
        declarationOrAssign = new Node(declType, withType(IR.name(variableName).useSourceInfoFrom(variable.getFirstChild()), typeParam));
        declarationOrAssign.getFirstChild().addChildToBack(withType(IR.getprop(iterResult.cloneTree(), withStringType(IR.string("value"))), typeParam));
        declarationOrAssign.setJSDocInfo(varJSDocInfo);
    }
    Node newBody = IR.block(declarationOrAssign, body).useSourceInfoFrom(body);
    Node newFor = IR.forNode(init, cond, incr, newBody);
    newFor.useSourceInfoIfMissingFromForTree(node);
    parent.replaceChild(node, newFor);
    compiler.reportChangeToEnclosingScope(newFor);
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Node(com.google.javascript.rhino.Node) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Token(com.google.javascript.rhino.Token) TypeIRegistry(com.google.javascript.rhino.TypeIRegistry) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

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