Search in sources :

Example 6 with DeclaredFunctionType

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

the class NewTypeInference method getTypeEnvFromDeclaredTypes.

private TypeEnv getTypeEnvFromDeclaredTypes() {
    TypeEnv env = new TypeEnv();
    Set<String> varNames = this.currentScope.getOuterVars();
    Set<String> locals = this.currentScope.getLocals();
    varNames.addAll(locals);
    varNames.addAll(this.currentScope.getExterns());
    if (this.currentScope.hasThis()) {
        varNames.add(THIS_ID);
    }
    if (this.currentScope.isFunction()) {
        Node fn = this.currentScope.getRoot();
        if (!this.currentScope.hasThis() && // a function.
        NodeUtil.containsType(fn.getLastChild(), Token.SUPER, NodeUtil.MATCH_NOT_FUNCTION)) {
            // This function is a static method on some class. To do lookups of the
            // class name, we add the root of the qualified name to the environment.
            Node funNameNode = NodeUtil.getBestLValue(fn);
            Node qnameRoot = NodeUtil.getRootOfQualifiedName(funNameNode);
            checkState(qnameRoot.isName());
            varNames.add(qnameRoot.getString());
        }
        if (this.currentScope.getName() != null) {
            varNames.add(this.currentScope.getName());
        }
        varNames.addAll(this.currentScope.getFormals());
        // In the rare case when there is a local variable named "arguments",
        // this entry will be overwritten in the foreach loop below.
        JSType argumentsType;
        DeclaredFunctionType dft = this.currentScope.getDeclaredTypeForOwnBody();
        if (dft.getOptionalArity() == 0 && dft.hasRestFormals()) {
            argumentsType = dft.getRestFormalsType();
        } else {
            argumentsType = UNKNOWN;
        }
        env = envPutType(env, "arguments", commonTypes.getArgumentsArrayType(argumentsType));
    }
    for (String varName : varNames) {
        if (!this.currentScope.isLocalFunDef(varName)) {
            JSType declType = this.currentScope.getDeclaredTypeOf(varName);
            if (declType == null) {
                declType = UNKNOWN;
            } else if (areTypeVariablesUnknown) {
                declType = declType.substituteGenericsWithUnknown();
            }
            env = envPutType(env, varName, declType);
        }
    }
    for (String fnName : this.currentScope.getLocalFunDefs()) {
        env = envPutType(env, fnName, getSummaryOfLocalFunDef(fnName));
    }
    return env;
}
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) TypeEnv(com.google.javascript.jscomp.newtypes.TypeEnv)

Aggregations

DeclaredFunctionType (com.google.javascript.jscomp.newtypes.DeclaredFunctionType)6 JSType (com.google.javascript.jscomp.newtypes.JSType)6 Node (com.google.javascript.rhino.Node)5 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)4 FunctionType (com.google.javascript.jscomp.newtypes.FunctionType)2 TypeEnv (com.google.javascript.jscomp.newtypes.TypeEnv)2 Declaration (com.google.javascript.jscomp.newtypes.Declaration)1 FunctionNamespace (com.google.javascript.jscomp.newtypes.FunctionNamespace)1 FunctionTypeBuilder (com.google.javascript.jscomp.newtypes.FunctionTypeBuilder)1 Namespace (com.google.javascript.jscomp.newtypes.Namespace)1 NominalType (com.google.javascript.jscomp.newtypes.NominalType)1 QualifiedName (com.google.javascript.jscomp.newtypes.QualifiedName)1 RawNominalType (com.google.javascript.jscomp.newtypes.RawNominalType)1 JSDocInfo (com.google.javascript.rhino.JSDocInfo)1