Package com.google.javascript.jscomp.newtypes

Examples of com.google.javascript.jscomp.newtypes.JSType


    switch (expr.getType()) {
      case Token.EMPTY: // can be created by a FOR with empty condition
        return new EnvTypePair(inEnv, JSType.UNKNOWN);
      case Token.FUNCTION: {
        String fnName = symbolTable.getFunInternalName(expr);
        JSType fnType = envGetType(inEnv, fnName);
        Preconditions.checkState(fnType != null,
            "Could not find type for %s", fnName);
        return new EnvTypePair(inEnv, fnType);
      }
      case Token.FALSE:
      case Token.NULL:
      case Token.NUMBER:
      case Token.STRING:
      case Token.TRUE:
        return new EnvTypePair(inEnv, scalarValueToType(expr.getType()));
      case Token.OBJECTLIT:
        return analyzeObjLitFwd(expr, inEnv, requiredType, specializedType);
      case Token.THIS: {
        if (!currentScope.hasThis()) {
          warnings.add(JSError.make(expr, CheckGlobalThis.GLOBAL_THIS));
          return new EnvTypePair(inEnv, JSType.UNKNOWN);
        }
        JSType thisType = currentScope.getDeclaredTypeOf("this");
        return new EnvTypePair(inEnv, thisType);
      }
      case Token.NAME:
        return analyzeNameFwd(expr, inEnv, requiredType, specializedType);
      case Token.AND:
View Full Code Here


        currentScope.isLocalExtern(varName) ||
        currentScope.isFormalParam(varName) ||
        currentScope.isLocalFunDef(varName) ||
        currentScope.isOuterVar(varName) ||
        varName.equals(currentScope.getName())) {
      JSType inferredType = envGetType(inEnv, varName);
      println(varName, "'s inferredType: ", inferredType,
          " requiredType:  ", requiredType,
          " specializedType:  ", specializedType);
      if (!inferredType.isSubtypeOf(requiredType)) {
        // The inferred type of a variable is always an upper bound, but
        // sometimes it's also a lower bound, eg, if x was the lhs of an =
        // where we know the type of the rhs.
        // We don't track whether the inferred type is a lower bound, so we
        // conservatively assume that it always is.
        // This is why we warn when !inferredType.isSubtypeOf(requiredType).
        // In some rare cases, the inferred type is only an upper bound,
        // and we would falsely warn.
        // (These usually include the polymorphic operators += and <.)
        // We have a heuristic check to avoid the spurious warnings,
        // but we also miss some true warnings.
        JSType declType = currentScope.getDeclaredTypeOf(varName);
        if (tightenTypeAndDontWarn(
            varName, declType, inferredType, requiredType)) {
          inferredType = inferredType.specialize(requiredType);
        } else {
          // Propagate incorrect type so that the context catches
          // the mismatch
          return new EnvTypePair(inEnv, inferredType);
        }
      }
      // If preciseType is bottom, there is a condition that can't be true,
      // but that's not necessarily a type error.
      JSType preciseType = inferredType.specialize(specializedType);
      println(varName, "'s preciseType: ", preciseType);
      if (!preciseType.isBottom() &&
          currentScope.isUndeclaredFormal(varName) &&
          preciseType.hasNonScalar()) {
        // In the bwd direction, we may infer a loose type and then join w/
        // top and forget it. That's why we also loosen types going fwd.
        preciseType = preciseType.withLoose();
      }
      return EnvTypePair.addBinding(inEnv, varName, preciseType);
    }
    println("Found global variable ", varName);
    // For now, we don't warn for global variables
View Full Code Here

          analyzeExprFwd(rhs, lhsPair.env, JSType.UNKNOWN, specializedType);
      return EnvTypePair.join(rhsPair, shortCircuitPair);
    } else {
      // Independently of the specializedType, && rhs is only analyzed when
      // lhs is truthy, and || rhs is only analyzed when lhs is falsy.
      JSType stopAfterLhsType = exprKind == Token.AND ?
          JSType.FALSY : JSType.TRUTHY;
      EnvTypePair shortCircuitPair =
          analyzeExprFwd(lhs, inEnv, requiredType, stopAfterLhsType);
      EnvTypePair lhsPair = analyzeExprFwd(
          lhs, inEnv, JSType.UNKNOWN, stopAfterLhsType.negate());
      EnvTypePair rhsPair =
          analyzeExprFwd(rhs, lhsPair.env, requiredType, specializedType);
      return EnvTypePair.join(rhsPair, shortCircuitPair);
    }
  }
