From 6fba69be98b6bf7426b6c58cdd82c486a2e51746 Mon Sep 17 00:00:00 2001 From: shulaoda Date: Fri, 4 Oct 2024 23:43:55 +0800 Subject: [PATCH] fix(minifier): correct the reference link --- .../collapse_variable_declarations.rs | 3 +- .../src/ast_passes/exploit_assigns.rs | 1 + .../src/ast_passes/peephole_fold_constants.rs | 2 +- .../ast_passes/peephole_remove_dead_code.rs | 48 +++++++++++++++++++ .../peephole_replace_known_methods.rs | 2 +- .../peephole_substitute_alternate_syntax.rs | 3 +- 6 files changed, 55 insertions(+), 4 deletions(-) diff --git a/crates/oxc_minifier/src/ast_passes/collapse_variable_declarations.rs b/crates/oxc_minifier/src/ast_passes/collapse_variable_declarations.rs index d16a187660971..c424fd6905e2d 100644 --- a/crates/oxc_minifier/src/ast_passes/collapse_variable_declarations.rs +++ b/crates/oxc_minifier/src/ast_passes/collapse_variable_declarations.rs @@ -7,6 +7,7 @@ use crate::{CompressOptions, CompressorPass}; /// Collapse variable declarations. /// /// `var a; var b = 1; var c = 2` => `var a, b = 1; c = 2` +/// pub struct CollapseVariableDeclarations { options: CompressOptions, @@ -99,7 +100,7 @@ impl<'a> CollapseVariableDeclarations { } } -/// +/// #[cfg(test)] mod test { use oxc_allocator::Allocator; diff --git a/crates/oxc_minifier/src/ast_passes/exploit_assigns.rs b/crates/oxc_minifier/src/ast_passes/exploit_assigns.rs index 524b2b487ab4c..84b5783b13b75 100644 --- a/crates/oxc_minifier/src/ast_passes/exploit_assigns.rs +++ b/crates/oxc_minifier/src/ast_passes/exploit_assigns.rs @@ -29,6 +29,7 @@ impl ExploitAssigns { } } +/// #[cfg(test)] mod test { use oxc_allocator::Allocator; diff --git a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs index e9f846b1adc02..33b467c82ebfe 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs @@ -867,7 +867,7 @@ impl<'a> PeepholeFoldConstants { } } -/// +/// #[cfg(test)] mod test { use oxc_allocator::Allocator; diff --git a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs index 65f225e30cb78..0190bcae65705 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs @@ -10,6 +10,7 @@ use crate::{keep_var::KeepVar, node_util::NodeUtil, tri::Tri, CompressorPass}; /// Terser option: `dead_code: true`. /// /// See `KeepVar` at the end of this file for `var` hoisting logic. +/// pub struct PeepholeRemoveDeadCode { changed: bool, } @@ -183,3 +184,50 @@ impl<'a> PeepholeRemoveDeadCode { } } } + +/// +#[cfg(test)] +mod test { + use oxc_allocator::Allocator; + + use crate::tester; + + fn test(source_text: &str, positive: &str) { + let allocator = Allocator::default(); + let mut pass = super::PeepholeRemoveDeadCode::new(); + tester::test(&allocator, source_text, positive, &mut pass); + } + + fn test_same(source_text: &str) { + test(source_text, source_text); + } + + fn fold_same(js: &str) { + test_same(js); + } + + fn fold(js: &str, expected: &str) { + test(js, expected); + } + + #[test] + #[ignore] + fn test_remove_no_op_labelled_statement() { + fold("a: break a;", ""); + fold("a: { break a; }", ""); + + fold( + // + "a: { break a; console.log('unreachable'); }", // + "", + ); + fold( + // + "a: { break a; var x = 1; } x = 2;", // + "var x; x = 2;", + ); + + fold_same("b: { var x = 1; } x = 2;"); + fold_same("a: b: { var x = 1; } x = 2;"); + } +} diff --git a/crates/oxc_minifier/src/ast_passes/peephole_replace_known_methods.rs b/crates/oxc_minifier/src/ast_passes/peephole_replace_known_methods.rs index c5303eb7ae0d5..ee53e9484a07f 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_replace_known_methods.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_replace_known_methods.rs @@ -4,7 +4,7 @@ use oxc_traverse::{Traverse, TraverseCtx}; use crate::CompressorPass; /// Minimize With Known Methods -/// +/// pub struct PeepholeReplaceKnownMethods { changed: bool, } diff --git a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs index f3204e25a699c..895b5d0a8a4c3 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs @@ -12,6 +12,7 @@ use crate::{node_util::NodeUtil, CompressOptions, CompressorPass}; /// A peephole optimization that minimizes code by simplifying conditional /// expressions, replacing IFs with HOOKs, replacing object constructors /// with literals, and simplifying returns. +/// pub struct PeepholeSubstituteAlternateSyntax { options: CompressOptions, in_define_export: bool, @@ -329,7 +330,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax { } } -/// +/// #[cfg(test)] mod test { use oxc_allocator::Allocator;