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;
}
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.");
}
}
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));
}
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));
}
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;
}
Aggregations