diff --git a/crates/oxc_minifier/src/peephole/normalize.rs b/crates/oxc_minifier/src/peephole/normalize.rs index 25a4f3ba46ce0..c82497acb33ac 100644 --- a/crates/oxc_minifier/src/peephole/normalize.rs +++ b/crates/oxc_minifier/src/peephole/normalize.rs @@ -467,7 +467,8 @@ mod test { #[test] fn drop_debugger() { - test("debugger", ""); + let options = CompressOptions { drop_debugger: true, ..default_options() }; + test_options("debugger", "", &options); } #[test] diff --git a/crates/oxc_minifier/src/tester.rs b/crates/oxc_minifier/src/tester.rs index 2123ebbc93bec..414190c10d1e5 100644 --- a/crates/oxc_minifier/src/tester.rs +++ b/crates/oxc_minifier/src/tester.rs @@ -1,3 +1,4 @@ +#![expect(clippy::allow_attributes)] use oxc_allocator::Allocator; use oxc_codegen::{Codegen, CodegenOptions}; use oxc_parser::{ParseOptions, Parser}; @@ -6,19 +7,26 @@ use oxc_span::SourceType; use crate::{CompressOptions, CompressOptionsUnused, Compressor}; pub fn default_options() -> CompressOptions { - CompressOptions { unused: CompressOptionsUnused::Keep, ..CompressOptions::smallest() } + CompressOptions { + drop_debugger: false, + unused: CompressOptionsUnused::Keep, + ..CompressOptions::smallest() + } } +#[allow(dead_code)] #[track_caller] pub fn test_same(source_text: &str) { test(source_text, source_text); } +#[allow(dead_code)] #[track_caller] pub fn test_same_options(source_text: &str, options: &CompressOptions) { test_options(source_text, source_text, options); } +#[allow(dead_code)] #[track_caller] pub fn test_same_options_source_type( source_text: &str, @@ -46,9 +54,28 @@ pub fn test_options_source_type( source_type: SourceType, options: &CompressOptions, ) { - let result = run(source_text, source_type, Some(options.clone())); + test_options_source_type_with_idempotency(source_text, expected, source_type, options, false); +} + +#[track_caller] +pub fn test_options_source_type_with_idempotency( + source_text: &str, + expected: &str, + source_type: SourceType, + options: &CompressOptions, + idempotency: bool, +) { + let first = run(source_text, source_type, Some(options.clone())); let expected = run(expected, source_type, None); - assert_eq!(result, expected, "\nfor source\n{source_text}\nexpect\n{expected}\ngot\n{result}"); + assert_eq!(first, expected, "\nfor source\n{source_text}\nexpect\n{expected}\ngot\n{first}"); + + if idempotency { + let second = run(&first, source_type, Some(options.clone())); + assert_eq!( + first, second, + "\nidempotency for source\n{source_text}\ngot\n{first}\nthen\n{second}" + ); + } } #[track_caller] diff --git a/crates/oxc_minifier/tests/mod.rs b/crates/oxc_minifier/tests/mod.rs index af42a37424054..f54e1a2f4e156 100644 --- a/crates/oxc_minifier/tests/mod.rs +++ b/crates/oxc_minifier/tests/mod.rs @@ -1,50 +1,42 @@ #![allow(clippy::literal_string_with_formatting_args)] +#[path = "../src/tester.rs"] +mod tester; + mod ecmascript; mod mangler; mod peephole; -use oxc_allocator::Allocator; -use oxc_codegen::{Codegen, CodegenOptions}; -use oxc_minifier::{CompressOptions, Compressor}; -use oxc_parser::{ParseOptions, Parser}; +use oxc_minifier::{CompressOptions, CompressOptionsUnused, Compressor}; use oxc_span::SourceType; #[track_caller] -pub(crate) fn test(source_text: &str, expected: &str, options: CompressOptions) { - let source_type = SourceType::default(); - let first = run(source_text, source_type, Some(options.clone())); +pub(crate) fn test_same(source_text: &str) { + test(source_text, source_text); +} - let expected = run(expected, source_type, None); - assert_eq!(first, expected, "\nfor source\n{source_text}\nexpect\n{expected}\ngot\n{first}"); +#[track_caller] +pub(crate) fn test(source_text: &str, expected: &str) { + test_options(source_text, expected, &tester::default_options()); +} - let second = run(&first, source_type, Some(options)); - assert_eq!( - first, second, - "\nidempotency for source\n{source_text}\ngot\n{first}\nthen\n{second}" - ); +#[track_caller] +pub(crate) fn test_options(source_text: &str, expected: &str, options: &CompressOptions) { + test_options_source_type(source_text, expected, SourceType::mjs(), options); } -pub(crate) fn run( +#[track_caller] +pub(crate) fn test_options_source_type( source_text: &str, + expected: &str, source_type: SourceType, - options: Option, -) -> String { - let allocator = Allocator::default(); - let ret = Parser::new(&allocator, source_text, source_type) - .with_options(ParseOptions { - allow_return_outside_function: true, - ..ParseOptions::default() - }) - .parse(); - assert!(!ret.panicked, "{source_text}"); - assert!(ret.errors.is_empty(), "{source_text}"); - let mut program = ret.program; - if let Some(options) = options { - Compressor::new(&allocator).build(&mut program, options); - } - Codegen::new() - .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) - .build(&program) - .code + options: &CompressOptions, +) { + tester::test_options_source_type_with_idempotency( + source_text, + expected, + source_type, + options, + true, + ); } diff --git a/crates/oxc_minifier/tests/peephole/collapse_variable_declarations.rs b/crates/oxc_minifier/tests/peephole/collapse_variable_declarations.rs index d76f80ce0b4a7..9c867a862a2fe 100644 --- a/crates/oxc_minifier/tests/peephole/collapse_variable_declarations.rs +++ b/crates/oxc_minifier/tests/peephole/collapse_variable_declarations.rs @@ -1,8 +1,6 @@ /// -use super::{test, test_same}; - mod join_vars { - use super::{test, test_same}; + use crate::{test, test_same}; #[test] fn test_collapsing() { @@ -148,7 +146,7 @@ mod join_vars { /// #[cfg(test)] mod collapse_for { - use super::{test, test_same}; + use crate::{test, test_same}; #[test] fn test_for() { diff --git a/crates/oxc_minifier/tests/peephole/esbuild.rs b/crates/oxc_minifier/tests/peephole/esbuild.rs index 5ce72554743d3..d907f9dec5410 100644 --- a/crates/oxc_minifier/tests/peephole/esbuild.rs +++ b/crates/oxc_minifier/tests/peephole/esbuild.rs @@ -1,4 +1,4 @@ -use super::test; +use crate::test; /// Esbuild minfication tests /// diff --git a/crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs b/crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs index aaae14453d9f9..0bbfdc8e07d83 100644 --- a/crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs +++ b/crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs @@ -1,4 +1,4 @@ -use super::{test, test_same}; +use crate::{test, test_same}; #[test] fn test_inline_single_use_variable() { diff --git a/crates/oxc_minifier/tests/peephole/merge_assignments_to_declarations.rs b/crates/oxc_minifier/tests/peephole/merge_assignments_to_declarations.rs index e93d1bdd79bc5..57e2a0f5513d6 100644 --- a/crates/oxc_minifier/tests/peephole/merge_assignments_to_declarations.rs +++ b/crates/oxc_minifier/tests/peephole/merge_assignments_to_declarations.rs @@ -1,4 +1,4 @@ -use super::{test, test_same}; +use crate::{test, test_same}; #[test] fn merge_assignments_to_declarations_var() { diff --git a/crates/oxc_minifier/tests/peephole/minimize_exit_points.rs b/crates/oxc_minifier/tests/peephole/minimize_exit_points.rs index 64a1651793fe1..4c538b6b06512 100644 --- a/crates/oxc_minifier/tests/peephole/minimize_exit_points.rs +++ b/crates/oxc_minifier/tests/peephole/minimize_exit_points.rs @@ -1,4 +1,4 @@ -use super::{test, test_same}; +use crate::{test, test_same}; #[test] #[ignore] diff --git a/crates/oxc_minifier/tests/peephole/mod.rs b/crates/oxc_minifier/tests/peephole/mod.rs index 344ae16c4cb15..43de31ddf2e50 100644 --- a/crates/oxc_minifier/tests/peephole/mod.rs +++ b/crates/oxc_minifier/tests/peephole/mod.rs @@ -8,23 +8,3 @@ mod obscure_edge_cases; mod oxc; mod real_world_patterns; mod statement_fusion; - -use oxc_minifier::{CompressOptions, CompressOptionsUnused}; - -pub fn default_options() -> CompressOptions { - CompressOptions { - drop_debugger: false, - unused: CompressOptionsUnused::Keep, - ..CompressOptions::smallest() - } -} - -#[track_caller] -fn test(source_text: &str, expected: &str) { - crate::test(source_text, expected, default_options()); -} - -#[track_caller] -fn test_same(source_text: &str) { - test(source_text, source_text); -} diff --git a/crates/oxc_minifier/tests/peephole/obscure_edge_cases.rs b/crates/oxc_minifier/tests/peephole/obscure_edge_cases.rs index 805d9e12d46b6..e194f825295aa 100644 --- a/crates/oxc_minifier/tests/peephole/obscure_edge_cases.rs +++ b/crates/oxc_minifier/tests/peephole/obscure_edge_cases.rs @@ -1,4 +1,4 @@ -use super::{test, test_same}; +use crate::{test, test_same}; /// Tests for edge cases that should reveal minification problems /// Focus on cases where optimizations might be unsafe or incorrect diff --git a/crates/oxc_minifier/tests/peephole/oxc.rs b/crates/oxc_minifier/tests/peephole/oxc.rs index 793aee7376fba..491cedc084c7d 100644 --- a/crates/oxc_minifier/tests/peephole/oxc.rs +++ b/crates/oxc_minifier/tests/peephole/oxc.rs @@ -1,11 +1,11 @@ use oxc_minifier::CompressOptions; -use super::super::test as test_options; /// Oxc Integration Tests -use super::{test, test_same}; +use crate::{test, test_options, test_same}; +#[track_caller] fn test_unused(source_text: &str, expected: &str) { - test_options(source_text, expected, CompressOptions::default()); + test_options(source_text, expected, &CompressOptions::default()); } #[test] diff --git a/crates/oxc_minifier/tests/peephole/real_world_patterns.rs b/crates/oxc_minifier/tests/peephole/real_world_patterns.rs index 6133daa7ec2df..307a81596d3cb 100644 --- a/crates/oxc_minifier/tests/peephole/real_world_patterns.rs +++ b/crates/oxc_minifier/tests/peephole/real_world_patterns.rs @@ -1,4 +1,4 @@ -use super::{test, test_same}; +use crate::{test, test_same}; /// Focused minification tests for specific patterns and edge cases /// Tests demonstrate actual oxc minifier capabilities diff --git a/crates/oxc_minifier/tests/peephole/statement_fusion.rs b/crates/oxc_minifier/tests/peephole/statement_fusion.rs index 212a10ccb248c..d0be0cff329ef 100644 --- a/crates/oxc_minifier/tests/peephole/statement_fusion.rs +++ b/crates/oxc_minifier/tests/peephole/statement_fusion.rs @@ -3,7 +3,7 @@ /// Tries to fuse all the statements in a block into a one statement by using COMMAs or statements. /// /// -use super::{test, test_same}; +use crate::{test, test_same}; #[test] fn fold_block_with_statements() {