Search in sources :

Example 16 with QualifiedName

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

the class NTIScope method getNamespaceAfterFreezing.

private Namespace getNamespaceAfterFreezing(String typeName) {
    checkNotNull(preservedNamespaces, "Failed to preserve namespaces post-finalization");
    QualifiedName qname = QualifiedName.fromQualifiedString(typeName);
    Namespace ns = preservedNamespaces.get(qname.getLeftmostName());
    if (ns != null && !qname.isIdentifier()) {
        ns = ns.getSubnamespace(qname.getAllButLeftmost());
    }
    return ns;
}
Also used : QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) Namespace(com.google.javascript.jscomp.newtypes.Namespace) FunctionNamespace(com.google.javascript.jscomp.newtypes.FunctionNamespace)

Example 17 with QualifiedName

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

the class NTIScope method addTypedef.

void addTypedef(Node qnameNode, Typedef td) {
    if (qnameNode.isName()) {
        checkState(!localTypedefs.containsKey(qnameNode.getString()));
        localTypedefs.put(qnameNode.getString(), td);
    } else {
        checkState(!isDefined(qnameNode));
        QualifiedName qname = QualifiedName.fromNode(qnameNode);
        Namespace ns = getNamespace(qname.getLeftmostName());
        ns.addTypedef(qname.getAllButLeftmost(), td);
        namespaceTypedefs.add(td);
    }
}
Also used : QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) Namespace(com.google.javascript.jscomp.newtypes.Namespace) FunctionNamespace(com.google.javascript.jscomp.newtypes.FunctionNamespace)

Example 18 with QualifiedName

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

the class NTIScope method getScope.

NTIScope getScope(String fnName) {
    NTIScope s = getScopeHelper(new QualifiedName(fnName));
    checkState(s != null);
    return s;
}
Also used : QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName)

Example 19 with QualifiedName

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

the class SimpleInference method inferPrototypeProperty.

private JSType inferPrototypeProperty(Node recv, String pname, NTIScope scope) {
    QualifiedName recvQname = QualifiedName.fromNode(recv);
    Declaration decl = scope.getDeclaration(recvQname, false);
    if (decl != null) {
        Namespace ns = decl.getNamespace();
        if (ns instanceof RawNominalType) {
            return ((RawNominalType) ns).getProtoPropDeclaredType(pname);
        }
    }
    return null;
}
Also used : QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) RawNominalType(com.google.javascript.jscomp.newtypes.RawNominalType) Declaration(com.google.javascript.jscomp.newtypes.Declaration) FunctionNamespace(com.google.javascript.jscomp.newtypes.FunctionNamespace) Namespace(com.google.javascript.jscomp.newtypes.Namespace)

Example 20 with QualifiedName

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

the class NewTypeInference method analyzeObjLitFwd.

private EnvTypePair analyzeObjLitFwd(Node objLit, TypeEnv inEnv, JSType requiredType, JSType specializedType) {
    if (NodeUtil.isEnumDecl(objLit.getParent())) {
        return analyzeEnumObjLitFwd(objLit, inEnv, requiredType);
    }
    JSDocInfo jsdoc = objLit.getJSDocInfo();
    boolean isStruct = jsdoc != null && jsdoc.makesStructs();
    boolean isDict = jsdoc != null && jsdoc.makesDicts();
    TypeEnv env = inEnv;
    JSType result = pickReqObjType(objLit);
    for (Node prop : objLit.children()) {
        if (isStruct && prop.isQuotedString()) {
            warnings.add(JSError.make(prop, ILLEGAL_OBJLIT_KEY, "struct"));
        } else if (isDict && !prop.isQuotedString()) {
            warnings.add(JSError.make(prop, ILLEGAL_OBJLIT_KEY, "dict"));
        }
        // an accidental clash.
        if (prop.isGetterDef() || prop.isSetterDef()) {
            String pname = NodeUtil.getObjectLitKeyName(prop);
            EnvTypePair pair = analyzeExprFwd(prop.getFirstChild(), env);
            FunctionType funType = pair.type.getFunType();
            checkNotNull(funType);
            String specialPropName;
            JSType propType;
            if (prop.isGetterDef()) {
                specialPropName = commonTypes.createGetterPropName(pname);
                propType = funType.getReturnType();
            } else {
                specialPropName = commonTypes.createSetterPropName(pname);
                propType = pair.type;
            }
            result = result.withProperty(new QualifiedName(specialPropName), propType);
            env = pair.env;
        } else {
            Node pnameNode = NodeUtil.getObjectLitKeyNode(prop);
            if (pnameNode == null) {
                // pnameNode is null when prop is a computed prop does not have a String node key.
                // Just type-check the prop, then move on to the next property.
                env = analyzeExprFwd(prop, env).env;
                continue;
            }
            QualifiedName qname = new QualifiedName(pnameNode.getString());
            JSType jsdocType = (JSType) prop.getTypeI();
            JSType reqPtype;
            JSType specPtype;
            if (jsdocType != null) {
                reqPtype = specPtype = jsdocType;
            } else if (requiredType.mayHaveProp(qname)) {
                reqPtype = specPtype = requiredType.getProp(qname);
                if (specializedType.mayHaveProp(qname)) {
                    specPtype = specializedType.getProp(qname);
                }
            } else {
                reqPtype = specPtype = UNKNOWN;
            }
            EnvTypePair pair = analyzeExprFwd(prop, env, reqPtype, specPtype);
            if (jsdocType != null) {
                // First declare it; then set the maybe more precise inferred type
                result = result.withDeclaredProperty(qname, jsdocType, false);
                if (!pair.type.isSubtypeOf(jsdocType)) {
                    warnings.add(JSError.make(prop, INVALID_OBJLIT_PROPERTY_TYPE, errorMsgWithTypeDiff(jsdocType, pair.type)));
                    pair.type = jsdocType;
                }
            }
            result = result.withProperty(qname, pair.type);
            env = pair.env;
        }
    }
    result = mayAdjustObjLitType(objLit, jsdoc, inEnv, result);
    return new EnvTypePair(env, result);
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) FunctionType(com.google.javascript.jscomp.newtypes.FunctionType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) JSDocInfo(com.google.javascript.rhino.JSDocInfo) 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