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

Code before:
impl<'a> Fold for EnvReplacer<'a> {
  fn fold_expr(&mut self, node: Expr) -> Expr {
    // Replace assignments to process.browser with `true`
    // TODO: this seems questionable but we did it in the JS version??
    if let Expr::Assign(ref assign) = node {
      if let PatOrExpr::Pat(ref pat) = assign.left {
        if let Pat::Expr(ref expr) = &**pat {
          if let Expr::Member(ref member) = &**expr {
            if self.is_browser && match_member_expr(member, vec!["process", "browser"], self.decls)
            {
              let mut res = assign.clone();
              res.right = Box::new(Expr::Lit(Lit::Bool(Bool {
                value: true,
                span: DUMMY_SP,
              })));
              return Expr::Assign(res);
            }
          }
        }
      }
    }

    if let Expr::Bin(ref binary) = node {
      if let Expr::Member(ref member) = &*binary.right {
        if let Expr::Lit(ref lit) = &*binary.left {
          if let Lit::Str(ref left) = lit {
            let obj = &member.obj;
            match &**obj {
              Expr::Ident(ref ident) => {
                if &ident.sym == "process" {
                  if let MemberProp::Ident(ref prop) = member.prop {
                    if &prop.sym == "env" {
                      if self.env.contains_key(&left.value) {
                        return Expr::Lit(Lit::Bool(Bool {
                          value: true,
                          span: DUMMY_SP,
                        }));
                      } else {
                        return Expr::Lit(Lit::Bool(Bool {
                          value: false,
                          span: DUMMY_SP,
                        }));
                      }
                    }
                  }
                }
              }
              _ => {}
            }
          }
        }

Review comment (suggestion):
I'm getting some clippy warnings that 
1. the match can be replaced with an `if let` because there's only one branch.
2. you can save one `if let` by doing `if let Expr::Lit(Lit::Str(ref left))`.

Code after:
impl<'a> Fold for EnvReplacer<'a> {
  fn fold_expr(&mut self, node: Expr) -> Expr {
    // Replace assignments to process.browser with `true`
    // TODO: this seems questionable but we did it in the JS version??
    if let Expr::Assign(ref assign) = node {
      if let PatOrExpr::Pat(ref pat) = assign.left {
        if let Pat::Expr(ref expr) = &**pat {
          if let Expr::Member(ref member) = &**expr {
            if self.is_browser && match_member_expr(member, vec!["process", "browser"], self.decls)
            {
              let mut res = assign.clone();
              res.right = Box::new(Expr::Lit(Lit::Bool(Bool {
                value: true,
                span: DUMMY_SP,
              })));
              return Expr::Assign(res);
            }
          }
        }
      }
    }

    // Replace `'foo' in process.env` with a boolean.
    match &node {
      Expr::Bin(binary) if binary.op == BinaryOp::In => {
        if let (Expr::Lit(Lit::Str(left)), Expr::Member(member)) = (&*binary.left, &*binary.right) {
          if match_member_expr(member, vec!["process", "env"], self.decls) {
            return Expr::Lit(Lit::Bool(Bool {
              value: self.env.contains_key(&left.value),
              span: DUMMY_SP,
            }));
          }
        }
      }
      _ => {}
    }

    if let Expr::Member(ref member) = node {
      if self.is_browser && match_member_expr(member, vec!["process", "browser"], self.decls) {
        return Expr::Lit(Lit::Bool(Bool {
          value: true,
          span: DUMMY_SP,
        }));
      }

      if !self.replace_env {
        return node.fold_children_with(self);
      }

      if let Expr::Member(obj) = &*member.obj {
        if match_member_expr(obj, vec!["process", "env"], self.decls) {