@@ -7,8 +7,6 @@ use oxc_minifier::Compressor;
77use oxc_parser:: Parser ;
88use oxc_span:: SourceType ;
99
10- use super :: default_options;
11-
1210#[ track_caller]
1311fn run ( source_text : & str , source_type : SourceType , options : Option < CompressOptions > ) -> String {
1412 let allocator = Allocator :: default ( ) ;
@@ -28,7 +26,7 @@ fn test(source_text: &str, expected: &str) {
2826 let source_text = source_text. cow_replace ( "false" , f) ;
2927
3028 let source_type = SourceType :: default ( ) ;
31- let result = run ( & source_text, source_type, Some ( default_options ( ) ) ) ;
29+ let result = run ( & source_text, source_type, Some ( CompressOptions :: dce ( ) ) ) ;
3230 let expected = run ( expected, source_type, None ) ;
3331 assert_eq ! ( result, expected, "\n for source\n {source_text}\n expect\n {expected}\n got\n {result}" ) ;
3432}
@@ -64,8 +62,8 @@ fn dce_if_statement() {
6462 "if (xxx) foo; else bar" ,
6563 ) ;
6664 test (
67- "if (xxx) { foo } else if (false) { var a; var b; } else if (false) { var c; var d; }" ,
68- "if (xxx) foo; else if (0) var a, b; else if (0) var c, d;" ,
65+ "if (xxx) { foo } else if (false) { var a; var b; } else if (false) { var c; var d; } f(a,b,c,d) " ,
66+ "if (xxx) foo; else if (0) var a, b; else if (0) var c, d; f(a,b,c,d) " ,
6967 ) ;
7068
7169 test ( "if (!false) { foo }" , "foo" ) ;
@@ -88,18 +86,18 @@ fn dce_if_statement() {
8886 // Shadowed `undefined` as a variable should not be erased.
8987 // This is a rollup test.
9088 // <https://github.com/rollup/rollup/blob/master/test/function/samples/allow-undefined-as-parameter/main.js>
91- test_same ( "function foo(undefined) { if (!undefined) throw Error('') }" ) ;
89+ test_same ( "function foo(undefined) { if (!undefined) throw Error('') } foo() " ) ;
9290
93- test ( "function foo() { if (undefined) { bar } }" , "function foo() { }" ) ;
94- test ( "function foo() { { bar } }" , "function foo() { bar }" ) ;
91+ test ( "function foo() { if (undefined) { bar } } foo() " , "function foo() { } foo() " ) ;
92+ test ( "function foo() { { bar } } foo() " , "function foo() { bar } foo() " ) ;
9593
9694 test ( "if (true) { foo; } if (true) { foo; }" , "foo; foo;" ) ;
97- test ( "if (true) { foo; return } foo; if (true) { bar; return } bar;" , "{ foo; return } " ) ;
95+ test ( "if (true) { foo; return } foo; if (true) { bar; return } bar;" , "foo; return" ) ;
9896
9997 // nested expression
10098 test (
101- "const a = { fn: function() { if (true) { foo; } } }" ,
102- "const a = { fn: function() { foo; } }" ,
99+ "const a = { fn: function() { if (true) { foo; } } } bar(a) " ,
100+ "const a = { fn: function() { foo; } } bar(a) " ,
103101 ) ;
104102
105103 // parenthesized
@@ -128,61 +126,67 @@ fn dce_conditional_expression() {
128126 test ( "!!false ? foo : bar;" , "bar" ) ;
129127 test ( "!!true ? foo : bar;" , "foo" ) ;
130128
131- test ( "const foo = true ? A : B" , "const foo = A" ) ;
132- test ( "const foo = false ? A : B" , "const foo = B" ) ;
129+ test ( "const foo = true ? A : B" , "A" ) ;
130+ test ( "const foo = false ? A : B" , "B" ) ;
133131}
134132
135133#[ test]
136134fn dce_logical_expression ( ) {
137135 test ( "false && bar()" , "" ) ;
138136 test ( "true && bar()" , "bar()" ) ;
139137
140- test ( "const foo = false && bar()" , "const foo = false" ) ;
141- test ( "const foo = true && bar()" , "const foo = bar()" ) ;
138+ test ( "var foo = false && bar(); baz(foo)" , "var foo = false; baz(foo)" ) ;
139+ test ( "var foo = true && bar(); baz(foo)" , "var foo = bar(); baz(foo)" ) ;
140+
141+ test ( "foo = false && bar()" , "foo = false" ) ;
142+ test ( "foo = true && bar()" , "foo = bar()" ) ;
142143}
143144
144145#[ test]
145146fn dce_var_hoisting ( ) {
146147 test (
147148 "function f() {
149+ KEEP();
148150 return () => {
149151 var x;
150152 }
151153 REMOVE;
152- function KEEP() {}
154+ function KEEP() { FOO }
153155 REMOVE;
154- }" ,
156+ } f() " ,
155157 "function f() {
156- return () => {
157- var x;
158- }
159- function KEEP() {}
160- }" ,
158+ KEEP();
159+ return () => { }
160+ function KEEP() { FOO }
161+ } f()" ,
161162 ) ;
162163 test (
163164 "function f() {
165+ KEEP();
164166 return function g() {
165167 var x;
166168 }
167169 REMOVE;
168170 function KEEP() {}
169171 REMOVE;
170- }" ,
172+ } f() " ,
171173 "function f() {
172- return function g() {
173- var x;
174- }
174+ KEEP();
175+ return function g() {}
175176 function KEEP() {}
176- }" ,
177+ } f() " ,
177178 ) ;
178179}
179180
180181#[ test]
181182fn pure_comment_for_pure_global_constructors ( ) {
182- test ( "var x = new WeakSet" , "var x = /* @__PURE__ */ new WeakSet();\n " ) ;
183- test ( "var x = new WeakSet(null)" , "var x = /* @__PURE__ */ new WeakSet(null);\n " ) ;
184- test ( "var x = new WeakSet(undefined)" , "var x = /* @__PURE__ */ new WeakSet(void 0);\n " ) ;
185- test ( "var x = new WeakSet([])" , "var x = /* @__PURE__ */ new WeakSet([]);\n " ) ;
183+ test ( "var x = new WeakSet; foo(x)" , "var x = /* @__PURE__ */ new WeakSet();\n foo(x)" ) ;
184+ test ( "var x = new WeakSet(null); foo(x)" , "var x = /* @__PURE__ */ new WeakSet(null);\n foo(x)" ) ;
185+ test (
186+ "var x = new WeakSet(undefined); foo(x)" ,
187+ "var x = /* @__PURE__ */ new WeakSet(void 0);\n foo(x)" ,
188+ ) ;
189+ test ( "var x = new WeakSet([]); foo(x)" , "var x = /* @__PURE__ */ new WeakSet([]);\n foo(x)" ) ;
186190}
187191
188192#[ test]
@@ -203,13 +207,12 @@ fn dce_from_terser() {
203207 if (x) {
204208 y();
205209 }
206- }" ,
210+ } f() " ,
207211 "function f() {
208212 a();
209213 b();
210214 x = 10;
211- return;
212- }" ,
215+ } f()" ,
213216 ) ;
214217
215218 test (
@@ -248,9 +251,6 @@ fn dce_from_terser() {
248251 }
249252 console.log(foo, bar, Baz);
250253 " ,
251- "
252- if (0) var qux;
253- console.log(foo, bar, Baz);
254- " ,
254+ "console.log(foo, bar, Baz);" ,
255255 ) ;
256256}
0 commit comments