src/com/google/javascript/jscomp/TypeValidator.java
/**
   * Expect that the given variable has not been declared with a type.
   *
   * @param sourceName The name of the source file we're in.
   * @param n The node where warnings should point to.
   * @param parent The parent of {@code n}.
   * @param var The variable that we're checking.
   * @param variableName The name of the variable.
   * @param newType The type being applied to the variable. Mostly just here
   *     for the benefit of the warning.
   * @return The variable we end up with. Most of the time, this will just
   *     be {@code var}, but in some rare cases we will need to declare
   *     a new var with new source info.
   */
Expect that the given variable has not been declared with a type.
@param sourceName The name of the source file we're in.
@param n The node where warnings should point to.
@param parent The parent of  n.
@param var The variable that we're checking.
@param variableName The name of the variable.
@param newType The type being applied to the variable. Mostly just here
for the benefit of the warning.
@return The variable we end up with. Most of the time, this will just
be  var, but in some rare cases we will need to declare
a new var with new source info.
Expect that the given variable has not been declared with a type . @param sourceName The name of the source file we ' re in . @param n The node where warnings should point to . @param parent The parent of n . @param var The variable that we ' re checking . @param variableName The name of the variable . @param newType The type being applied to the variable . Mostly just here for the benefit of the warning . @return The variable we end up with . Most of the time this will just be var but in some rare cases we will need to declare a new var with new source info .
TypedVar expectUndeclaredVariable(String sourceName, CompilerInput input,
      Node n, Node parent, TypedVar var, String variableName, JSType newType) {
    TypedVar newVar = var;
    JSType varType = var.getType();
    if (varType != null
        && varType != typeRegistry.getNativeType(UNKNOWN_TYPE)
        && newType != null
        && newType != typeRegistry.getNativeType(UNKNOWN_TYPE)) {
      if (var.input == null) {
        TypedScope s = var.getScope();
        s.undeclare(var);
        newVar = s.declare(variableName, n, varType, input, false);
        n.setJSType(varType);
        if (parent.isVar()) {
          if (n.hasChildren()) {
            n.getFirstChild().setJSType(varType);
          }
        } else {
          checkState(parent.isFunction() || parent.isClass());
          parent.setJSType(varType);
        }
      } else {
        boolean allowDupe = hasDuplicateDeclarationSuppression(compiler, var.getNameNode());
        if (!allowDupe) {
          if (!newType.isEquivalentTo(varType, true)) {
            report(
                JSError.make(
                    n,
                    DUP_VAR_DECLARATION_TYPE_MISMATCH,
                    variableName,
                    newType.toString(),
                    var.getInputName(),
                    String.valueOf(var.nameNode.getLineno()),
                    varType.toString()));
          } else if (!var.getParentNode().isExprResult()) {
            report(
                JSError.make(
                    n,
                    DUP_VAR_DECLARATION,
                    variableName,
                    var.getInputName(),
                    String.valueOf(var.nameNode.getLineno())));
          }
        }
      }
    }
    return newVar;
  }
TypedVar expectUndeclaredVariable ( String sourceName , CompilerInput input , Node n , Node parent , TypedVar var , String variableName , JSType newType ) { TypedVar newVar = var ; JSType varType = var . getType ( ) ; if ( varType != null && varType != typeRegistry . getNativeType ( UNKNOWN_TYPE ) && newType != null && newType != typeRegistry . getNativeType ( UNKNOWN_TYPE ) ) { if ( var . input == null ) { TypedScope s = var . getScope ( ) ; s . undeclare ( var ) ; newVar = s . declare ( variableName , n , varType , input , false ) ; n . setJSType ( varType ) ; if ( parent . isVar ( ) ) { if ( n . hasChildren ( ) ) { n . getFirstChild ( ) . setJSType ( varType ) ; } } else { checkState ( parent . isFunction ( ) || parent . isClass ( ) ) ; parent . setJSType ( varType ) ; } } else { boolean allowDupe = hasDuplicateDeclarationSuppression ( compiler , var . getNameNode ( ) ) ; if ( ! allowDupe ) { if ( ! newType . isEquivalentTo ( varType , true ) ) { report ( JSError . make ( n , DUP_VAR_DECLARATION_TYPE_MISMATCH , variableName , newType . toString ( ) , var . getInputName ( ) , String . valueOf ( var . nameNode . getLineno ( ) ) , varType . toString ( ) ) ) ; } else if ( ! var . getParentNode ( ) . isExprResult ( ) ) { report ( JSError . make ( n , DUP_VAR_DECLARATION , variableName , var . getInputName ( ) , String . valueOf ( var . nameNode . getLineno ( ) ) ) ) ; } } } } return newVar ; }
Expect that the given variable has not been declared with a type.
Expect that the given variable has not been declared with a type .
// Only report duplicate declarations that have types. Other duplicates // will be reported by the syntactic scope creator later in the // compilation process. // If there are two typed declarations of the same variable, that // is an error and the second declaration is ignored, except in the // case of native types. A null input type means that the declaration // was made in TypedScopeCreator#createInitialScope and is a // native type. We should redeclare it at the new input site. // Check for @suppress duplicate or similar warnings guard on the previous variable // declaration location. // If the previous definition doesn't suppress the warning, emit it here (i.e. always emit // on the second of the duplicate definitions). The warning might still be suppressed by an // @suppress tag on this declaration. // Report specifically if it is not just a duplicate, but types also don't mismatch. // NOTE: structural matches are explicitly allowed here. // If the type matches and the previous declaration was a stub declaration // (isExprResult), then ignore the duplicate, otherwise emit an error.