@@ -172,13 +172,81 @@ impl<'a> Traverse<'a, MinifierState<'a>> for PeepholeOptimizations {
172172 }
173173
174174 fn exit_expression ( & mut self , expr : & mut Expression < ' a > , ctx : & mut TraverseCtx < ' a > ) {
175- let mut ctx = Ctx :: new ( ctx) ;
176- self . fold_constants_exit_expression ( expr, & mut ctx) ;
177- self . minimize_conditions_exit_expression ( expr, & mut ctx) ;
178- self . remove_dead_code_exit_expression ( expr, & mut ctx) ;
179- self . replace_known_methods_exit_expression ( expr, & mut ctx) ;
180- self . substitute_exit_expression ( expr, & mut ctx) ;
181- self . inline_identifier_reference ( expr, & mut ctx) ;
175+ let ctx = & mut Ctx :: new ( ctx) ;
176+ match expr {
177+ Expression :: TemplateLiteral ( t) => {
178+ self . inline_template_literal ( t, ctx) ;
179+ Self :: try_fold_template_literal ( expr, ctx) ;
180+ }
181+ Expression :: ObjectExpression ( e) => self . fold_object_exp ( e, ctx) ,
182+ Expression :: BinaryExpression ( e) => {
183+ Self :: swap_binary_expressions ( e) ;
184+ Self :: fold_binary_expr ( expr, ctx) ;
185+ Self :: fold_binary_typeof_comparison ( expr, ctx) ;
186+ Self :: try_compress_is_loose_boolean ( expr, ctx) ;
187+ Self :: try_minimize_binary ( expr, ctx) ;
188+ Self :: try_fold_loose_equals_undefined ( expr, ctx) ;
189+ Self :: try_compress_typeof_undefined ( expr, ctx) ;
190+ }
191+ Expression :: UnaryExpression ( _) => {
192+ Self :: fold_unary_expr ( expr, ctx) ;
193+ self . try_minimize_not ( expr, ctx) ;
194+ Self :: try_remove_unary_plus ( expr, ctx) ;
195+ }
196+ Expression :: StaticMemberExpression ( _) => {
197+ Self :: fold_static_member_expr ( expr, ctx) ;
198+ self . try_fold_known_property_access ( expr, ctx) ;
199+ }
200+ Expression :: ComputedMemberExpression ( _) => {
201+ Self :: fold_computed_member_expr ( expr, ctx) ;
202+ self . try_fold_known_property_access ( expr, ctx) ;
203+ }
204+ Expression :: LogicalExpression ( _) => {
205+ Self :: fold_logical_expr ( expr, ctx) ;
206+ self . minimize_logical_expression ( expr, ctx) ;
207+ Self :: try_compress_is_object_and_not_null ( expr, ctx) ;
208+ Self :: try_rotate_logical_expression ( expr, ctx) ;
209+ }
210+ Expression :: ChainExpression ( _) => {
211+ Self :: fold_chain_expr ( expr, ctx) ;
212+ Self :: try_compress_chain_call_expression ( expr, ctx) ;
213+ }
214+ Expression :: CallExpression ( _) => {
215+ Self :: fold_call_expression ( expr, ctx) ;
216+ self . remove_dead_code_call_expression ( expr, ctx) ;
217+ self . try_fold_concat_chain ( expr, ctx) ;
218+ self . try_fold_known_global_methods ( expr, ctx) ;
219+ self . try_fold_simple_function_call ( expr, ctx) ;
220+ Self :: try_fold_object_or_array_constructor ( expr, ctx) ;
221+ }
222+ Expression :: ConditionalExpression ( logical_expr) => {
223+ self . try_fold_expr_in_boolean_context ( & mut logical_expr. test , ctx) ;
224+ if let Some ( changed) = self . try_minimize_conditional ( logical_expr, ctx) {
225+ * expr = changed;
226+ ctx. state . changed = true ;
227+ }
228+ self . try_fold_conditional_expression ( expr, ctx) ;
229+ }
230+ Expression :: AssignmentExpression ( e) => {
231+ self . try_compress_normal_assignment_to_combined_logical_assignment ( e, ctx) ;
232+ Self :: try_compress_normal_assignment_to_combined_assignment ( e, ctx) ;
233+ Self :: try_compress_assignment_to_update_expression ( expr, ctx) ;
234+ self . remove_unused_assignment_expression ( expr, ctx) ;
235+ }
236+ Expression :: SequenceExpression ( _) => self . try_fold_sequence_expression ( expr, ctx) ,
237+ Expression :: ArrowFunctionExpression ( e) => self . try_compress_arrow_expression ( e, ctx) ,
238+ Expression :: FunctionExpression ( e) => Self :: try_remove_name_from_functions ( e, ctx) ,
239+ Expression :: ClassExpression ( e) => Self :: try_remove_name_from_classes ( e, ctx) ,
240+ Expression :: NewExpression ( e) => {
241+ Self :: try_compress_typed_array_constructor ( e, ctx) ;
242+ Self :: try_fold_new_expression ( expr, ctx) ;
243+ Self :: try_fold_object_or_array_constructor ( expr, ctx) ;
244+ }
245+ Expression :: BooleanLiteral ( _) => Self :: try_compress_boolean ( expr, ctx) ,
246+ Expression :: ArrayExpression ( _) => Self :: try_compress_array_expression ( expr, ctx) ,
247+ Expression :: Identifier ( _) => self . inline_identifier_reference ( expr, ctx) ,
248+ _ => { }
249+ }
182250 }
183251
184252 fn exit_unary_expression ( & mut self , expr : & mut UnaryExpression < ' a > , ctx : & mut TraverseCtx < ' a > ) {
@@ -328,10 +396,37 @@ impl<'a> Traverse<'a, MinifierState<'a>> for DeadCodeElimination {
328396 stmts. retain ( |stmt| !matches ! ( stmt, Statement :: EmptyStatement ( _) ) ) ;
329397 }
330398
331- fn exit_expression ( & mut self , expr : & mut Expression < ' a > , ctx : & mut TraverseCtx < ' a > ) {
332- let mut ctx = Ctx :: new ( ctx) ;
333- self . inner . fold_constants_exit_expression ( expr, & mut ctx) ;
334- self . inner . remove_dead_code_exit_expression ( expr, & mut ctx) ;
399+ fn exit_expression ( & mut self , e : & mut Expression < ' a > , ctx : & mut TraverseCtx < ' a > ) {
400+ let ctx = & mut Ctx :: new ( ctx) ;
401+ match e {
402+ Expression :: TemplateLiteral ( t) => self . inner . inline_template_literal ( t, ctx) ,
403+ Expression :: ObjectExpression ( e) => self . inner . fold_object_exp ( e, ctx) ,
404+ Expression :: BinaryExpression ( _) => {
405+ PeepholeOptimizations :: fold_binary_expr ( e, ctx) ;
406+ PeepholeOptimizations :: fold_binary_typeof_comparison ( e, ctx) ;
407+ }
408+ Expression :: UnaryExpression ( _) => PeepholeOptimizations :: fold_unary_expr ( e, ctx) ,
409+ Expression :: StaticMemberExpression ( _) => {
410+ PeepholeOptimizations :: fold_static_member_expr ( e, ctx) ;
411+ }
412+ Expression :: ComputedMemberExpression ( _) => {
413+ PeepholeOptimizations :: fold_computed_member_expr ( e, ctx) ;
414+ }
415+ Expression :: LogicalExpression ( _) => PeepholeOptimizations :: fold_logical_expr ( e, ctx) ,
416+ Expression :: ChainExpression ( _) => PeepholeOptimizations :: fold_chain_expr ( e, ctx) ,
417+ Expression :: CallExpression ( _) => {
418+ PeepholeOptimizations :: fold_call_expression ( e, ctx) ;
419+ self . inner . remove_dead_code_call_expression ( e, ctx) ;
420+ }
421+ Expression :: ConditionalExpression ( _) => {
422+ self . inner . try_fold_conditional_expression ( e, ctx) ;
423+ }
424+ Expression :: SequenceExpression ( _) => self . inner . try_fold_sequence_expression ( e, ctx) ,
425+ Expression :: AssignmentExpression ( _) => {
426+ self . inner . remove_unused_assignment_expression ( e, ctx) ;
427+ }
428+ _ => { }
429+ }
335430 }
336431}
337432
0 commit comments