Search in sources :

Example 86 with JSType

use of com.google.javascript.jscomp.newtypes.JSType in project closure-compiler by google.

the class NewTypeInference method analyzeAssertionCall.

private EnvTypePair analyzeAssertionCall(Node callNode, TypeEnv env, AssertionFunctionSpec assertionFunctionSpec) {
    analyzeExprFwdIgnoreResult(callNode.getFirstChild(), env);
    Node firstParam = callNode.getSecondChild();
    if (firstParam == null) {
        return new EnvTypePair(env, UNKNOWN);
    }
    for (Node assertionArgument : firstParam.siblings()) {
        analyzeExprFwdIgnoreResult(assertionArgument, env);
    }
    Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
    if (assertedNode == null) {
        return new EnvTypePair(env, UNKNOWN);
    }
    JSType assertedType = assertionFunctionSpec.getAssertedNewType(callNode, currentScope);
    if (assertedType.isUnknown()) {
        warnings.add(JSError.make(callNode, UNKNOWN_ASSERTION_TYPE));
    }
    EnvTypePair pair = analyzeExprFwd(assertedNode, env, UNKNOWN, assertedType);
    boolean haveCommonSubtype = JSType.haveCommonSubtype(assertedType, pair.type);
    if (!pair.type.isSubtypeOf(assertedType) && haveCommonSubtype) {
        // We do this because the assertion needs to return a subtype of the
        // asserted type to its context, but sometimes the asserted expression
        // can't be specialized.
        pair.type = assertedType;
    }
    if (!haveCommonSubtype) {
        JSType t = analyzeExprFwd(assertedNode, env).type.substituteGenericsWithUnknown();
        if (t.isSubtypeOf(assertedType)) {
            pair.type = t;
        } else {
            if (!firstParam.isFalse()) {
                // Don't warn for an explicit: assert(false);
                warnings.add(JSError.make(assertedNode, ASSERT_FALSE));
            }
            pair.type = UNKNOWN;
            pair.env = env;
        }
    }
    return pair;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)

Aggregations

JSType (com.google.javascript.jscomp.newtypes.JSType)86 Node (com.google.javascript.rhino.Node)60 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)54 TypeEnv (com.google.javascript.jscomp.newtypes.TypeEnv)28 DeclaredFunctionType (com.google.javascript.jscomp.newtypes.DeclaredFunctionType)20 QualifiedName (com.google.javascript.jscomp.newtypes.QualifiedName)18 FunctionType (com.google.javascript.jscomp.newtypes.FunctionType)16 Declaration (com.google.javascript.jscomp.newtypes.Declaration)5 FunctionTypeBuilder (com.google.javascript.jscomp.newtypes.FunctionTypeBuilder)5 FunctionNamespace (com.google.javascript.jscomp.newtypes.FunctionNamespace)4 Namespace (com.google.javascript.jscomp.newtypes.Namespace)4 ArrayList (java.util.ArrayList)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 TypeI (com.google.javascript.rhino.TypeI)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 NamespaceLit (com.google.javascript.jscomp.newtypes.NamespaceLit)2 NominalType (com.google.javascript.jscomp.newtypes.NominalType)2 RawNominalType (com.google.javascript.jscomp.newtypes.RawNominalType)2 JSDocInfo (com.google.javascript.rhino.JSDocInfo)2