@@ -4,34 +4,37 @@ use oxc_minifier::RemoveDeadCode;
4
4
use oxc_parser:: Parser ;
5
5
use oxc_span:: SourceType ;
6
6
7
- fn minify ( source_text : & str ) -> String {
7
+ fn print ( source_text : & str , remove_dead_code : bool ) -> String {
8
8
let source_type = SourceType :: default ( ) ;
9
9
let allocator = Allocator :: default ( ) ;
10
10
let ret = Parser :: new ( & allocator, source_text, source_type) . parse ( ) ;
11
11
let program = allocator. alloc ( ret. program ) ;
12
- RemoveDeadCode :: new ( & allocator) . build ( program) ;
12
+ if remove_dead_code {
13
+ RemoveDeadCode :: new ( & allocator) . build ( program) ;
14
+ }
13
15
WhitespaceRemover :: new ( ) . build ( program) . source_text
14
16
}
15
17
16
18
pub ( crate ) fn test ( source_text : & str , expected : & str ) {
17
- let minified = minify ( source_text) ;
19
+ let minified = print ( source_text, true ) ;
20
+ let expected = print ( expected, false ) ;
18
21
assert_eq ! ( minified, expected, "for source {source_text}" ) ;
19
22
}
20
23
21
24
#[ test]
22
25
fn remove_dead_code ( ) {
23
- test ( "if (true) { foo }" , "{foo}" ) ;
24
- test ( "if (true) { foo } else { bar }" , "{foo}" ) ;
25
- test ( "if (false) { foo } else { bar }" , "{bar}" ) ;
26
+ test ( "if (true) { foo }" , "{ foo }" ) ;
27
+ test ( "if (true) { foo } else { bar }" , "{ foo }" ) ;
28
+ test ( "if (false) { foo } else { bar }" , "{ bar }" ) ;
26
29
27
- test ( "if (!false) { foo }" , "{foo}" ) ;
28
- test ( "if (!true) { foo } else { bar }" , "{bar}" ) ;
30
+ test ( "if (!false) { foo }" , "{ foo }" ) ;
31
+ test ( "if (!true) { foo } else { bar }" , "{ bar }" ) ;
29
32
30
- test ( "if ('production' == 'production') { foo } else { bar }" , "{foo}" ) ;
31
- test ( "if ('development' == 'production') { foo } else { bar }" , "{bar}" ) ;
33
+ test ( "if ('production' == 'production') { foo } else { bar }" , "{ foo }" ) ;
34
+ test ( "if ('development' == 'production') { foo } else { bar }" , "{ bar }" ) ;
32
35
33
- test ( "if ('production' === 'production') { foo } else { bar }" , "{foo}" ) ;
34
- test ( "if ('development' === 'production') { foo } else { bar }" , "{bar}" ) ;
36
+ test ( "if ('production' === 'production') { foo } else { bar }" , "{ foo }" ) ;
37
+ test ( "if ('development' === 'production') { foo } else { bar }" , "{ bar }" ) ;
35
38
36
39
test ( "false ? foo : bar;" , "bar" ) ;
37
40
test ( "true ? foo : bar;" , "foo" ) ;
@@ -42,12 +45,35 @@ fn remove_dead_code() {
42
45
test ( "!!false ? foo : bar;" , "bar" ) ;
43
46
test ( "!!true ? foo : bar;" , "foo" ) ;
44
47
45
- test ( "const foo = true ? A : B" , "const foo= A" ) ;
46
- test ( "const foo = false ? A : B" , "const foo= B" ) ;
48
+ test ( "const foo = true ? A : B" , "const foo = A" ) ;
49
+ test ( "const foo = false ? A : B" , "const foo = B" ) ;
47
50
48
51
// Shadowed `undefined` as a variable should not be erased.
49
52
test (
50
53
"function foo(undefined) { if (!undefined) { } }" ,
51
- "function foo(undefined){if(!undefined){}}" ,
54
+ "function foo(undefined) { if (!undefined) { } }" ,
55
+ ) ;
56
+ }
57
+
58
+ // https://github.com/terser/terser/blob/master/test/compress/dead-code.js
59
+ #[ test]
60
+ fn remove_dead_code_from_terser ( ) {
61
+ test (
62
+ "function f() {
63
+ a();
64
+ b();
65
+ x = 10;
66
+ return;
67
+ if (x) {
68
+ y();
69
+ }
70
+ }" ,
71
+ "
72
+ function f() {
73
+ a();
74
+ b();
75
+ x = 10;
76
+ return;
77
+ }" ,
52
78
) ;
53
79
}
0 commit comments