View Full Code Here

      // 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))) {
        pair.type = requiredType;
        return pair;
      }
    }
View Full Code Here

    Node ctor = expr.getLastChild();
    EnvTypePair objPair, ctorPair;

    // First, evaluate ignoring the specialized context
    objPair = analyzeExprFwd(obj, inEnv);
    JSType objType = objPair.type;
    if (!objType.equals(JSType.TOP) &&
        !objType.equals(JSType.UNKNOWN) &&
        !objType.hasNonScalar()) {
      warnInvalidOperand(
          obj, Token.INSTANCEOF,
          "an object or a union type that includes an object",
          objPair.type);
    }
    ctorPair = analyzeExprFwd(ctor, objPair.env, JSType.topFunction());
    JSType ctorType = ctorPair.type;
    FunctionType ctorFunType = ctorType.getFunType();
    if (!ctorType.isUnknown() &&
        (!ctorType.isSubtypeOf(JSType.topFunction()) ||
            !ctorFunType.isQmarkFunction() && !ctorFunType.isConstructor())) {
      warnInvalidOperand(
          ctor, Token.INSTANCEOF, "a constructor function", ctorType);
    }
    if (ctorFunType == null || !ctorFunType.isConstructor() ||
        (!specializedType.isTruthy() && !specializedType.isFalsy())) {
      ctorPair.type = JSType.BOOLEAN;
      return ctorPair;
    }

    if (ctorFunType.isGeneric()) {
      ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
      for (String typeVar : ctorFunType.getTypeParameters()) {
        builder.put(typeVar, JSType.UNKNOWN);
      }
      ctorFunType = ctorFunType.instantiateGenerics(builder.build());
    }
    // We are in a specialized context *and* we know the constructor type
    JSType instanceType = ctorFunType.getTypeOfThis();
    objPair = analyzeExprFwd(obj, inEnv, JSType.UNKNOWN,
        specializedType.isTruthy() ?
        objPair.type.specialize(instanceType) :
        objPair.type.removeType(instanceType));
    ctorPair = analyzeExprFwd(ctor, objPair.env, JSType.topFunction());
View Full Code Here

  private EnvTypePair analyzeAddFwd(Node expr, TypeEnv inEnv) {
    Node lhs = expr.getFirstChild();
    Node rhs = expr.getLastChild();
    EnvTypePair lhsPair = analyzeExprFwd(lhs, inEnv, JSType.NUM_OR_STR);
    EnvTypePair rhsPair = analyzeExprFwd(rhs, lhsPair.env, JSType.NUM_OR_STR);
    JSType lhsType = lhsPair.type;
    JSType rhsType = rhsPair.type;
    if (!lhsType.isSubtypeOf(JSType.NUM_OR_STR)) {
      warnInvalidOperand(lhs, expr.getType(), JSType.NUM_OR_STR, lhsType);
    }
    if (!rhsType.isSubtypeOf(JSType.NUM_OR_STR)) {
      warnInvalidOperand(rhs, expr.getType(), JSType.NUM_OR_STR, rhsType);
    }
    return new EnvTypePair(rhsPair.env, JSType.plus(lhsType, rhsType));
  }
