Skip to content

Commit

Permalink
simplify a little
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Apr 21, 2022
1 parent 0d76522 commit d4ffae8
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions packages/transformers/js/core/src/env_replacer.rs
Expand Up @@ -6,7 +6,6 @@ use swc_common::{SyntaxContext, DUMMY_SP};
use swc_ecmascript::ast;
use swc_ecmascript::visit::{Fold, FoldWith};

use crate::id;
use crate::utils::*;
use ast::*;

Expand Down Expand Up @@ -42,25 +41,19 @@ impl<'a> Fold for EnvReplacer<'a> {
}
}

if let Expr::Bin(ref binary) = node {
if let BinaryOp::In = binary.op {
if let Expr::Member(ref member) = &*binary.right {
if let Expr::Lit(Lit::Str(ref left)) = &*binary.left {
if let Expr::Ident(ref ident) = &*member.obj {
if !self.decls.contains(&id!(ident)) && &ident.sym == "process" {
if let MemberProp::Ident(ref prop) = member.prop {
if &prop.sym == "env" {
return Expr::Lit(Lit::Bool(Bool {
value: self.env.contains_key(&left.value),
span: DUMMY_SP,
}));
}
}
}
}
// 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 {
Expand Down

0 comments on commit d4ffae8

Please sign in to comment.