diff --git a/.prettierignore b/.prettierignore index ac0836a104b..f82fa5a645d 100644 --- a/.prettierignore +++ b/.prettierignore @@ -55,3 +55,8 @@ node-swc/src node-swc/tests crates/swc/tests/srcmap/ crates/swc/tests/stacktrace/ + + +# git submodules +crates/swc_ecma_parser/tests/test262-parser +crates/swc_html_parser/tests/html5lib-tests \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/TODO.txt b/crates/swc_ecma_minifier/tests/TODO.txt index d2ce8713705..43153ecb48f 100644 --- a/crates/swc_ecma_minifier/tests/TODO.txt +++ b/crates/swc_ecma_minifier/tests/TODO.txt @@ -1,5 +1,4 @@ asm/asm_mixed/input.js -async/async_function_declaration/input.js classes/pure_prop_assignment_for_classes/input.js collapse_vars/collapse_vars_assignment/input.js collapse_vars/collapse_vars_constants/input.js @@ -20,14 +19,8 @@ collapse_vars/collapse_vars_unary/input.js collapse_vars/issue_1631_1/input.js collapse_vars/issue_1631_2/input.js collapse_vars/issue_1631_3/input.js -collapse_vars/issue_1858/input.js -collapse_vars/issue_2436_1/input.js collapse_vars/issue_2436_10/input.js collapse_vars/issue_2436_11/input.js -collapse_vars/issue_2436_4/input.js -collapse_vars/issue_2436_6/input.js -collapse_vars/issue_2436_7/input.js -collapse_vars/issue_2436_8/input.js collapse_vars/issue_2436_9/input.js collapse_vars/issue_2437/input.js collapse_vars/issue_2497/input.js @@ -45,60 +38,40 @@ conditionals/ifs_5/input.js conditionals/issue_2535_1/input.js dead_code/issue_2749/input.js destructuring/destructuring_dont_evaluate_with_undefined_as_default_assignment/input.js -drop_unused/issue_2163/input.js -drop_unused/issue_2418_2/input.js -drop_unused/issue_2418_3/input.js -drop_unused/issue_2418_4/input.js drop_unused/issue_2418_5/input.js drop_unused/issue_2768/input.js drop_unused/keep_assign/input.js drop_unused/reassign_const/input.js drop_unused/var_catch_toplevel/input.js drop_unused/variable_refs_outside_unused_class/input.js -evaluate/call_args_drop_param/input.js evaluate/issue_2535_1/input.js expansions/avoid_spread_getset_object/input.js expansions/avoid_spread_hole/input.js functions/avoid_generating_duplicate_functions_compared_together_3/input.js -functions/empty_body/input.js functions/inline_true/input.js functions/inner_ref/input.js functions/issue_2084/input.js functions/issue_2114_1/input.js functions/issue_2114_2/input.js functions/issue_2601_1/input.js -functions/issue_2601_2/input.js functions/issue_2604_1/input.js -functions/issue_2604_2/input.js functions/issue_2620_1/input.js -functions/issue_2620_2/input.js functions/issue_2620_3/input.js functions/issue_2620_4/input.js -functions/issue_2630_1/input.js -functions/issue_2630_4/input.js functions/issue_3016_3/input.js functions/issue_3018/input.js functions/issue_3076/input.js -functions/issue_3166/input.js functions/loop_init_arg/input.js -functions/unsafe_apply_2/input.js -functions/unsafe_call_2/input.js -global_defs/issue_3217/input.js harmony/array_literal_with_spread_4a/input.js -harmony/class_name_can_be_preserved_with_reserved/input.js harmony/classes_extending_classes_out_of_pure_iifes/input.js harmony/default_assign/input.js -harmony/fat_arrow_as_param/input.js -harmony/module_enables_strict_mode/input.js harmony/object_rest_spread/input.js hoist_props/issue_851_hoist_to_conflicting_name/input.js if_return/if_return_same_value/input.js if_return/if_var_return/input.js if_return/issue_2747/input.js -inline/do_not_repeat_when_variable_larger_than_inlined_node/input.js inline/dont_inline_funcs_into_default_param/input.js inline/dont_inline_funcs_into_default_param_2/input.js -inline/inline_annotation/input.js inline/inline_into_scope_conflict/input.js inline/inline_within_extends_1/input.js issue_1034/non_hoisted_function_after_return_2a/input.js @@ -123,12 +96,8 @@ issue_973/this_binding_collapse_vars/input.js issue_973/this_binding_conditionals/input.js issue_t120/issue_t120_3/input.js issue_t120/pr_152_regression/input.js -negate_iife/issue_1254_negate_iife_nested/input.js -negate_iife/issue_1288/input.js -nullish/conditional_to_nullish_coalescing/input.js nullish/conditional_to_nullish_coalescing_2/input.js numbers/evaluate_4/input.js -object/concise_methods_and_mangle_props/input.js object/getter_setter_mangler/input.js properties/dot_properties/input.js properties/issue_2208_8/input.js @@ -162,17 +131,13 @@ pure_getters/unsafe/input.js pure_getters/unsafe_reduce_vars/input.js reduce_vars/defun_inline_2/input.js reduce_vars/defun_reference/input.js -reduce_vars/defun_var_2/input.js reduce_vars/escaped_prop_1/input.js reduce_vars/escaped_prop_2/input.js -reduce_vars/issue_1595_3/input.js reduce_vars/issue_294/input.js reduce_vars/issue_308/input.js reduce_vars/issue_432_1/input.js reduce_vars/issue_432_2/input.js reduce_vars/issue_639/input.js -reduce_vars/obj_var_2/input.js -reduce_vars/unsafe_evaluate_object_2/input.js reduce_vars/variables_collision_in_immediately_invoked_func/input.js return_undefined/return_undefined/input.js sequences/delete_seq_4/input.js @@ -182,4 +147,3 @@ sequences/issue_1758/input.js sequences/lift_sequences_5/input.js template_string/special_chars_in_string/input.js transform/if_else_empty/input.js -transform/if_return/input.js diff --git a/crates/swc_ecma_minifier/tests/compress.rs b/crates/swc_ecma_minifier/tests/compress.rs index e1b0401bd65..72a9a0ab861 100644 --- a/crates/swc_ecma_minifier/tests/compress.rs +++ b/crates/swc_ecma_minifier/tests/compress.rs @@ -17,8 +17,8 @@ use anyhow::{bail, Context, Error}; use once_cell::sync::Lazy; use serde::Deserialize; use swc_common::{ - comments::SingleThreadedComments, errors::Handler, sync::Lrc, EqIgnoreSpan, FileName, Mark, - SourceMap, Spanned, + comments::SingleThreadedComments, errors::Handler, sync::Lrc, util::take::Take, EqIgnoreSpan, + FileName, Mark, SourceMap, Spanned, }; use swc_ecma_ast::*; use swc_ecma_codegen::{ @@ -38,7 +38,7 @@ use swc_ecma_parser::{ }; use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver}; use swc_ecma_utils::drop_span; -use swc_ecma_visit::{FoldWith, Visit, VisitMutWith, VisitWith}; +use swc_ecma_visit::{FoldWith, Visit, VisitMut, VisitMutWith, VisitWith}; use testing::{assert_eq, DebugUsingDisplay, NormalizedOutput}; fn load_txt(filename: &str) -> Vec { @@ -440,16 +440,28 @@ fn fixture(input: PathBuf) { })?; let mut expected = expected.fold_with(&mut fixer(None)); expected = drop_span(expected); + expected + .body + .retain(|s| !matches!(s, ModuleItem::Stmt(Stmt::Empty(..)))); - if output_module.eq_ignore_span(&expected) - || drop_span(output_module.clone()) == expected + let mut normalized_expected = expected.clone(); + normalized_expected.visit_mut_with(&mut DropParens); + + let mut actual = output_module.clone(); + actual.visit_mut_with(&mut DropParens); + + if actual.eq_ignore_span(&normalized_expected) + || drop_span(actual.clone()) == normalized_expected + { + return Ok(()); + } + + if print(cm.clone(), &[actual], false, false) + == print(cm.clone(), &[normalized_expected], false, false) { return Ok(()); } - expected - .body - .retain(|s| !matches!(s, ModuleItem::Stmt(Stmt::Empty(..)))); print(cm.clone(), &[expected], false, false) }; @@ -1595,3 +1607,15 @@ fn full(input: PathBuf) { }) .unwrap() } + +struct DropParens; + +impl VisitMut for DropParens { + fn visit_mut_expr(&mut self, e: &mut Expr) { + e.visit_mut_children_with(self); + + if let Expr::Paren(p) = e { + *e = *p.expr.take(); + } + } +} diff --git a/crates/swc_ecma_minifier/tests/golden.txt b/crates/swc_ecma_minifier/tests/golden.txt index 636f4671042..c1cd1158d1e 100644 --- a/crates/swc_ecma_minifier/tests/golden.txt +++ b/crates/swc_ecma_minifier/tests/golden.txt @@ -674,6 +674,7 @@ if_return/if_if_return_return/input.js if_return/if_return_1/input.js if_return/if_return_2/input.js if_return/if_return_3/input.js +if_return/if_return_4/input.js if_return/if_return_5/input.js if_return/if_return_6/input.js if_return/if_return_7/input.js @@ -1365,6 +1366,7 @@ template_string/template_with_newline/input.js transform/booleans_evaluate/input.js transform/booleans_global_defs/input.js transform/condition_evaluate/input.js +transform/if_return/input.js transform/while_if_break/input.js try_catch/broken_safari_catch_scope/input.js try_catch/broken_safari_catch_scope_caveat/input.js diff --git a/crates/swc_ecma_minifier/tests/postponed.txt b/crates/swc_ecma_minifier/tests/postponed.txt index a177c7d2380..433ad60d8cb 100644 --- a/crates/swc_ecma_minifier/tests/postponed.txt +++ b/crates/swc_ecma_minifier/tests/postponed.txt @@ -19,6 +19,7 @@ arrow/issue_2136_3/input.js arrow/issue_27/input.js ascii/ascii_only_false/input.js ascii/ascii_only_true/input.js +async/async_function_declaration/input.js async/async_inline/input.js block_scope/issue_334/input.js class_properties/class_expression_constant/input.js @@ -42,6 +43,7 @@ collapse_vars/double_def_1/input.js collapse_vars/double_def_2/input.js collapse_vars/inner_lvalues/input.js collapse_vars/issue_1605_2/input.js +collapse_vars/issue_1858/input.js collapse_vars/issue_2187_2/input.js collapse_vars/issue_2203_2/input.js collapse_vars/issue_2203_4/input.js @@ -50,8 +52,13 @@ collapse_vars/issue_2250_2/input.js collapse_vars/issue_2298/input.js collapse_vars/issue_2319_1/input.js collapse_vars/issue_2319_3/input.js +collapse_vars/issue_2436_1/input.js collapse_vars/issue_2436_13/input.js collapse_vars/issue_2436_14/input.js +collapse_vars/issue_2436_4/input.js +collapse_vars/issue_2436_6/input.js +collapse_vars/issue_2436_7/input.js +collapse_vars/issue_2436_8/input.js collapse_vars/issue_2453/input.js collapse_vars/issue_2506/input.js collapse_vars/issue_2914_2/input.js @@ -122,10 +129,14 @@ drop_unused/issue_2105_1/input.js drop_unused/issue_2105_2/input.js drop_unused/issue_2136_2/input.js drop_unused/issue_2136_3/input.js +drop_unused/issue_2163/input.js drop_unused/issue_2226_2/input.js drop_unused/issue_2226_3/input.js drop_unused/issue_2288/input.js drop_unused/issue_2418_1/input.js +drop_unused/issue_2418_2/input.js +drop_unused/issue_2418_3/input.js +drop_unused/issue_2418_4/input.js drop_unused/issue_2516_1/input.js drop_unused/issue_2516_2/input.js drop_unused/issue_2660_1/input.js @@ -140,6 +151,7 @@ drop_unused/unused_circular_references_3/input.js drop_unused/unused_funarg_1/input.js drop_unused/unused_funarg_2/input.js drop_unused/unused_null_conditional_chain/input.js +evaluate/call_args_drop_param/input.js evaluate/issue_2207_3/input.js evaluate/issue_399/input.js evaluate/prototype_function/input.js @@ -174,6 +186,7 @@ export/name_cache_mangle_export_default_class/input.js export/name_cache_mangle_export_default_function/input.js export/name_cache_mangle_local_import_and_export_aliases/input.js functions/duplicate_arg_var/input.js +functions/empty_body/input.js functions/hoist_funs/input.js functions/hoist_funs_strict/input.js functions/inline_1/input.js @@ -188,9 +201,14 @@ functions/issue_2476/input.js functions/issue_2531_1/input.js functions/issue_2531_2/input.js functions/issue_2531_3/input.js +functions/issue_2601_2/input.js +functions/issue_2604_2/input.js functions/issue_2616/input.js +functions/issue_2620_2/input.js +functions/issue_2630_1/input.js functions/issue_2630_2/input.js functions/issue_2630_3/input.js +functions/issue_2630_4/input.js functions/issue_2630_5/input.js functions/issue_2657/input.js functions/issue_2663_2/input.js @@ -201,22 +219,28 @@ functions/issue_3016_2/input.js functions/issue_3016_2_ie8/input.js functions/issue_3016_3_ie8/input.js functions/issue_3125/input.js +functions/issue_3166/input.js functions/issue_t131a/input.js functions/issue_t131b/input.js functions/recursive_inline_2/input.js functions/unsafe_apply_1/input.js +functions/unsafe_apply_2/input.js functions/unsafe_apply_expansion_1/input.js functions/unsafe_apply_expansion_2/input.js functions/unsafe_call_1/input.js +functions/unsafe_call_2/input.js functions/unsafe_call_3/input.js functions/unsafe_call_expansion_1/input.js functions/unsafe_call_expansion_2/input.js functions/use_before_init_in_loop/input.js +global_defs/issue_3217/input.js harmony/array_literal_with_spread_2b/input.js harmony/array_literal_with_spread_4b/input.js harmony/array_spread_of_sequence/input.js harmony/class_name_can_be_mangled/input.js +harmony/class_name_can_be_preserved_with_reserved/input.js harmony/expansion/input.js +harmony/fat_arrow_as_param/input.js harmony/import_statement_mangling/input.js harmony/inline_arrow_using_arguments/input.js harmony/issue_1613/input.js @@ -231,6 +255,7 @@ harmony/issue_2794_2/input.js harmony/issue_2874_1/input.js harmony/issue_2874_2/input.js harmony/module_enabled/input.js +harmony/module_enables_strict_mode/input.js harmony/module_mangle_scope/input.js harmony/object_spread_of_sequence/input.js harmony/object_spread_unsafe/input.js @@ -266,8 +291,9 @@ ie8/issue_2120_1/input.js ie8/issue_2254_1/input.js ie8/issue_2254_2/input.js ie8/reduce_vars/input.js -if_return/if_return_4/input.js if_return/issue_512/input.js +inline/do_not_repeat_when_variable_larger_than_inlined_node/input.js +inline/inline_annotation/input.js inline/inline_within_extends_2/input.js issue_1034/non_hoisted_function_after_return/input.js issue_1034/non_hoisted_function_after_return_2a_strict/input.js @@ -379,11 +405,15 @@ loops/issue_2740_5/input.js loops/issue_2740_6/input.js loops/issue_2740_7/input.js loops/issue_2740_8/input.js +negate_iife/issue_1254_negate_iife_nested/input.js negate_iife/issue_1254_negate_iife_true/input.js +negate_iife/issue_1288/input.js +nullish/conditional_to_nullish_coalescing/input.js numbers/evaluate_1/input.js numbers/evaluate_2/input.js numbers/evaluate_3/input.js numbers/hex_numbers_in_parentheses_for_prototype_functions/input.js +object/concise_methods_and_mangle_props/input.js object/dont_join_repeat_object_keys/input.js object/prop_arrow_with_nested_this/input.js object/prop_arrow_with_this/input.js @@ -478,6 +508,7 @@ reduce_vars/defun_inline_3/input.js reduce_vars/defun_label/input.js reduce_vars/defun_redefine/input.js reduce_vars/defun_var_1/input.js +reduce_vars/defun_var_2/input.js reduce_vars/delay_def/input.js reduce_vars/duplicate_lambda_defun_name_1/input.js reduce_vars/escape_expansion/input.js @@ -489,6 +520,7 @@ reduce_vars/iife/input.js reduce_vars/iife_new/input.js reduce_vars/inner_var_for_2/input.js reduce_vars/inverted_var/input.js +reduce_vars/issue_1595_3/input.js reduce_vars/issue_1670_2/input.js reduce_vars/issue_1670_4/input.js reduce_vars/issue_1670_5/input.js @@ -512,6 +544,7 @@ reduce_vars/modified/input.js reduce_vars/obj_arg_1/input.js reduce_vars/obj_arg_2/input.js reduce_vars/obj_for_1/input.js +reduce_vars/obj_var_2/input.js reduce_vars/passes/input.js reduce_vars/perf_1/input.js reduce_vars/perf_3/input.js @@ -530,6 +563,7 @@ reduce_vars/unsafe_evaluate/input.js reduce_vars/unsafe_evaluate_array_2/input.js reduce_vars/unsafe_evaluate_array_4/input.js reduce_vars/unsafe_evaluate_modified/input.js +reduce_vars/unsafe_evaluate_object_2/input.js reduce_vars/unsafe_evaluate_side_effect_free_1/input.js reduce_vars/unsafe_evaluate_side_effect_free_2/input.js reduce_vars/unused_modified/input.js