Skip to content

Commit 8ca9909

Browse files
committed
feat(minifier): remove unused assignments for vars (#13231)
I found that the temporary variable are kept. This change would remove them. Variables that were declared with `var` did not have `SymbolValue` populated and that caused those variables to bail out at this line. https://github.com/oxc-project/oxc/blob/2141c18540803218889aa3c3f1bca5f51cdb5daa/crates/oxc_minifier/src/peephole/remove_unused_expression.rs#L624-L626 I think TDZ needs to be considered when inlining the values, but does not affect removing the unused assignments.
1 parent 0e804aa commit 8ca9909

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

crates/oxc_minifier/src/peephole/inline.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ impl<'a> PeepholeOptimizations {
1010
pub fn init_symbol_value(decl: &VariableDeclarator<'a>, ctx: &mut Ctx<'a, '_>) {
1111
let BindingPatternKind::BindingIdentifier(ident) = &decl.id.kind else { return };
1212
let Some(symbol_id) = ident.symbol_id.get() else { return };
13-
// Skip for `var` declarations, due to TDZ problems.
14-
if decl.kind.is_var() {
15-
return;
16-
}
17-
let value =
18-
decl.init.as_ref().map_or(Some(ConstantValue::Undefined), |e| e.evaluate_value(ctx));
13+
let value = if decl.kind.is_var() {
14+
// Skip constant value inlining for `var` declarations, due to TDZ problems.
15+
None
16+
} else {
17+
decl.init.as_ref().map_or(Some(ConstantValue::Undefined), |e| e.evaluate_value(ctx))
18+
};
1919
ctx.init_value(symbol_id, value);
2020
}
2121

crates/oxc_minifier/src/peephole/remove_unused_expression.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,8 @@ mod test {
733733
use crate::{
734734
CompressOptions, TreeShakeOptions,
735735
tester::{
736-
default_options, test, test_options, test_same, test_same_options,
737-
test_same_options_source_type,
736+
default_options, test, test_options, test_options_source_type, test_same,
737+
test_same_options, test_same_options_source_type,
738738
},
739739
};
740740

@@ -1040,13 +1040,16 @@ mod test {
10401040
fn remove_unused_assignment_expression() {
10411041
use oxc_span::SourceType;
10421042
let options = CompressOptions::smallest();
1043-
// Vars are not handled yet due to TDZ.
1044-
test_same_options("var x = 1; x = 2;", &options);
1045-
test_same_options("var x = 1; x = foo();", &options);
1043+
test_options("var x = 1; x = 2;", "", &options);
1044+
test_options("var x = 1; x = foo();", "foo()", &options);
10461045
test_same_options("export var foo; foo = 0;", &options);
10471046
test_same_options("var x = 1; x = 2, foo(x)", &options);
10481047
test_same_options("function foo() { return t = x(); } foo();", &options);
1049-
test_same_options("function foo() { var t; return t = x(); } foo();", &options);
1048+
test_options(
1049+
"function foo() { var t; return t = x(); } foo();",
1050+
"function foo() { return x(); } foo();",
1051+
&options,
1052+
);
10501053
test_same_options("function foo(t) { return t = x(); } foo();", &options);
10511054

10521055
test_options("let x = 1; x = 2;", "", &options);
@@ -1081,8 +1084,9 @@ mod test {
10811084
let source_type = SourceType::cjs();
10821085
test_same_options_source_type("var x = 1; x = 2;", source_type, &options);
10831086
test_same_options_source_type("var x = 1; x = 2, foo(x)", source_type, &options);
1084-
test_same_options_source_type(
1087+
test_options_source_type(
10851088
"function foo() { var x = 1; x = 2, bar() } foo()",
1089+
"function foo() { bar() } foo()",
10861090
source_type,
10871091
&options,
10881092
);

tasks/minsize/minsize.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
| Oxc | ESBuild | Oxc | ESBuild |
22
Original | minified | minified | gzip | gzip | Iterations | File
33
-------------------------------------------------------------------------------------
4-
72.14 kB | 23.38 kB | 23.70 kB | 8.45 kB | 8.54 kB | 2 | react.development.js
4+
72.14 kB | 23.34 kB | 23.70 kB | 8.43 kB | 8.54 kB | 2 | react.development.js
55

66
173.90 kB | 59.48 kB | 59.82 kB | 19.18 kB | 19.33 kB | 2 | moment.js
77

@@ -19,9 +19,9 @@ Original | minified | minified | gzip | gzip | Iterations | Fi
1919

2020
2.14 MB | 715.37 kB | 724.14 kB | 161.66 kB | 181.07 kB | 2 | victory.js
2121

22-
3.20 MB | 1.01 MB | 1.01 MB | 323.98 kB | 331.56 kB | 2 | echarts.js
22+
3.20 MB | 1.01 MB | 1.01 MB | 324.01 kB | 331.56 kB | 2 | echarts.js
2323

24-
6.69 MB | 2.23 MB | 2.31 MB | 461.69 kB | 488.28 kB | 3 | antd.js
24+
6.69 MB | 2.23 MB | 2.31 MB | 461.08 kB | 488.28 kB | 4 | antd.js
2525

26-
10.95 MB | 3.35 MB | 3.49 MB | 860.58 kB | 915.50 kB | 2 | typescript.js
26+
10.95 MB | 3.34 MB | 3.49 MB | 857.11 kB | 915.50 kB | 3 | typescript.js
2727

0 commit comments

Comments
 (0)