Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/oxc_minifier/src/peephole/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
33 changes: 30 additions & 3 deletions crates/oxc_minifier/src/tester.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![expect(clippy::allow_attributes)]
use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_parser::{ParseOptions, Parser};
Expand All @@ -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,
Expand Down Expand Up @@ -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]
Expand Down
60 changes: 26 additions & 34 deletions crates/oxc_minifier/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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<CompressOptions>,
) -> 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,
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/CollapseVariableDeclarationsTest.java>
use super::{test, test_same};

mod join_vars {
use super::{test, test_same};
use crate::{test, test_same};

#[test]
fn test_collapsing() {
Expand Down Expand Up @@ -148,7 +146,7 @@ mod join_vars {
/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/DenormalizeTest.java>
#[cfg(test)]
mod collapse_for {
use super::{test, test_same};
use crate::{test, test_same};

#[test]
fn test_for() {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/peephole/esbuild.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::test;
use crate::test;

/// Esbuild minfication tests
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{test, test_same};
use crate::{test, test_same};

#[test]
fn test_inline_single_use_variable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{test, test_same};
use crate::{test, test_same};

#[test]
fn merge_assignments_to_declarations_var() {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/peephole/minimize_exit_points.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{test, test_same};
use crate::{test, test_same};

#[test]
#[ignore]
Expand Down
20 changes: 0 additions & 20 deletions crates/oxc_minifier/tests/peephole/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/peephole/obscure_edge_cases.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_minifier/tests/peephole/oxc.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/peephole/real_world_patterns.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/peephole/statement_fusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// Tries to fuse all the statements in a block into a one statement by using COMMAs or statements.
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/StatementFusion.java>
use super::{test, test_same};
use crate::{test, test_same};

#[test]
fn fold_block_with_statements() {
Expand Down
Loading