Search in sources :

Example 16 with TypeI

use of com.google.javascript.rhino.TypeI 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 17 with TypeI

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

the class Es6TemplateLiterals method visitTemplateLiteral.

/**
 * Converts `${a} b ${c} d ${e}` to (a + " b " + c + " d " + e)
 *
 * @param n A TEMPLATELIT node that is not prefixed with a tag
 */
static void visitTemplateLiteral(NodeTraversal t, Node n, boolean addTypes) {
    TypeIRegistry registry = t.getCompiler().getTypeIRegistry();
    TypeI stringType = createType(addTypes, registry, JSTypeNative.STRING_TYPE);
    int length = n.getChildCount();
    if (length == 0) {
        n.replaceWith(withType(IR.string("\"\""), stringType));
    } else {
        Node first = n.removeFirstChild();
        checkState(first.isString());
        if (length == 1) {
            n.replaceWith(first);
        } else {
            // Add the first string with the first substitution expression
            Node add = withType(IR.add(first, n.removeFirstChild().removeFirstChild()), n.getTypeI());
            // Process the rest of the template literal
            for (int i = 2; i < length; i++) {
                Node child = n.removeFirstChild();
                if (child.isString()) {
                    if (child.getString().isEmpty()) {
                        continue;
                    } else if (i == 2 && first.getString().isEmpty()) {
                        // So that `${hello} world` gets translated into (hello + " world")
                        // instead of ("" + hello + " world").
                        add = add.getSecondChild().detach();
                    }
                }
                add = withType(IR.add(add, child.isString() ? child : child.removeFirstChild()), n.getTypeI());
            }
            n.replaceWith(add.useSourceInfoIfMissingFromForTree(n));
        }
    }
    t.reportCodeChange();
}
Also used : TypeI(com.google.javascript.rhino.TypeI) Node(com.google.javascript.rhino.Node) TypeIRegistry(com.google.javascript.rhino.TypeIRegistry)

Example 18 with TypeI

use of com.google.javascript.rhino.TypeI 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 19 with TypeI

use of com.google.javascript.rhino.TypeI 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 20 with TypeI

use of com.google.javascript.rhino.TypeI 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)

Aggregations

TypeI (com.google.javascript.rhino.TypeI)37 ObjectTypeI (com.google.javascript.rhino.ObjectTypeI)24 FunctionTypeI (com.google.javascript.rhino.FunctionTypeI)23 Node (com.google.javascript.rhino.Node)10 TypeIRegistry (com.google.javascript.rhino.TypeIRegistry)4 LinkedHashMap (java.util.LinkedHashMap)4 JSType (com.google.javascript.jscomp.newtypes.JSType)3 JSDocInfo (com.google.javascript.rhino.JSDocInfo)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)1 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)1 JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)1 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)1 Token (com.google.javascript.rhino.Token)1 ArrayList (java.util.ArrayList)1