Java Code Examples for com.google.javascript.rhino.jstype.JSType#findPropertyType()

The following examples show how to use com.google.javascript.rhino.jstype.JSType#findPropertyType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Closure_25_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 2
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 3
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 4
Source File: Closure_35_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      if (varType.equals(getNativeType(UNKNOWN_TYPE)) &&
          var != syntacticScope.getSlot(qualifiedName)) {
        // If the type of this qualified name has been checked in this scope,
        // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
        return getNativeType(CHECKED_UNKNOWN_TYPE);
      } else {
        return varType;
      }
    }
  }

  JSType propertyType = null;
  if (objType != null) {
    propertyType = objType.findPropertyType(propName);
  }

  if ((propertyType == null || propertyType.isUnknownType()) &&
      qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  return propertyType;
}
 
Example 5
Source File: Closure_112_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // We often have a couple of different types to choose from for the
  // property. Ordered by accuracy, we have
  // 1) A locally inferred qualified name (which is in the FlowScope)
  // 2) A globally declared qualified name (which is in the FlowScope)
  // 3) A property on the owner type (which is on objType)
  // 4) A name in the type registry (as a last resort)
  JSType propertyType = null;
  boolean isLocallyInferred = false;

  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      boolean isDeclared = !var.isTypeInferred();
      isLocallyInferred = (var != syntacticScope.getSlot(qualifiedName));
      if (isDeclared || isLocallyInferred) {
        propertyType = varType;
      }
    }
  }

  if (propertyType == null && objType != null) {
    JSType foundType = objType.findPropertyType(propName);
    if (foundType != null) {
      propertyType = foundType;
    }
  }

  if (propertyType != null && objType != null) {
    JSType restrictedObjType = objType.restrictByNotNullOrUndefined();
    if (!restrictedObjType.getTemplateTypeMap().isEmpty()
        && propertyType.hasAnyTemplateTypes()) {
      TemplateTypeMap typeMap = restrictedObjType.getTemplateTypeMap();
      TemplateTypeMapReplacer replacer = new TemplateTypeMapReplacer(
          registry, typeMap);
      propertyType = propertyType.visit(replacer);
    }
  }

  if ((propertyType == null || propertyType.isUnknownType())
      && qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  if (propertyType == null) {
    return unknownType;
  } else if (propertyType.isEquivalentTo(unknownType) && isLocallyInferred) {
    // If the type has been checked in this scope,
    // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
    return getNativeType(CHECKED_UNKNOWN_TYPE);
  } else {
    return propertyType;
  }
}
 
Example 6
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // We often have a couple of different types to choose from for the
  // property. Ordered by accuracy, we have
  // 1) A locally inferred qualified name (which is in the FlowScope)
  // 2) A globally declared qualified name (which is in the FlowScope)
  // 3) A property on the owner type (which is on objType)
  // 4) A name in the type registry (as a last resort)
  JSType propertyType = null;
  boolean isLocallyInferred = false;

  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      boolean isDeclared = !var.isTypeInferred();
      isLocallyInferred = (var != syntacticScope.getSlot(qualifiedName));
      if (isDeclared || isLocallyInferred) {
        propertyType = varType;
      }
    }
  }

  if (propertyType == null && objType != null) {
    JSType foundType = objType.findPropertyType(propName);
    if (foundType != null) {
      propertyType = foundType;
    }
  }

  if (propertyType != null && objType != null) {
    JSType restrictedObjType = objType.restrictByNotNullOrUndefined();
    if (!restrictedObjType.getTemplateTypeMap().isEmpty()
        && propertyType.hasAnyTemplateTypes()) {
      TemplateTypeMap typeMap = restrictedObjType.getTemplateTypeMap();
      TemplateTypeMapReplacer replacer = new TemplateTypeMapReplacer(
          registry, typeMap);
      propertyType = propertyType.visit(replacer);
    }
  }

  if ((propertyType == null || propertyType.isUnknownType())
      && qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  if (propertyType == null) {
    return unknownType;
  } else if (propertyType.isEquivalentTo(unknownType) && isLocallyInferred) {
    // If the type has been checked in this scope,
    // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
    return getNativeType(CHECKED_UNKNOWN_TYPE);
  } else {
    return propertyType;
  }
}
 
