Search in sources :

Example 1 with ObjectLiteralCast

use of com.google.javascript.jscomp.CodingConvention.ObjectLiteralCast 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)

Aggregations

ObjectLiteralCast (com.google.javascript.jscomp.CodingConvention.ObjectLiteralCast)1 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)1 DeclaredFunctionType (com.google.javascript.jscomp.newtypes.DeclaredFunctionType)1 FunctionType (com.google.javascript.jscomp.newtypes.FunctionType)1 JSType (com.google.javascript.jscomp.newtypes.JSType)1 QualifiedName (com.google.javascript.jscomp.newtypes.QualifiedName)1 Node (com.google.javascript.rhino.Node)1