use of com.google.javascript.jscomp.newtypes.JSType 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);
}
use of com.google.javascript.jscomp.newtypes.JSType 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();
}
use of com.google.javascript.jscomp.newtypes.JSType in project closure-compiler by google.
the class NewTypeInference method analyzeVarDeclFwd.
/**
* This method processes a single variable declaration in a VAR statement, in the forward
* phase of the analysis.
*/
private TypeEnv analyzeVarDeclFwd(Node nameNode, TypeEnv inEnv) {
String varName = nameNode.getString();
JSType declType = this.currentScope.getDeclaredTypeOf(varName);
if (this.currentScope.isLocalFunDef(varName)) {
return inEnv;
}
Node rhs = nameNode.getFirstChild();
if (NodeUtil.isNamespaceDecl(nameNode) || (GlobalTypeInfoCollector.isCtorDefinedByCall(nameNode) && !isFunctionBind(rhs.getFirstChild(), inEnv, true)) || nameNode.getParent().getBooleanProp(Node.ANALYZED_DURING_GTI)) {
Preconditions.checkNotNull(declType, "Can't skip var declaration with undeclared type at: %s", nameNode);
if (!rhs.isQualifiedName()) {
analyzeExprFwdIgnoreResult(rhs, inEnv);
}
maybeSetTypeI(nameNode, declType);
maybeSetTypeI(rhs, declType);
return envPutType(inEnv, varName, declType);
}
TypeEnv outEnv = inEnv;
JSType rhsType = null;
if (rhs != null) {
EnvTypePair pair = analyzeExprFwd(rhs, inEnv, firstNonNull(declType, UNKNOWN));
outEnv = pair.env;
rhsType = pair.type;
if (declType != null) {
if (rhsType.isSubtypeOf(declType)) {
registerImplicitUses(rhs, rhsType, declType);
} else {
registerMismatchAndWarn(JSError.make(rhs, MISTYPED_ASSIGN_RHS, errorMsgWithTypeDiff(declType, rhsType)), rhsType, declType);
}
}
}
JSType varType = getInitialTypeOfVar(nameNode, declType, rhsType);
maybeSetTypeI(nameNode, varType);
return envPutType(outEnv, varName, varType);
}
use of com.google.javascript.jscomp.newtypes.JSType in project closure-compiler by google.
the class NewTypeInference method analyzeAddBwd.
private EnvTypePair analyzeAddBwd(Node expr, TypeEnv outEnv, JSType requiredType) {
Node lhs = expr.getFirstChild();
Node rhs = expr.getLastChild();
JSType operandType = requiredType.isNumber() ? NUMBER : UNKNOWN;
EnvTypePair rhsPair = analyzeExprBwd(rhs, outEnv, operandType);
EnvTypePair lhsPair = analyzeExprBwd(lhs, rhsPair.env, operandType);
lhsPair.type = JSType.plus(lhsPair.type, rhsPair.type);
return lhsPair;
}
use of com.google.javascript.jscomp.newtypes.JSType in project closure-compiler by google.
the class NewTypeInference method analyzeAssignAddFwd.
private EnvTypePair analyzeAssignAddFwd(Node expr, TypeEnv inEnv, JSType requiredType) {
mayWarnAboutConst(expr);
Node lhs = expr.getFirstChild();
Node rhs = expr.getLastChild();
JSType lhsReqType = specializeKeep2ndWhenBottom(requiredType, NUMBER_OR_STRING);
LValueResultFwd lvalue = analyzeLValueFwd(lhs, inEnv, lhsReqType);
JSType lhsType = lvalue.type;
if (!lhsType.isSubtypeOf(NUMBER_OR_STRING)) {
warnInvalidOperand(lhs, Token.ASSIGN_ADD, NUMBER_OR_STRING, lhsType);
}
// if lhs is a string, rhs can still be a number
JSType rhsReqType = lhsType.isNumber() ? NUMBER : NUMBER_OR_STRING;
EnvTypePair pair = analyzeExprFwd(rhs, lvalue.env, rhsReqType);
if (!pair.type.isSubtypeOf(rhsReqType)) {
warnInvalidOperand(rhs, Token.ASSIGN_ADD, rhsReqType, pair.type);
}
return pair;
}
Aggregations