visitFun()

in lib/semantic.js [1060:1092]


  visitFun(ast) {
    assert.equal(ast.type, 'function');
    const paramMap = this.visitParams(ast.params, {});
    this.checkType(ast.returnType);

    const returnType = ast.returnType;
    if(returnType.type  === 'iterator' && ast.isAsync) {
      this.error(`async function return type must be asyncIterator`, ast.functionName);
    }
    if (ast.functionBody) {
      const local = new Env();
      // merge the parameters into local env
      for (const [key, value] of paramMap) {
        local.set(key, value);
      }
      const env = {
        returnType,
        isStatic: ast.isStatic,
        isAsync: ast.isAsync,
        local: local
      };
      this.visitFunctionBody(ast.functionBody, env);

      if (returnType.tag === Tag.TYPE && returnType.lexeme === 'void') {
        // no check for void
        return;
      }

      if (!this.hasReturnStmt(ast.functionBody.stmts)) {
        this.error(`no return statement`, ast.functionName);
      }
    }
  }