Skip to content

Commit

Permalink
Simplify NamedType resolution logic by removing redundancy.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=211992701
  • Loading branch information
concavelenz authored and lauraharker committed Sep 7, 2018
1 parent 72b720a commit 8c0d22b
Showing 1 changed file with 14 additions and 36 deletions.
50 changes: 14 additions & 36 deletions src/com/google/javascript/rhino/jstype/NamedType.java
Expand Up @@ -239,28 +239,17 @@ JSType resolveInternal(ErrorReporter reporter) {
// makes more sense. Now, resolution via registry is first in order to
// avoid triggering the warnings built into the resolution via properties.
boolean resolved = resolveViaRegistry(reporter);
if (detectInheritanceCycle()) {
handleTypeCycle(reporter);
}

if (resolved) {
super.resolveInternal(reporter);
finishPropertyContinuations();
} else {

if (!resolved) {
resolveViaProperties(reporter);
if (detectInheritanceCycle()) {
handleTypeCycle(reporter);
}
}

super.resolveInternal(reporter);
if (isResolved()) {
finishPropertyContinuations();
}
if (detectInheritanceCycle()) {
handleTypeCycle(reporter);
}
super.resolveInternal(reporter);
finishPropertyContinuations();

JSType result = getReferencedType();

if (isSuccessfullyResolved()) {
int numKeys = result.getTemplateTypeMap().numUnfilledTemplateKeys();
if (result.isObjectType()
Expand Down Expand Up @@ -301,7 +290,7 @@ private boolean resolveViaRegistry(ErrorReporter reporter) {
* as properties. The scope must have been fully parsed and a symbol table constructed.
*/
private void resolveViaProperties(ErrorReporter reporter) {
JSType value = lookupViaProperties(reporter);
JSType value = lookupViaProperties();
// last component of the chain
if (value != null && value.isFunctionType() &&
(value.isConstructor() || value.isInterface())) {
Expand Down Expand Up @@ -331,39 +320,37 @@ private void resolveViaProperties(ErrorReporter reporter) {
* parsed and a symbol table constructed.
* @return The type of the symbol, or null if the type could not be found.
*/
private JSType lookupViaProperties(ErrorReporter reporter) {
private JSType lookupViaProperties() {
String[] componentNames = reference.split("\\.", -1);
if (componentNames[0].length() == 0) {
return null;
}

StaticTypedSlot slot = resolutionScope.getSlot(componentNames[0]);
if (slot == null) {
return null;
}

// If the first component has a type of 'Unknown', then any type
// names using it should be regarded as silently 'Unknown' rather than be
// noisy about it.
JSType slotType = slot.getType();
if (slotType == null || slotType.isAllType() || slotType.isNoType()) {
return null;
}
JSType value = getTypedefType(reporter, slot);
if (value == null) {
return null;
}

// resolving component by component
for (int i = 1; i < componentNames.length; i++) {
ObjectType parentClass = ObjectType.cast(value);
if (parentClass == null) {
ObjectType parentObj = ObjectType.cast(slotType);
if (parentObj == null) {
return null;
}
if (componentNames[i].length() == 0) {
return null;
}
value = parentClass.getPropertyType(componentNames[i]);
slotType = parentObj.getPropertyType(componentNames[i]);
}
return value;
return slotType;
}

private void setReferencedAndResolvedType(
Expand Down Expand Up @@ -418,15 +405,6 @@ private void handleUnresolvedType(
setResolvedTypeInternal(getReferencedType());
}

private JSType getTypedefType(ErrorReporter reporter, StaticTypedSlot slot) {
JSType type = slot.getType();
if (type != null) {
return type;
}
handleUnresolvedType(reporter, true);
return null;
}

@Override
public boolean setValidator(Predicate<JSType> validator) {
// If the type is already resolved, we can validate it now. If
Expand Down

0 comments on commit 8c0d22b

Please sign in to comment.