Search in sources :

Example 21 with QualifiedName

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

the class NewTypeInference method analyzePropAccessBwd.

private EnvTypePair analyzePropAccessBwd(Node receiver, String pname, TypeEnv outEnv, JSType requiredType) {
    Node propAccessNode = receiver.getParent();
    QualifiedName qname = new QualifiedName(pname);
    JSType reqObjType = pickReqObjType(propAccessNode);
    if (!NodeUtil.isPropertyTest(compiler, propAccessNode)) {
        reqObjType = reqObjType.withProperty(qname, requiredType);
    }
    EnvTypePair pair = analyzeExprBwd(receiver, outEnv, reqObjType);
    JSType receiverType = pair.type;
    JSType propAccessType = receiverType.mayHaveProp(qname) ? receiverType.getProp(qname) : requiredType;
    pair.type = propAccessType;
    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) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName)

Example 22 with QualifiedName

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

the class NewTypeInference method mayAdjustObjLitType.

/**
 * If the object literal is lended, or assigned to a prototype, find a better
 * type for it than the object-literal type.
 */
private JSType mayAdjustObjLitType(Node objLit, JSDocInfo jsdoc, TypeEnv env, JSType originalType) {
    Node parent = objLit.getParent();
    QualifiedName classqname = null;
    if (parent.isAssign() && NodeUtil.isPrototypeAssignment(parent.getFirstChild())) {
        classqname = QualifiedName.fromNode(parent.getFirstFirstChild());
    } else if (jsdoc != null && jsdoc.getLendsName() != null) {
        QualifiedName lendsQname = QualifiedName.fromQualifiedString(jsdoc.getLendsName());
        if (lendsQname.getRightmostName().equals("prototype")) {
            classqname = lendsQname.getAllButRightmost();
        }
    } else if (parent.isCall() && this.convention.getObjectLiteralCast(parent) != null) {
        ObjectLiteralCast cast = this.convention.getObjectLiteralCast(parent);
        if (cast.typeName != null) {
            classqname = QualifiedName.fromQualifiedString(cast.typeName);
        }
    }
    if (classqname != null) {
        JSType classType = envGetTypeOfQname(env, classqname);
        if (classType != null) {
            FunctionType clazz = classType.getFunTypeIfSingletonObj();
            JSType instance = clazz == null ? null : clazz.getInstanceTypeOfCtor();
            if (instance != null) {
                return instance.getPrototypeObject();
            }
        }
    }
    return originalType;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) FunctionType(com.google.javascript.jscomp.newtypes.FunctionType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) ObjectLiteralCast(com.google.javascript.jscomp.CodingConvention.ObjectLiteralCast)

Example 23 with QualifiedName

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

the class NewTypeInference method mayWarnAboutNullableReferenceAndTighten.

private EnvTypePair mayWarnAboutNullableReferenceAndTighten(Node obj, JSType recvType, JSType maybeSpecType, TypeEnv inEnv) {
    if (!recvType.isUnknown() && !recvType.isTop() && (NULL.isSubtypeOf(recvType) || UNDEFINED.isSubtypeOf(recvType))) {
        JSType minusNull = recvType.removeType(NULL_OR_UNDEFINED);
        if (!minusNull.isBottom()) {
            if (this.reportNullDeref) {
                warnings.add(JSError.make(obj, NULLABLE_DEREFERENCE, recvType.toString()));
            }
            TypeEnv outEnv = inEnv;
            if (obj.isQualifiedName()) {
                QualifiedName qname = QualifiedName.fromNode(obj);
                if (maybeSpecType != null && maybeSpecType.isSubtypeOf(minusNull)) {
                    minusNull = maybeSpecType;
                }
                outEnv = updateLvalueTypeInEnv(inEnv, obj, qname, minusNull);
            }
            return new EnvTypePair(outEnv, minusNull);
        }
    }
    return new EnvTypePair(inEnv, recvType);
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) TypeEnv(com.google.javascript.jscomp.newtypes.TypeEnv)

Aggregations

QualifiedName (com.google.javascript.jscomp.newtypes.QualifiedName)23 JSType (com.google.javascript.jscomp.newtypes.JSType)18 Node (com.google.javascript.rhino.Node)15 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)14 TypeEnv (com.google.javascript.jscomp.newtypes.TypeEnv)7 DeclaredFunctionType (com.google.javascript.jscomp.newtypes.DeclaredFunctionType)6 FunctionNamespace (com.google.javascript.jscomp.newtypes.FunctionNamespace)5 FunctionType (com.google.javascript.jscomp.newtypes.FunctionType)5 Namespace (com.google.javascript.jscomp.newtypes.Namespace)5 Declaration (com.google.javascript.jscomp.newtypes.Declaration)3 ImmutableList (com.google.common.collect.ImmutableList)1 ObjectLiteralCast (com.google.javascript.jscomp.CodingConvention.ObjectLiteralCast)1 EnumType (com.google.javascript.jscomp.newtypes.EnumType)1 RawNominalType (com.google.javascript.jscomp.newtypes.RawNominalType)1 JSDocInfo (com.google.javascript.rhino.JSDocInfo)1