src/com/google/javascript/jscomp/TypeCheck.java
/**
   * Visits a RETURN node.
   *
   * @param t The node traversal object that supplies context, such as the
   * scope chain to use in name lookups as well as error reporting.
   * @param n The node being visited.
   */
Visits a RETURN node.
@param t The node traversal object that supplies context, such as the
scope chain to use in name lookups as well as error reporting.
@param n The node being visited.
Visits a RETURN node . @param t The node traversal object that supplies context such as the scope chain to use in name lookups as well as error reporting . @param n The node being visited .
private void visitReturn(NodeTraversal t, Node n) {
    Node enclosingFunction = t.getEnclosingFunction();
    if (enclosingFunction.isGeneratorFunction() && !n.hasChildren()) {
      return;
    }
    JSType jsType = getJSType(enclosingFunction);
    if (jsType.isFunctionType()) {
      FunctionType functionType = jsType.toMaybeFunctionType();
      JSType returnType = functionType.getReturnType();
      if (returnType == null) {
        returnType = getNativeType(VOID_TYPE);
      } else if (enclosingFunction.isGeneratorFunction()) {
        returnType = getTemplateTypeOfGenerator(returnType);
      } else if (enclosingFunction.isAsyncFunction()) {
        returnType = Promises.getTemplateTypeOfThenable(typeRegistry, returnType);
      } else if (returnType.isVoidType() && functionType.isConstructor()) {
        if (!n.hasChildren()) {
          return;
        }
        returnType = functionType.getInstanceType();
      }
      Node valueNode = n.getFirstChild();
      JSType actualReturnType;
      if (valueNode == null) {
        actualReturnType = getNativeType(VOID_TYPE);
        valueNode = n;
      } else {
        actualReturnType = getJSType(valueNode);
        if (enclosingFunction.isAsyncFunction()) {
          actualReturnType = Promises.getResolvedType(typeRegistry, actualReturnType);
        }
      }
      validator.expectCanAssignTo(
          t, valueNode, actualReturnType, returnType, "inconsistent return type");
    }
  }
private void visitReturn ( NodeTraversal t , Node n ) { Node enclosingFunction = t . getEnclosingFunction ( ) ; if ( enclosingFunction . isGeneratorFunction ( ) && ! n . hasChildren ( ) ) { return ; } JSType jsType = getJSType ( enclosingFunction ) ; if ( jsType . isFunctionType ( ) ) { FunctionType functionType = jsType . toMaybeFunctionType ( ) ; JSType returnType = functionType . getReturnType ( ) ; if ( returnType == null ) { returnType = getNativeType ( VOID_TYPE ) ; } else if ( enclosingFunction . isGeneratorFunction ( ) ) { returnType = getTemplateTypeOfGenerator ( returnType ) ; } else if ( enclosingFunction . isAsyncFunction ( ) ) { returnType = Promises . getTemplateTypeOfThenable ( typeRegistry , returnType ) ; } else if ( returnType . isVoidType ( ) && functionType . isConstructor ( ) ) { if ( ! n . hasChildren ( ) ) { return ; } returnType = functionType . getInstanceType ( ) ; } Node valueNode = n . getFirstChild ( ) ; JSType actualReturnType ; if ( valueNode == null ) { actualReturnType = getNativeType ( VOID_TYPE ) ; valueNode = n ; } else { actualReturnType = getJSType ( valueNode ) ; if ( enclosingFunction . isAsyncFunction ( ) ) { actualReturnType = Promises . getResolvedType ( typeRegistry , actualReturnType ) ; } } validator . expectCanAssignTo ( t , valueNode , actualReturnType , returnType , "inconsistent return type" ) ; } }
Visits a RETURN node.
Visits a RETURN node .
// Allow "return;" in a generator function, even if it's not the declared return type. // e.g. Don't warn for a generator function with JSDoc "@return {!Generator<number>}" and // a "return;" in the fn body, even though "undefined" does not match "number". // if no return type is specified, undefined must be returned // (it's a void function) // Unwrap the template variable from a generator function's declared return type. // e.g. if returnType is "Generator<string>", make it just "string". // Unwrap the template variable from a async function's declared return type. // e.g. if returnType is "!Promise<string>" or "!IThenable<string>", make it just "string". // Allow constructors to use empty returns for flow control. // Allow constructors to return its own instance type // fetching the returned value's type // We want to treat `return Promise.resolve(1);` as if it were `return 1;` inside an async // function. // verifying