Search in sources :

Example 21 with TypeI

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

the class DisambiguateProperties method buildPropNames.

/**
 * Chooses a name to use for renaming in each equivalence class and maps
 * the representative type of that class to that name.
 */
private Map<TypeI, String> buildPropNames(Property prop) {
    UnionFind<TypeI> pTypes = prop.getTypes();
    String pname = prop.name;
    Map<TypeI, String> names = new HashMap<>();
    for (Set<TypeI> set : pTypes.allEquivalenceClasses()) {
        checkState(!set.isEmpty());
        TypeI representative = pTypes.find(set.iterator().next());
        String typeName = null;
        for (TypeI type : set) {
            String typeString = type.toString();
            if (typeName == null || typeString.compareTo(typeName) < 0) {
                typeName = typeString;
            }
        }
        String newName;
        if ("{...}".equals(typeName)) {
            newName = pname;
        } else {
            newName = NONWORD_PATTERN.matcher(typeName).replaceAll("_") + '$' + pname;
        }
        names.put(representative, newName);
    }
    return names;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

Example 22 with TypeI

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

the class DisambiguateProperties method renameProperties.

/**
 * Renames all properties with references on more than one type.
 */
void renameProperties() {
    int propsRenamed = 0;
    int propsSkipped = 0;
    int instancesRenamed = 0;
    int instancesSkipped = 0;
    int singleTypeProps = 0;
    Set<String> reported = new HashSet<>();
    for (Property prop : properties.values()) {
        if (prop.shouldRename()) {
            UnionFind<TypeI> pTypes = prop.getTypes();
            Map<TypeI, String> propNames = buildPropNames(prop);
            ++propsRenamed;
            prop.expandTypesToSkip();
            // different places in the code.
            for (Map.Entry<Node, TypeI> entry : prop.rootTypesByNode.entrySet()) {
                Node node = entry.getKey();
                TypeI rootType = entry.getValue();
                if (prop.shouldRename(rootType)) {
                    String newName = propNames.get(pTypes.find(rootType));
                    node.setString(newName);
                    compiler.reportChangeToEnclosingScope(node);
                    ++instancesRenamed;
                } else {
                    ++instancesSkipped;
                    CheckLevel checkLevelForProp = propertiesToErrorFor.get(prop.name);
                    if (checkLevelForProp != null && checkLevelForProp != CheckLevel.OFF && !reported.contains(prop.name)) {
                        reported.add(prop.name);
                        compiler.report(JSError.make(node, checkLevelForProp, Warnings.INVALIDATION_ON_TYPE, prop.name, rootType.toString(), ""));
                    }
                }
            }
        } else {
            if (prop.skipRenaming) {
                ++propsSkipped;
            } else {
                ++singleTypeProps;
            }
        }
    }
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Renamed " + instancesRenamed + " instances of " + propsRenamed + " properties.");
        logger.fine("Skipped renaming " + instancesSkipped + " invalidated " + "properties, " + propsSkipped + " instances of properties " + "that were skipped for specific types and " + singleTypeProps + " properties that were referenced from only one type.");
    }
}
Also used : FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Node(com.google.javascript.rhino.Node) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashSet(java.util.HashSet)

Example 23 with TypeI

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

the class ProcessDefines method isValidDefineType.

/**
 * Only defines of literal number, string, or boolean are supported.
 */
private boolean isValidDefineType(JSTypeExpression expression) {
    TypeIRegistry registry = compiler.getTypeIRegistry();
    TypeI type = registry.evaluateTypeExpressionInGlobalScope(expression);
    return !type.isUnknownType() && type.isSubtypeOf(registry.getNativeType(NUMBER_STRING_BOOLEAN));
}
Also used : TypeI(com.google.javascript.rhino.TypeI) TypeIRegistry(com.google.javascript.rhino.TypeIRegistry)

Example 24 with TypeI

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

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

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