Search in sources :

Example 1 with EnumType

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

the class NTIScope method addNamespace.

void addNamespace(QualifiedName qname, Node defSite, Namespace ns) {
    if (ns instanceof EnumType) {
        this.localEnums.add((EnumType) ns);
    }
    if (qname.isIdentifier()) {
        String varName = qname.getLeftmostName();
        Preconditions.checkState(!this.localNamespaces.containsKey(varName), "Namespace %s already defined.", varName);
        this.localNamespaces.put(varName, ns);
        if (defSite.isFromExterns() && !this.externs.containsKey(varName)) {
            // We don't know the full type of a namespace until after we see all
            // its properties. But we want to add it to the externs, otherwise it
            // is treated as a local and initialized to the wrong thing in NTI.
            this.externs.put(varName, null);
        }
    } else {
        checkState(!isDefined(qname));
        Namespace rootns = getNamespace(qname.getLeftmostName());
        rootns.addNamespace(qname.getAllButLeftmost(), ns);
    }
}
Also used : EnumType(com.google.javascript.jscomp.newtypes.EnumType) Namespace(com.google.javascript.jscomp.newtypes.Namespace) FunctionNamespace(com.google.javascript.jscomp.newtypes.FunctionNamespace)

Example 2 with EnumType

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

the class SimpleInference method inferPropAccess.

private JSType inferPropAccess(Node recv, String pname, NTIScope scope) {
    if (recv.isGetProp() && recv.getLastChild().getString().equals("prototype")) {
        return inferPrototypeProperty(recv.getFirstChild(), pname, scope);
    }
    QualifiedName propQname = new QualifiedName(pname);
    JSType recvType = null;
    if (recv.isQualifiedName()) {
        QualifiedName recvQname = QualifiedName.fromNode(recv);
        Declaration decl = scope.getDeclaration(recvQname, false);
        if (decl != null) {
            EnumType et = decl.getEnum();
            if (et != null && et.enumLiteralHasKey(pname)) {
                return et.getPropType();
            }
            Namespace ns = decl.getNamespace();
            if (ns != null) {
                return inferDeclaration(ns.getDeclaration(propQname));
            }
            recvType = decl.getTypeOfSimpleDecl();
        }
    }
    if (recvType == null) {
        recvType = inferExprRecur(recv, scope);
    }
    if (recvType == null) {
        return null;
    }
    if (recvType.isScalar()) {
        recvType = recvType.autobox();
    }
    FunctionType ft = recvType.getFunTypeIfSingletonObj();
    if (ft != null && pname.equals("call")) {
        return this.commonTypes.fromFunctionType(ft.transformByCallProperty());
    } else if (ft != null && pname.equals("apply")) {
        return this.commonTypes.fromFunctionType(ft.transformByApplyProperty());
    }
    if (recvType.mayHaveProp(propQname)) {
        return recvType.getProp(propQname);
    }
    return null;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) EnumType(com.google.javascript.jscomp.newtypes.EnumType) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) FunctionType(com.google.javascript.jscomp.newtypes.FunctionType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) Declaration(com.google.javascript.jscomp.newtypes.Declaration) FunctionNamespace(com.google.javascript.jscomp.newtypes.FunctionNamespace) Namespace(com.google.javascript.jscomp.newtypes.Namespace)

Aggregations

EnumType (com.google.javascript.jscomp.newtypes.EnumType)2 FunctionNamespace (com.google.javascript.jscomp.newtypes.FunctionNamespace)2 Namespace (com.google.javascript.jscomp.newtypes.Namespace)2 Declaration (com.google.javascript.jscomp.newtypes.Declaration)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