third_party/closure-compiler/src/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java
/**
   * Returns whether the given property can possibly be set on the given type.
   */
Returns whether the given property can possibly be set on the given type.
Returns whether the given property can possibly be set on the given type .
public PropDefinitionKind canPropertyBeDefined(JSType type, String propertyName) {
    if (type.isStruct()) {
      switch (type.getPropertyKind(propertyName)) {
        case KNOWN_PRESENT:
          return PropDefinitionKind.KNOWN;
        case MAYBE_PRESENT:
          return PropDefinitionKind.KNOWN;
        case ABSENT:
          return PropDefinitionKind.UNKNOWN;
      }
    } else {
      if (!type.isEmptyType() && !type.isUnknownType()) {
        switch (type.getPropertyKind(propertyName)) {
          case KNOWN_PRESENT:
            return PropDefinitionKind.KNOWN;
          case MAYBE_PRESENT:
            return PropDefinitionKind.KNOWN;
          case ABSENT:
            break;
        }
      }
      if (typesIndexedByProperty.containsKey(propertyName)) {
        for (JSType alternative : typesIndexedByProperty.get(propertyName)) {
          JSType greatestSubtype = alternative.getGreatestSubtype(type);
          if (!greatestSubtype.isEmptyType()) {
            RecordType maybeRecordType = greatestSubtype.toMaybeRecordType();
            if (maybeRecordType != null && maybeRecordType.isSynthetic()) {
              continue;
            }
            return PropDefinitionKind.LOOSE;
          }
        }
      }
      if (type.toMaybeRecordType() != null) {
        RecordType rec = type.toMaybeRecordType();
        boolean mayBeInUnion = false;
        for (String pname : rec.getPropertyMap().getOwnPropertyNames()) {
          if (this.propertiesOfSupertypesInUnions.contains(pname)) {
            mayBeInUnion = true;
            break;
          }
        }
        if (mayBeInUnion && this.droppedPropertiesOfUnions.contains(propertyName)) {
          return PropDefinitionKind.LOOSE;
        }
      }
    }
    return PropDefinitionKind.UNKNOWN;
  }
public PropDefinitionKind canPropertyBeDefined ( JSType type , String propertyName ) { if ( type . isStruct ( ) ) { switch ( type . getPropertyKind ( propertyName ) ) { case KNOWN_PRESENT : return PropDefinitionKind . KNOWN ; case MAYBE_PRESENT : return PropDefinitionKind . KNOWN ; case ABSENT : return PropDefinitionKind . UNKNOWN ; } } else { if ( ! type . isEmptyType ( ) && ! type . isUnknownType ( ) ) { switch ( type . getPropertyKind ( propertyName ) ) { case KNOWN_PRESENT : return PropDefinitionKind . KNOWN ; case MAYBE_PRESENT : return PropDefinitionKind . KNOWN ; case ABSENT : break ; } } if ( typesIndexedByProperty . containsKey ( propertyName ) ) { for ( JSType alternative : typesIndexedByProperty . get ( propertyName ) ) { JSType greatestSubtype = alternative . getGreatestSubtype ( type ) ; if ( ! greatestSubtype . isEmptyType ( ) ) { RecordType maybeRecordType = greatestSubtype . toMaybeRecordType ( ) ; if ( maybeRecordType != null && maybeRecordType . isSynthetic ( ) ) { continue ; } return PropDefinitionKind . LOOSE ; } } } if ( type . toMaybeRecordType ( ) != null ) { RecordType rec = type . toMaybeRecordType ( ) ; boolean mayBeInUnion = false ; for ( String pname : rec . getPropertyMap ( ) . getOwnPropertyNames ( ) ) { if ( this . propertiesOfSupertypesInUnions . contains ( pname ) ) { mayBeInUnion = true ; break ; } } if ( mayBeInUnion && this . droppedPropertiesOfUnions . contains ( propertyName ) ) { return PropDefinitionKind . LOOSE ; } } } return PropDefinitionKind . UNKNOWN ; }
Returns whether the given property can possibly be set on the given type.
Returns whether the given property can possibly be set on the given type .
// We are stricter about "struct" types and only allow access to // properties that to the best of our knowledge are available at creation // time and specifically not properties only defined on subtypes. // TODO(johnlenz): return LOOSE_UNION here. // TODO(johnlenz): return LOOSE_UNION here. // check for loose properties below. // We've found a type with this property. Now we just have to make // sure it's not a type used for internal bookkeeping.