Search in sources :

Example 6 with QualifiedName

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

the class NewTypeInference method analyzePropLValBwd.

private LValueResultBwd analyzePropLValBwd(Node obj, QualifiedName pname, TypeEnv outEnv, JSType type, boolean doSlicing) {
    checkArgument(pname.isIdentifier());
    JSType reqObjType = pickReqObjType(obj.getParent()).withProperty(pname, type);
    LValueResultBwd lvalue = analyzeLValueBwd(obj, outEnv, reqObjType, false, true);
    if (lvalue.ptr != null) {
        lvalue.ptr = QualifiedName.join(lvalue.ptr, pname);
        if (doSlicing) {
            String objName = lvalue.ptr.getLeftmostName();
            QualifiedName props = lvalue.ptr.getAllButLeftmost();
            JSType objType = envGetType(lvalue.env, objName);
            // withoutProperty only removes inferred properties
            JSType slicedObjType = objType.withoutProperty(props);
            lvalue.env = envPutType(lvalue.env, objName, slicedObjType);
        }
    }
    lvalue.type = lvalue.type.mayHaveProp(pname) ? lvalue.type.getProp(pname) : UNKNOWN;
    return lvalue;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName)

Example 7 with QualifiedName

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

the class NewTypeInference method analyzeEnumObjLitFwd.

private EnvTypePair analyzeEnumObjLitFwd(Node objLit, TypeEnv inEnv, JSType requiredType) {
    // so we ignore them here.
    if (objLit.getFirstChild() == null) {
        return new EnvTypePair(inEnv, requiredType);
    }
    String pname = NodeUtil.getObjectLitKeyName(objLit.getFirstChild());
    JSType enumeratedType = requiredType.getProp(new QualifiedName(pname)).getEnumeratedTypeOfEnumElement();
    if (enumeratedType == null) {
        // enumeratedType is null only if there is some other type error
        return new EnvTypePair(inEnv, requiredType);
    }
    TypeEnv env = inEnv;
    for (Node prop : objLit.children()) {
        EnvTypePair pair = analyzeExprFwd(prop, env, enumeratedType);
        if (!pair.type.isSubtypeOf(enumeratedType)) {
            warnings.add(JSError.make(prop, INVALID_OBJLIT_PROPERTY_TYPE, errorMsgWithTypeDiff(enumeratedType, pair.type)));
        }
        env = pair.env;
    }
    return new EnvTypePair(env, requiredType);
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) TypeEnv(com.google.javascript.jscomp.newtypes.TypeEnv)

Example 8 with QualifiedName

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

the class NewTypeInference method analyzePropLValFwd.