Example 7
Source File: TypeInference.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private JSType getPropertyType(JSType objType, String propName,
    Node n, FlowScope scope) {
  // We often have a couple of different types to choose from for the
  // property. Ordered by accuracy, we have
  // 1) A locally inferred qualified name (which is in the FlowScope)
  // 2) A globally declared qualified name (which is in the FlowScope)
  // 3) A property on the owner type (which is on objType)
  // 4) A name in the type registry (as a last resort)
  JSType propertyType = null;
  boolean isLocallyInferred = false;

  // Scopes sometimes contain inferred type info about qualified names.
  String qualifiedName = n.getQualifiedName();
  StaticSlot<JSType> var = scope.getSlot(qualifiedName);
  if (var != null) {
    JSType varType = var.getType();
    if (varType != null) {
      boolean isDeclared = !var.isTypeInferred();
      isLocallyInferred = (var != syntacticScope.getSlot(qualifiedName));
      if (isDeclared || isLocallyInferred) {
        propertyType = varType;
      }
    }
  }

  if (propertyType == null && objType != null) {
    JSType foundType = objType.findPropertyType(propName);
    if (foundType != null) {
      propertyType = foundType;
    }
  }

  if ((propertyType == null || propertyType.isUnknownType())
      && qualifiedName != null) {
    // If we find this node in the registry, then we can infer its type.
    ObjectType regType = ObjectType.cast(registry.getType(qualifiedName));
    if (regType != null) {
      propertyType = regType.getConstructor();
    }
  }

  if (propertyType == null) {
    return unknownType;
  } else if (propertyType.isEquivalentTo(unknownType) && isLocallyInferred) {
    // If the type has been checked in this scope,
    // then use CHECKED_UNKNOWN_TYPE instead to indicate that.
    return getNativeType(CHECKED_UNKNOWN_TYPE);
  } else {
    return propertyType;
  }
}
 
Example 8
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private void processSymbol(Symbol symbol) {
  final JSType globalThis =
      compiler.getTypeRegistry().getNativeObjectType(JSTypeNative.GLOBAL_THIS);

  if (typeRegistry.isType(symbol.getName())) {
    return;
  }

  JSType type = null;
  boolean isFromCompilerRegistry = false;

  if (symbol.getReferencedSymbol() != null
      && typeRegistry.isType(symbol.getReferencedSymbol())) {
    type = typeRegistry.getType(symbol.getReferencedSymbol()).getType();
  }

  if (type == null) {
    type = compiler.getTypeRegistry().getGlobalType(symbol.getName());
    isFromCompilerRegistry = type != null;
  }

  if (type == null) {
    type = globalThis;
    for (String part : Splitter.on('.').split(symbol.getName())) {
      type = type.findPropertyType(part);
      if (type == null) {
        return;
      }
    }
  }

  if (type != null) {
    if (type.isInstanceType()
        && (isFromCompilerRegistry
            || shouldConvertToConstructor(type)
            || shouldConvertToConstructor(symbol))) {
      type = type.toObjectType().getConstructor();
    } else if (type.isEnumElementType()) {
      type = type.toMaybeEnumElementType().getEnumType();
    }
  }

  if (type == null) {
    log.warning("skipping " + symbol.getName() + "; no type");
    return;
  } else if (globalThis.equals(type)) {
    log.warning("skipping " + symbol.getName() + "; references global this");
    return;
  } else if (type.isOrdinaryFunction() && !typeRegistry.isProvided(symbol.getName())) {
    log.info("skipping " + symbol.getName() + "; is ordinary function");
    return;
  }
  collectTypes(symbol.getName(), type, symbol.getNode(), symbol.getJSDocInfo());
}