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

Code before:
                {
                  self.self_references.insert(key.clone());
                  return Expr::Ident(self.get_export_ident(member.span, &key));
                }
              }
              _ => {}
            }
          }
          _ => {}
        }

        // Don't visit member.prop so we avoid the ident visitor.
        return Expr::Member(MemberExpr {
          span: member.span,
          obj: member.obj.fold_with(self),
          prop: member.prop,
          computed: member.computed,
        });
      }
      Expr::Call(ref call) => {
        // require('foo') -> $id$import$foo
        if let Some(source) = match_require(&node, &self.collect.decls, self.collect.ignore_mark) {
          self.add_require(&source);

          // !!

          return Expr::Ident(self.get_import_ident(
            call.span,
            &source,
            &("*".into()),
            SourceLocation::from(&self.collect.source_map, call.span),
          ));
        }

        if let Some(source) = match_import(&node, self.collect.ignore_mark) {
          self.add_require(&source);
          let name: JsWord = format!("${}$importAsync${:x}", self.module_id, hash!(source)).into();
          self.dynamic_imports.insert(name.clone(), source.clone());
          if self.collect.non_static_requires.contains(&source) || self.collect.should_wrap {
            self.imported_symbols.insert(
              name.clone(),
              (
                source,
                "*".into(),
                SourceLocation::from(&self.collect.source_map, call.span),
              ),
            );
          }
          return Expr::Ident(Ident::new(name, call.span));
        }
      }

Review comment (suggestion):
What we need here is the logic from the old scope hoisting implementation which checked `isUnused()`, so if the return value is unused (`require("x");` is a statement or as the non-last item in a sequence expression)

But that's harder with swc than with Babel

Code after:
                if self.collect.static_cjs_exports
                  && !self.collect.should_wrap
                  && match_member_expr(&mem, vec!["module", "exports"], &self.collect.decls)
                {
                  self.self_references.insert(key.clone());
                  return Expr::Ident(self.get_export_ident(member.span, &key));
                }
              }
              Expr::This(_) => {
                // this.foo -> $id$export$foo
                if self.collect.static_cjs_exports
                  && !self.collect.should_wrap
                  && !self.in_function_scope
                  && !self.collect.is_esm
                {
                  self.self_references.insert(key.clone());
                  return Expr::Ident(self.get_export_ident(member.span, &key));
                }
              }
              _ => {}
            }
          }
          _ => {}
        }

        // Don't visit member.prop so we avoid the ident visitor.
        return Expr::Member(MemberExpr {
          span: member.span,
          obj: member.obj.fold_with(self),
          prop: member.prop,
          computed: member.computed,
        });
      }
      Expr::Call(ref call) => {
        // require('foo') -> $id$import$foo
        if let Some(source) = match_require(&node, &self.collect.decls, self.collect.ignore_mark) {
          self.add_require(&source);
          return Expr::Ident(self.get_import_ident(
            call.span,
            &source,
            &("*".into()),
            SourceLocation::from(&self.collect.source_map, call.span),
          ));
        }

        if let Some(source) = match_import(&node, self.collect.ignore_mark) {
          self.add_require(&source);
          let name: JsWord = format!("${}$importAsync${:x}", self.module_id, hash!(source)).into();
          self.dynamic_imports.insert(name.clone(), source.clone());
          if self.collect.non_static_requires.contains(&source) || self.collect.should_wrap {
            self.imported_symbols.insert(