private LValueResultFwd analyzePropLValFwd(Node obj, QualifiedName pname, LValueResultFwd recvLvalue, JSType requiredType, boolean insideQualifiedName) {
    checkArgument(pname.isIdentifier());
    TypeEnv inEnv = recvLvalue.env;
    JSType recvType = recvLvalue.type;
    if (!recvType.isUnion() && !recvType.isSingletonObj()) {
        // The lvalue is a subtype of TOP_OBJECT, but does not contain an object
        // yet, eg, it is ?, truthy, or bottom.
        recvType = TOP_OBJECT.withLoose();
    }
    Node propAccessNode = obj.getParent();
    if (propAccessNode.isGetProp() && propAccessNode.getParent().isAssign() && mayWarnAboutPropCreation(pname, propAccessNode, recvType)) {
        return new LValueResultFwd(inEnv, requiredType, null, null);
    }
    if (!insideQualifiedName && mayWarnAboutConstProp(propAccessNode, recvType, pname)) {
        return new LValueResultFwd(inEnv, requiredType, null, null);
    }
    if (!recvType.hasProp(pname)) {
        // name, or for assignment ops that won't create a new property.
        if (insideQualifiedName || !propAccessNode.getParent().isAssign()) {
            mayWarnAboutInexistentProp(propAccessNode, recvType, pname);
            if (!recvType.isLoose()) {
                return new LValueResultFwd(inEnv, requiredType, null, null);
            }
        }
        if (recvType.isLoose()) {
            // For loose objects, create the inner property if it doesn't exist.
            recvType = recvType.withProperty(pname, UNKNOWN);
            inEnv = updateLvalueTypeInEnv(inEnv, obj, recvLvalue.ptr, recvType);
        }
    }
    if (propAccessNode.isGetElem()) {
        mayWarnAboutStructPropAccess(obj, recvType);
    } else if (propAccessNode.isGetProp()) {
        mayWarnAboutDictPropAccess(obj, recvType);
    }
    QualifiedName setterPname = new QualifiedName(commonTypes.createSetterPropName(pname.getLeftmostName()));
    if (recvType.hasProp(setterPname)) {
        FunctionType funType = recvType.getProp(setterPname).getFunType();
        checkNotNull(funType, "recvType=%s, setterPname=%s", recvType, setterPname);
        JSType formalType = funType.getFormalType(0);
        checkState(!formalType.isBottom());
        return new LValueResultFwd(inEnv, formalType, formalType, null);
    }
    QualifiedName ptr = recvLvalue.ptr == null ? null : QualifiedName.join(recvLvalue.ptr, pname);
    return recvType.mayHaveProp(pname) ? new LValueResultFwd(inEnv, recvType.getProp(pname), recvType.getDeclaredProp(pname), ptr) : new LValueResultFwd(inEnv, UNKNOWN, null, ptr);
}
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) TypeEnv(com.google.javascript.jscomp.newtypes.TypeEnv)

Example 9 with QualifiedName

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

the class NewTypeInference method analyzeIncDecFwd.

private EnvTypePair analyzeIncDecFwd(Node expr, TypeEnv inEnv, JSType requiredType) {
    mayWarnAboutConst(expr);
    Node ch = expr.getFirstChild();
    if (ch.isGetProp() || (ch.isGetElem() && ch.getLastChild().isString())) {
        // We prefer to analyze the child of INC/DEC one extra time here,
        // to putting the @const prop check in analyzePropAccessFwd.
        Node recv = ch.getFirstChild();
        String pname = ch.getLastChild().getString();
        EnvTypePair pair = analyzeExprFwd(recv, inEnv);
        JSType recvType = pair.type;
        if (mayWarnAboutConstProp(ch, recvType, new QualifiedName(pname))) {
            maybeSetTypeI(ch, recvType.getProp(new QualifiedName(pname)));
            pair.type = requiredType;
            return pair;
        }
    }
    return analyzeUnaryNumFwd(expr, inEnv);
}
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 10 with QualifiedName

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

the class NewTypeInference method isAllowedToNotReturn.

private static boolean isAllowedToNotReturn(NTIScope methodScope) {
    Node fn = methodScope.getRoot();
    if (fn.isFromExterns()) {
        return true;
    }
    DeclaredFunctionType declFn = methodScope.getDeclaredFunctionType();
    if (declFn != null && declFn.isAbstract() && declFn.getReceiverType() != null) {
        return true;
    }
    if (!NodeUtil.isPrototypeMethod(fn)) {
        return false;
    }
    JSType maybeInterface;
    Node ntQnameNode = NodeUtil.getPrototypeClassName(fn.getParent().getFirstChild());
    if (ntQnameNode.isName()) {
        maybeInterface = methodScope.getDeclaredTypeOf(ntQnameNode.getString());
    } else {
        QualifiedName ntQname = QualifiedName.fromNode(ntQnameNode);
        JSType rootNamespace = methodScope.getDeclaredTypeOf(ntQname.getLeftmostName());
        maybeInterface = rootNamespace == null ? null : rootNamespace.getProp(ntQname.getAllButLeftmost());
    }
    return maybeInterface != null && maybeInterface.isInterfaceDefinition();
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName)

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