Repository: parcel-bundler/parcel
File: packages/transformers/js/core/src/dependency_collector.rs (Rust)

Code before:
              // Promise.resolve(require('foo'))
              if match_member_expr(member, vec!["Promise", "resolve"], self.decls) {
                if let Some(expr) = node.args.get(0) {
                  if match_require(&*expr.expr, self.decls, Mark::fresh(Mark::root())).is_some() {
                    self.in_promise = true;
                    let node = node.fold_children_with(self);
                    self.in_promise = was_in_promise;
                    return node;
                  }
                }
              }

              // Match compiled dynamic imports (TypeScript)
              // Promise.resolve().then(() => require('foo'))
              // Promise.resolve().then(() => { return require('foo') })
              // Promise.resolve().then(function () { return require('foo') })
              //   but not
              // Promise.resolve(require('foo'))
              if let Call(call) = &*member.obj {
                if let Callee::Expr(e) = &call.callee {
                  if let Member(m) = &**e {
                    if match_member_expr(m, vec!["Promise", "resolve"], self.decls) &&
                      // Make sure the arglist is empty.
                      // I.e. do not proceed with the below unless Promise.resolve has an empty arglist
                      // because build_promise_chain() will not work in this case.                   
                      !call.args.get(0).is_some()
                    {
                      if let MemberProp::Ident(id) = &member.prop {
                        if id.sym.to_string().as_str() == "then" {
                          if let Some(arg) = node.args.get(0) {
                            match &*arg.expr {
                              Fn(_) | Arrow(_) => {
                                self.in_promise = true;
                                let node =
                                  swc_ecmascript::visit::fold_call_expr(self, node.clone());
                                self.in_promise = was_in_promise;

                                // Transform Promise.resolve().then(() => __importStar(require('foo')))
                                //   => Promise.resolve().then(() => require('foo')).then(res => __importStar(res))
                                if let Some(require_node) = self.require_node.clone() {
                                  self.require_node = None;
                                  return build_promise_chain(node, require_node);
                                }
                              }
                              _ => {}
                            }
                          }
                        }
                      }
                    }
                  }

Review comment (suggestion):
Is there a reason you didn't do it like this? I had to think for a moment when I first read `!(...).get(0).is_some()`
```suggestion
                      call.args.is_empty()
```

Code after:
              // Promise.resolve(require('foo'))
              if match_member_expr(member, vec!["Promise", "resolve"], self.decls) {
                if let Some(expr) = node.args.get(0) {
                  if match_require(&*expr.expr, self.decls, Mark::fresh(Mark::root())).is_some() {
                    self.in_promise = true;
                    let node = node.fold_children_with(self);
                    self.in_promise = was_in_promise;
                    return node;
                  }
                }
              }

              // Match compiled dynamic imports (TypeScript)
              // Promise.resolve().then(() => require('foo'))
              // Promise.resolve().then(() => { return require('foo') })
              // Promise.resolve().then(function () { return require('foo') })
              //   but not
              // Promise.resolve(require('foo'))
              if let Call(call) = &*member.obj {
                if let Callee::Expr(e) = &call.callee {
                  if let Member(m) = &**e {
                    if match_member_expr(m, vec!["Promise", "resolve"], self.decls) &&
                      // Make sure the arglist is empty.
                      // I.e. do not proceed with the below unless Promise.resolve has an empty arglist
                      // because build_promise_chain() will not work in this case.                   
                      call.args.is_empty()
                    {
                      if let MemberProp::Ident(id) = &member.prop {
                        if id.sym.to_string().as_str() == "then" {
                          if let Some(arg) = node.args.get(0) {
                            match &*arg.expr {
                              Fn(_) | Arrow(_) => {
                                self.in_promise = true;
                                let node =
                                  swc_ecmascript::visit::fold_call_expr(self, node.clone());
                                self.in_promise = was_in_promise;

                                // Transform Promise.resolve().then(() => __importStar(require('foo')))
                                //   => Promise.resolve().then(() => require('foo')).then(res => __importStar(res))
                                if let Some(require_node) = self.require_node.clone() {
                                  self.require_node = None;
                                  return build_promise_chain(node, require_node);
                                }
                              }
                              _ => {}
                            }
                          }
                        }
                      }
                    }
                  }