Skip to content

Commit 1b80633

Browse files
committed
feat(minifier): remove unused function declaration (#12318)
1 parent 3f9a1f0 commit 1b80633

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed

crates/oxc_minifier/src/peephole/remove_dead_code.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl<'a> PeepholeOptimizations {
2828
Statement::ForStatement(s) => self.try_fold_for(s, state, ctx),
2929
Statement::TryStatement(s) => Self::try_fold_try(s, ctx),
3030
Statement::LabeledStatement(s) => Self::try_fold_labeled(s, ctx),
31+
Statement::FunctionDeclaration(f) => Self::remove_unused_function_declaration(f, ctx),
3132
_ => None,
3233
} {
3334
*stmt = new_stmt;
@@ -565,6 +566,21 @@ impl<'a> PeepholeOptimizations {
565566
_ => false,
566567
}
567568
}
569+
570+
fn remove_unused_function_declaration(
571+
f: &Function<'a>,
572+
ctx: &mut Ctx<'a, '_>,
573+
) -> Option<Statement<'a>> {
574+
if ctx.state.options.unused == CompressOptionsUnused::Keep {
575+
return None;
576+
}
577+
let id = f.id.as_ref()?;
578+
let symbol_id = id.symbol_id.get()?;
579+
if ctx.scoping().symbol_is_unused(symbol_id) {
580+
return Some(ctx.ast.statement_empty(f.span));
581+
}
582+
None
583+
}
568584
}
569585

570586
impl<'a> LatePeepholeOptimizations {
@@ -587,7 +603,10 @@ impl<'a> LatePeepholeOptimizations {
587603
/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/PeepholeRemoveDeadCodeTest.java>
588604
#[cfg(test)]
589605
mod test {
590-
use crate::tester::{test, test_same};
606+
use crate::{
607+
CompressOptions,
608+
tester::{test, test_options, test_same},
609+
};
591610

592611
#[test]
593612
fn test_fold_block() {
@@ -794,4 +813,10 @@ mod test {
794813
fn remove_constant_value() {
795814
test("const foo = false; if (foo) { console.log('foo') }", "const foo = !1;");
796815
}
816+
817+
#[test]
818+
fn remove_unused_function_declaration() {
819+
let options = CompressOptions::smallest();
820+
test_options("function foo() {}", "", &options);
821+
}
797822
}

crates/oxc_minifier/src/peephole/remove_unused_expression.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ impl<'a> PeepholeOptimizations {
665665
mod test {
666666
use crate::{
667667
CompressOptions, TreeShakeOptions,
668-
tester::{test, test_options, test_same, test_same_options},
668+
tester::{default_options, test, test_options, test_same, test_same_options},
669669
};
670670

671671
#[test]
@@ -948,14 +948,14 @@ mod test {
948948
fn treeshake_options_annotations_false() {
949949
let options = CompressOptions {
950950
treeshake: TreeShakeOptions { annotations: false, ..TreeShakeOptions::default() },
951-
..CompressOptions::smallest()
951+
..default_options()
952952
};
953953
test_same_options("function test() {} /* @__PURE__ */ test()", &options);
954954
test_same_options("function test() {} /* @__PURE__ */ new test()", &options);
955955

956956
let options = CompressOptions {
957957
treeshake: TreeShakeOptions { annotations: true, ..TreeShakeOptions::default() },
958-
..CompressOptions::smallest()
958+
..default_options()
959959
};
960960
test_options("function test() {} /* @__PURE__ */ test()", "function test() {}", &options);
961961
test_options(

crates/oxc_minifier/tests/peephole/dead_code_elimination.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use oxc_minifier::Compressor;
77
use oxc_parser::Parser;
88
use oxc_span::SourceType;
99

10+
use super::default_options;
11+
1012
#[track_caller]
1113
fn run(source_text: &str, source_type: SourceType, options: Option<CompressOptions>) -> String {
1214
let allocator = Allocator::default();
@@ -26,7 +28,7 @@ fn test(source_text: &str, expected: &str) {
2628
let source_text = source_text.cow_replace("false", f);
2729

2830
let source_type = SourceType::default();
29-
let result = run(&source_text, source_type, Some(CompressOptions::default()));
31+
let result = run(&source_text, source_type, Some(default_options()));
3032
let expected = run(expected, source_type, None);
3133
assert_eq!(result, expected, "\nfor source\n{source_text}\nexpect\n{expected}\ngot\n{result}");
3234
}
@@ -229,6 +231,7 @@ fn dce_from_terser() {
229231
"#,
230232
r#"function f() {
231233
g();
234+
x = 10;
232235
throw new Error("foo");
233236
var x;
234237
}

crates/oxc_minifier/tests/peephole/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ mod statement_fusion;
77

88
use oxc_minifier::{CompressOptions, CompressOptionsUnused};
99

10-
#[track_caller]
11-
fn test(source_text: &str, expected: &str) {
12-
let options = CompressOptions {
10+
pub fn default_options() -> CompressOptions {
11+
CompressOptions {
1312
drop_debugger: false,
14-
drop_console: false,
1513
unused: CompressOptionsUnused::Keep,
1614
..CompressOptions::smallest()
17-
};
18-
crate::test(source_text, expected, options);
15+
}
16+
}
17+
18+
#[track_caller]
19+
fn test(source_text: &str, expected: &str) {
20+
crate::test(source_text, expected, default_options());
1921
}
2022

2123
#[track_caller]

napi/minify/test/terser.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,19 +3385,17 @@ test('inline_script_on', () => {
33853385
});
33863386

33873387
test('test_unexpected_crash', () => {
3388-
const prepend = 'x();';
33893388
const code =
3390-
'function x(){var getsInlined=function(){var leakedVariable1=3;var leakedVariable2=1+2*leakedVariable1;console.log(leakedVariable1);console.log(leakedVariable2)};var getsDropped=getsInlined()}';
3389+
'x(); function x(){var getsInlined=function(){var leakedVariable1=3;var leakedVariable2=1+2*leakedVariable1;console.log(leakedVariable1);console.log(leakedVariable2)};var getsDropped=getsInlined()}';
33913390
const expected = ['3', '7'];
3392-
run(code, expected, prepend);
3391+
run(code, expected);
33933392
});
33943393

33953394
test('test_unexpected_crash_2', () => {
3396-
const prepend = 'x();';
33973395
const code =
3398-
'function x(){var getsInlined=function(){var leakedVariable1=3;var leakedVariable2=1+leakedVariable1[0];console.log(leakedVariable1);console.log(leakedVariable2)};var getsDropped=getsInlined()}';
3396+
'x(); function x(){var getsInlined=function(){var leakedVariable1=3;var leakedVariable2=1+leakedVariable1[0];console.log(leakedVariable1);console.log(leakedVariable2)};var getsDropped=getsInlined()}';
33993397
const expected = ['3', 'NaN'];
3400-
run(code, expected, prepend);
3398+
run(code, expected);
34013399
});
34023400

34033401
test('issue_1724', () => {

tasks/minsize/minsize.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Original | minified | minified | gzip | gzip | Fixture
2121

2222
3.20 MB | 1.01 MB | 1.01 MB | 324.08 kB | 331.56 kB | echarts.js
2323

24-
6.69 MB | 2.25 MB | 2.31 MB | 463.18 kB | 488.28 kB | antd.js
24+
6.69 MB | 2.24 MB | 2.31 MB | 462.45 kB | 488.28 kB | antd.js
2525

2626
10.95 MB | 3.34 MB | 3.49 MB | 856.90 kB | 915.50 kB | typescript.js
2727

0 commit comments

Comments
 (0)