fix(es/transforms/optimization): Fix inline_globals (#2524)

swc_ecma_transforms_optimization:
 - `inline_globals`: Skip assignment to `process.env.FOO`. (#2499)
This commit is contained in:
Donny/강동윤 2021-10-25 06:51:08 +09:00 committed by GitHub
parent cead404a53
commit 709b3c7cd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 2 deletions

2
Cargo.lock generated
View File

@ -2884,7 +2884,7 @@ dependencies = [
[[package]]
name = "swc_ecma_transforms_optimization"
version = "0.60.0"
version = "0.60.1"
dependencies = [
"ahash",
"dashmap",

View File

@ -25,6 +25,7 @@
"cname",
"combinator",
"Combinator",
"cond",
"Cond",
"constness",
"corejs",

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_optimization"
repository = "https://github.com/swc-project/swc.git"
version = "0.60.0"
version = "0.60.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]

View File

@ -68,6 +68,24 @@ impl Parallel for InlineGlobals {
impl VisitMut for InlineGlobals {
noop_visit_mut_type!();
fn visit_mut_assign_expr(&mut self, n: &mut AssignExpr) {
n.right.visit_mut_with(self);
match &mut n.left {
PatOrExpr::Expr(l) => {
(&mut **l).visit_mut_children_with(self);
}
PatOrExpr::Pat(l) => match &mut **l {
Pat::Expr(l) => {
(&mut **l).visit_mut_children_with(self);
}
_ => {
l.visit_mut_with(self);
}
},
}
}
fn visit_mut_expr(&mut self, expr: &mut Expr) {
match expr {
Expr::Ident(Ident { ref sym, span, .. }) => {
@ -336,4 +354,16 @@ mod tests {
"const test = process.env['x']",
"const test = 'FOO'"
);
test!(
Default::default(),
|tester| inline_globals(
envs(tester, &[("x", "BAR")]),
globals(tester, &[]),
Default::default(),
),
issue_2499_1,
"process.env.x = 'foo'",
"process.env.x = 'foo'"
);
}