Skip to content

Commit

Permalink
fix(es/transforms/optimization): Fix inline_globals (#2524)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_optimization:
 - `inline_globals`: Skip assignment to `process.env.FOO`. (#2499)
  • Loading branch information
kdy1 authored Oct 24, 2021
1 parent cead404 commit 709b3c7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"cname",
"combinator",
"Combinator",
"cond",
"Cond",
"constness",
"corejs",
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/transforms/optimization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
30 changes: 30 additions & 0 deletions ecmascript/transforms/optimization/src/inline_globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, .. }) => {
Expand Down Expand Up @@ -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'"
);
}

1 comment on commit 709b3c7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 709b3c7 Previous: cead404 Ratio
base_tr_fixer 20783 ns/iter (± 209) 23360 ns/iter (± 318) 0.89
base_tr_resolver_and_hygiene 116192 ns/iter (± 11832) 127551 ns/iter (± 24574) 0.91
codegen_es2015 45834 ns/iter (± 269) 52192 ns/iter (± 929) 0.88
codegen_es2016 45564 ns/iter (± 304) 51661 ns/iter (± 380) 0.88
codegen_es2017 45871 ns/iter (± 372) 51627 ns/iter (± 272) 0.89
codegen_es2018 45445 ns/iter (± 335) 52187 ns/iter (± 383) 0.87
codegen_es2019 45750 ns/iter (± 334) 52079 ns/iter (± 318) 0.88
codegen_es2020 45889 ns/iter (± 366) 52011 ns/iter (± 393) 0.88
codegen_es3 45863 ns/iter (± 327) 51935 ns/iter (± 336) 0.88
codegen_es5 45549 ns/iter (± 315) 52171 ns/iter (± 650) 0.87
config_for_file 11442 ns/iter (± 3267) 13034 ns/iter (± 4908) 0.88
full_es2015 162090081 ns/iter (± 4675391) 181730729 ns/iter (± 5602452) 0.89
full_es2016 119878277 ns/iter (± 15430320) 140240127 ns/iter (± 6808134) 0.85
full_es2017 134314928 ns/iter (± 9249817) 148465626 ns/iter (± 9550585) 0.90
full_es2018 133734623 ns/iter (± 6041954) 150030126 ns/iter (± 12044022) 0.89
full_es2019 133150678 ns/iter (± 9813186) 148204256 ns/iter (± 6923935) 0.90
full_es2020 132916019 ns/iter (± 7547172) 145148739 ns/iter (± 7510157) 0.92
full_es3 192789481 ns/iter (± 22366126) 212895004 ns/iter (± 18211005) 0.91
full_es5 179899506 ns/iter (± 11257463) 205430862 ns/iter (± 12639404) 0.88
parser 575801 ns/iter (± 24040) 648282 ns/iter (± 15980) 0.89
transforms_es2015 897456 ns/iter (± 36772) 1042096 ns/iter (± 108322) 0.86
transforms_es2016 530924 ns/iter (± 16328) 593871 ns/iter (± 21926) 0.89
transforms_es2017 515406 ns/iter (± 14352) 576006 ns/iter (± 16597) 0.89
transforms_es2018 508836 ns/iter (± 42109) 568525 ns/iter (± 88585) 0.90
transforms_es2019 494546 ns/iter (± 17449) 559955 ns/iter (± 24590) 0.88
transforms_es2020 488531 ns/iter (± 36202) 540425 ns/iter (± 41215) 0.90
transforms_es3 1021312 ns/iter (± 21818) 1142911 ns/iter (± 41382) 0.89
transforms_es5 890636 ns/iter (± 19898) 1005193 ns/iter (± 38318) 0.89
ser_ast_node 132 ns/iter (± 4) 149 ns/iter (± 3) 0.89
ser_serde 139 ns/iter (± 1) 159 ns/iter (± 1) 0.87
emit_colors 10396781 ns/iter (± 7890722) 7958067 ns/iter (± 5719793) 1.31
emit_large 114180492 ns/iter (± 153959566) 93716104 ns/iter (± 144563385) 1.22
base_clone 2435230 ns/iter (± 152022) 2305129 ns/iter (± 175341) 1.06
fold_span 3847543 ns/iter (± 233692) 3872803 ns/iter (± 235685) 0.99
fold_span_panic 4052015 ns/iter (± 148386) 4054257 ns/iter (± 84029) 1.00
visit_mut_span 2797358 ns/iter (± 141896) 2782658 ns/iter (± 70010) 1.01
visit_mut_span_panic 2872049 ns/iter (± 168322) 2839034 ns/iter (± 129585) 1.01
boxing_boxed 142 ns/iter (± 0) 141 ns/iter (± 0) 1.01
boxing_boxed_clone 67 ns/iter (± 0) 67 ns/iter (± 0) 1
boxing_unboxed 130 ns/iter (± 1) 129 ns/iter (± 0) 1.01
boxing_unboxed_clone 63 ns/iter (± 0) 63 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.