Skip to content

Commit 4f69363

Browse files
lauraharkercopybara-github
authored andcommitted
Remove reference types from "type by property name" index
This is not a major behavioral change but I did find one file with new missing prop errors PiperOrigin-RevId: 418019197
1 parent 5249124 commit 4f69363

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

src/com/google/javascript/rhino/jstype/JSTypeRegistry.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,14 @@ public final class JSTypeRegistry {
194194
private final transient Set<String> forwardDeclaredTypes;
195195

196196
// A map of properties to the types on which those properties have been declared.
197-
private transient Multimap<String, JSType> typesIndexedByProperty =
197+
// "Reference" types are excluded because those already exist in eachRefTypeIndexedByProperty to
198+
// avoid blowing up the size of this map.
199+
private final transient SetMultimap<String, JSType> nonRefTypesIndexedByProperty =
198200
MultimapBuilder.hashKeys().linkedHashSetValues().build();
199201

200202
private JSType sentinelObjectLiteral;
201203

202-
// To avoid blowing up the size of typesIndexedByProperty, we use the sentinel object
204+
// To avoid blowing up the size of nonRefTypesIndexedByProperty, we use the sentinel object
203205
// literal instead of registering arbitrarily many types.
204206
// But because of the way unions are constructed, some properties of record types in unions
205207
// are getting dropped and cause spurious "non-existent property" warnings.
@@ -209,7 +211,7 @@ public final class JSTypeRegistry {
209211
// canPropertyBeDefined, if the type has a property in propertiesOfSupertypesInUnions, we
210212
// consider it to possibly have any property in droppedPropertiesOfUnions. This is a loose
211213
// check, but we restrict it to records that may be present in unions, and it allows us to
212-
// keep typesIndexedByProperty small.
214+
// keep nonRefTypesIndexedByProperty small.
213215
private final Set<String> propertiesOfSupertypesInUnions = new HashSet<>();
214216
private final Set<String> droppedPropertiesOfUnions = new HashSet<>();
215217

@@ -1080,31 +1082,22 @@ void registerDroppedPropertiesInUnion(RecordType subtype, RecordType supertype)
10801082
* show up in the type registry").
10811083
*/
10821084
public void registerPropertyOnType(String propertyName, JSType type) {
1083-
if (isObjectLiteralThatCanBeSkipped(type)) {
1084-
type = getSentinelObjectLiteral();
1085-
}
1086-
10871085
if (type.isUnionType()) {
1088-
typesIndexedByProperty.putAll(propertyName, type.toMaybeUnionType().getAlternates());
1089-
} else {
1090-
typesIndexedByProperty.put(propertyName, type);
1086+
for (JSType alternate : type.toMaybeUnionType().getAlternates()) {
1087+
registerPropertyOnType(propertyName, alternate);
1088+
}
1089+
return;
10911090
}
10921091

1093-
addReferenceTypeIndexedByProperty(propertyName, type);
1094-
}
1092+
if (isObjectLiteralThatCanBeSkipped(type)) {
1093+
type = getSentinelObjectLiteral();
1094+
}
10951095

1096-
private void addReferenceTypeIndexedByProperty(
1097-
String propertyName, JSType type) {
10981096
if (type instanceof ObjectType && ((ObjectType) type).hasReferenceName()) {
10991097
ObjectType objType = (ObjectType) type;
11001098
eachRefTypeIndexedByProperty.put(propertyName, objType);
1101-
} else if (type instanceof NamedType) {
1102-
addReferenceTypeIndexedByProperty(
1103-
propertyName, ((NamedType) type).getReferencedType());
1104-
} else if (type.isUnionType()) {
1105-
for (JSType alternate : type.toMaybeUnionType().getAlternates()) {
1106-
addReferenceTypeIndexedByProperty(propertyName, alternate);
1107-
}
1099+
} else {
1100+
nonRefTypesIndexedByProperty.put(propertyName, type);
11081101
}
11091102
}
11101103

@@ -1148,19 +1141,26 @@ public PropDefinitionKind canPropertyBeDefined(JSType type, String propertyName)
11481141
}
11491142
}
11501143

1151-
if (typesIndexedByProperty.containsKey(propertyName)) {
1152-
for (JSType alternative : typesIndexedByProperty.get(propertyName)) {
1153-
JSType greatestSubtype = alternative.getGreatestSubtype(type);
1154-
if (!greatestSubtype.isEmptyType()) {
1155-
// We've found a type with this property. Now we just have to make
1156-
// sure it's not a type used for internal bookkeeping.
1157-
RecordType maybeRecordType = greatestSubtype.toMaybeRecordType();
1158-
if (maybeRecordType != null && maybeRecordType.isSynthetic()) {
1159-
continue;
1160-
}
1144+
Iterable<JSType> associatedTypes = ImmutableList.of();
1145+
if (nonRefTypesIndexedByProperty.containsKey(propertyName)) {
1146+
associatedTypes = nonRefTypesIndexedByProperty.get(propertyName);
1147+
}
1148+
if (eachRefTypeIndexedByProperty.containsKey(propertyName)) {
1149+
associatedTypes =
1150+
Iterables.concat(associatedTypes, eachRefTypeIndexedByProperty.get(propertyName));
1151+
}
11611152

1162-
return PropDefinitionKind.LOOSE;
1153+
for (JSType alternative : associatedTypes) {
1154+
JSType greatestSubtype = alternative.getGreatestSubtype(type);
1155+
if (!greatestSubtype.isEmptyType()) {
1156+
// We've found a type with this property. Now we just have to make
1157+
// sure it's not a type used for internal bookkeeping.
1158+
RecordType maybeRecordType = greatestSubtype.toMaybeRecordType();
1159+
if (maybeRecordType != null && maybeRecordType.isSynthetic()) {
1160+
continue;
11631161
}
1162+
1163+
return PropDefinitionKind.LOOSE;
11641164
}
11651165
}
11661166

0 commit comments

Comments
 (0)