fn build_promise_chain(node: ast::CallExpr, require_node: ast::CallExpr) -> ast::CallExpr {
  let mut transformer = PromiseTransformer {
    require_node: Some(require_node),
  };

  let node = node.fold_with(&mut transformer);

  if let Some(require_node) = &transformer.require_node {
    if let Some(f) = node.args.get(0) {
      // Add `res` as an argument to the original function
      let f = match &*f.expr {
        ast::Expr::Fn(f) => {
          let mut f = f.clone();
          f.function.params.insert(
            0,
            ast::Param {
              pat: ast::Pat::Ident(ast::BindingIdent::from(ast::Ident::new(
                "res".into(),
                DUMMY_SP,
              ))),
              decorators: vec![],
              span: DUMMY_SP,
            },
          );
          ast::Expr::Fn(f)
        }
        ast::Expr::Arrow(f) => {
          let mut f = f.clone();
          f.params.insert(
            0,
            ast::Pat::Ident(ast::BindingIdent::from(ast::Ident::new(
              "res".into(),
              DUMMY_SP,
            ))),
          );
          ast::Expr::Arrow(f)
        }
        _ => return node,
      };

      return ast::CallExpr {
        callee: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Member(ast::MemberExpr {
          span: DUMMY_SP,
          computed: false,
          obj: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Call(ast::CallExpr {
            callee: node.callee,
            args: vec![ast::ExprOrSpread {
              expr: Box::new(ast::Expr::Fn(ast::FnExpr {
                ident: None,
                function: ast::Function {
                  body: Some(ast::BlockStmt {
                    span: DUMMY_SP,
                    stmts: vec![ast::Stmt::Return(ast::ReturnStmt {
                      span: DUMMY_SP,
                      arg: Some(Box::new(ast::Expr::Call(require_node.clone()))),
                    })],
                  }),
                  params: vec![],
                  decorators: vec![],
                  is_async: false,
                  is_generator: false,
                  return_type: None,
                  type_params: None,
                  span: DUMMY_SP,
                },
              })),
              spread: None,
            }],
            span: DUMMY_SP,
            type_args: None,
          }))),
          prop: Box::new(ast::Expr::Ident(ast::Ident::new("then".into(), DUMMY_SP))),
        }))),
        args: vec![ast::ExprOrSpread {
          expr: Box::new(f),
          spread: None,
        }],
        span: DUMMY_SP,
        type_args: None,
      };
    }
  }

  return node;
}
// If the `require` call is not immediately returned (e.g. wrapped in another function),
// then transform the AST to create a promise chain so that the require is by itself.
// This is because the require will return a promise rather than the module synchronously.
// For example, TypeScript generates the following with the esModuleInterop flag:
//   Promise.resolve().then(() => __importStar(require('./foo')));
// This is transformed into:
//   Promise.resolve().then(() => require('./foo')).then(res => __importStar(res));
If the `require` call is not immediately returned ,
then transform the AST to create a promise chain so that the require is by itself.
This is because the require will return a promise rather than the module synchronously.
If the ` require ` call is not immediately returned then transform the AST to create a promise chain so that the require is by itself . This is because the require will return a promise rather than the module synchronously .
fn build_promise_chain(node: ast::CallExpr, require_node: ast::CallExpr) -> ast::CallExpr {
  let mut transformer = PromiseTransformer {
    require_node: Some(require_node),
  };
  let node = node.fold_with(&mut transformer);
  if let Some(require_node) = &transformer.require_node {
    if let Some(f) = node.args.get(0) {
      // Add `res` as an argument to the original function
      let f = match &*f.expr {
        ast::Expr::Fn(f) => {
          let mut f = f.clone();
          f.function.params.insert(
            0,
            ast::Param {
              pat: ast::Pat::Ident(ast::BindingIdent::from(ast::Ident::new(
                "res".into(),
                DUMMY_SP,
              ))),
              decorators: vec![],
              span: DUMMY_SP,
            },
          );
          ast::Expr::Fn(f)
        }
        ast::Expr::Arrow(f) => {
          let mut f = f.clone();
          f.params.insert(
            0,
            ast::Pat::Ident(ast::BindingIdent::from(ast::Ident::new(
              "res".into(),
              DUMMY_SP,
            ))),
          );
          ast::Expr::Arrow(f)
        }
        _ => return node,
      };
      return ast::CallExpr {
        callee: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Member(ast::MemberExpr {
          span: DUMMY_SP,
          computed: false,
          obj: ast::ExprOrSuper::Expr(Box::new(ast::Expr::Call(ast::CallExpr {
            callee: node.callee,
            args: vec![ast::ExprOrSpread {
              expr: Box::new(ast::Expr::Fn(ast::FnExpr {
                ident: None,
                function: ast::Function {
                  body: Some(ast::BlockStmt {
                    span: DUMMY_SP,
                    stmts: vec![ast::Stmt::Return(ast::ReturnStmt {
                      span: DUMMY_SP,
                      arg: Some(Box::new(ast::Expr::Call(require_node.clone()))),
                    })],
                  }),
                  params: vec![],
                  decorators: vec![],
                  is_async: false,
                  is_generator: false,
                  return_type: None,
                  type_params: None,
                  span: DUMMY_SP,
                },
              })),
              spread: None,
            }],
            span: DUMMY_SP,
            type_args: None,
          }))),
          prop: Box::new(ast::Expr::Ident(ast::Ident::new("then".into(), DUMMY_SP))),
        }))),
        args: vec![ast::ExprOrSpread {
          expr: Box::new(f),
          spread: None,
        }],
        span: DUMMY_SP,
        type_args: None,
      };
    }
  }
  return node;
}
fn build_promise_chain ( node : ast :: CallExpr , require_node : ast :: CallExpr ) -> ast :: CallExpr { let mut transformer = PromiseTransformer { require_node : Some ( require_node ) , } ; let node = node . fold_with ( & mut transformer ) ; if let Some ( require_node ) = & transformer . require_node { if let Some ( f ) = node . args . get ( 0 ) { // Add `res` as an argument to the original function let f = match & * f . expr { ast :: Expr :: Fn ( f ) => { let mut f = f . clone ( ) ; f . function . params . insert ( 0 , ast :: Param { pat : ast :: Pat :: Ident ( ast :: BindingIdent :: from ( ast :: Ident :: new ( " " . into ( ) , DUMMY_SP , ) ) ) , decorators : vec ! [ ] , span : DUMMY_SP , } , ) ; ast :: Expr :: Fn ( f ) } ast :: Expr :: Arrow ( f ) => { let mut f = f . clone ( ) ; f . params . insert ( 0 , ast :: Pat :: Ident ( ast :: BindingIdent :: from ( ast :: Ident :: new ( " " . into ( ) , DUMMY_SP , ) ) ) , ) ; ast :: Expr :: Arrow ( f ) } _ => return node , } ; return ast :: CallExpr { callee : ast :: ExprOrSuper :: Expr ( Box :: new ( ast :: Expr :: Member ( ast :: MemberExpr { span : DUMMY_SP , computed : false , obj : ast :: ExprOrSuper :: Expr ( Box :: new ( ast :: Expr :: Call ( ast :: CallExpr { callee : node . callee , args : vec ! [ ast ExprOrSpread { expr Box new ( ast Expr Fn ( ast FnExpr { ident None function ast Function { body Some ( ast BlockStmt { span DUMMY_SP stmts vec [ ast Stmt Return ( ast ReturnStmt { span DUMMY_SP arg Some ( Box new ( ast Expr Call ( require_node clone ( ) ) ) ) } ) ] } ) params vec [ ] decorators vec [ ] is_async false is_generator false return_type None type_params None span DUMMY_SP } } ) ) spread None } ] , span : DUMMY_SP , type_args : None , } ) ) ) , prop : Box :: new ( ast :: Expr :: Ident ( ast :: Ident :: new ( " " . into ( ) , DUMMY_SP ) ) ) , } ) ) ) , args : vec ! [ ast ExprOrSpread { expr Box new ( f ) spread None } ] , span : DUMMY_SP , type_args : None , } ; } } return node ; }
If the `require` call is not immediately returned (e.g.
If the ` require ` call is not immediately returned ( e . g .
node
ast::CallExpr
require_node
ast::CallExpr
node
ast::CallExpr
require_node
ast::CallExpr