View Full Code Here

    if (lhs.getBooleanProp(Node.ANALYZED_DURING_GTI)) {
      lhs.removeProp(Node.ANALYZED_DURING_GTI);
      if (rhs.matchesQualifiedName(ABSTRACT_METHOD_NAME)) {
        return new EnvTypePair(inEnv, requiredType);
      }
      JSType declType = getDeclaredTypeOfQname(lhs, inEnv);
      EnvTypePair rhsPair = analyzeExprFwd(rhs, inEnv, declType);
      if (!rhsPair.type.isSubtypeOf(declType)) {
        warnings.add(JSError.make(expr, MISTYPED_ASSIGN_RHS,
                declType.toString(), rhsPair.type.toString()));
      }
      return rhsPair;
    }
    LValueResultFwd lvalue = analyzeLValueFwd(lhs, inEnv, requiredType);
    JSType declType = lvalue.declType;
    EnvTypePair rhsPair =
        analyzeExprFwd(rhs, lvalue.env, requiredType, specializedType);
    if (declType != null && !rhsPair.type.isSubtypeOf(declType)) {
      warnings.add(JSError.make(expr, MISTYPED_ASSIGN_RHS,
              declType.toString(), rhsPair.type.toString()));
    } else {
      rhsPair.env = updateLvalueTypeInEnv(
          rhsPair.env, lhs, lvalue.ptr, rhsPair.type);
    }
    return rhsPair;
View Full Code Here

  private EnvTypePair analyzeAssignAddFwd(
      Node expr, TypeEnv inEnv, JSType requiredType) {
    mayWarnAboutConst(expr);
    Node lhs = expr.getFirstChild();
    Node rhs = expr.getLastChild();
    JSType lhsReqType =
        specializeWithCorrection(requiredType, JSType.NUM_OR_STR);
    LValueResultFwd lvalue = analyzeLValueFwd(lhs, inEnv, lhsReqType);
    JSType lhsType = lvalue.type;
    if (!lhsType.isSubtypeOf(JSType.NUM_OR_STR)) {
      warnInvalidOperand(lhs, Token.ASSIGN_ADD, JSType.NUM_OR_STR, lhsType);
    }
    // if lhs is a string, rhs can still be a number
    JSType rhsReqType = lhsType.equals(JSType.NUMBER) ?
        JSType.NUMBER : JSType.NUM_OR_STR;
    EnvTypePair pair = analyzeExprFwd(rhs, lvalue.env, rhsReqType);
    if (!pair.type.isSubtypeOf(rhsReqType)) {
      warnInvalidOperand(rhs, Token.ASSIGN_ADD, rhsReqType, pair.type);
    }
View Full Code Here

  private EnvTypePair analyzeAssignNumericOpFwd(Node expr, TypeEnv inEnv) {
    mayWarnAboutConst(expr);
    Node lhs = expr.getFirstChild();
    Node rhs = expr.getLastChild();
    LValueResultFwd lvalue = analyzeLValueFwd(lhs, inEnv, JSType.NUMBER);
    JSType lhsType = lvalue.type;
    boolean lhsWarned = false;
    if (!lhsType.isSubtypeOf(JSType.NUMBER)) {
      warnInvalidOperand(lhs, expr.getType(), JSType.NUMBER, lhsType);
      lhsWarned = true;
    }
    EnvTypePair pair = analyzeExprFwd(rhs, lvalue.env, JSType.NUMBER);
    if (!pair.type.isSubtypeOf(JSType.NUMBER)) {
View Full Code Here

        rhs.isName() && rhsPair.type.isUnknown()) {
      TypeEnv env = envPutType(rhsPair.env, lhs.getString(), JSType.TOP_SCALAR);
      env = envPutType(rhsPair.env, rhs.getString(), JSType.TOP_SCALAR);
      return new EnvTypePair(env, JSType.BOOLEAN);
    }
    JSType lhsType = lhsPair.type;
    JSType rhsType = rhsPair.type;
    if (!lhsType.isSubtypeOf(JSType.TOP_SCALAR) ||
        !rhsType.isSubtypeOf(JSType.TOP_SCALAR) ||
        !JSType.areCompatibleScalarTypes(lhsType, rhsType)) {
      warnInvalidOperand(expr, expr.getType(), "matching scalar types",
          lhsType.toString() + ", " + rhsType.toString());
    }
    rhsPair.type = JSType.BOOLEAN;
    return rhsPair;
  }
View Full Code Here

TOP

Related Classes of com.google.javascript.jscomp.newtypes.JSType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.