From d953a6be023b2547fa01ec09213b98d9e247fec7 Mon Sep 17 00:00:00 2001 From: dalaoshu Date: Sat, 5 Oct 2024 08:54:59 +0800 Subject: [PATCH] fix(minifier): correct the reference link (#6283) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These are minor changes, and recently I’ve been learning and trying to implement some features of `oxc_minifier`, which feels a bit complex. By the way, I’m wondering whether we should gradually add test cases during the feature implementation process or just copy all the corresponding tests directly? Perhaps we could implement something like `rulegen` similar to `linter`? --- .../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 d16a18766..c424fd690 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 524b2b487..84b5783b1 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 e9f846b1a..33b467c82 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 65f225e30..0190bcae6 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 c5303eb7a..ee53e9484 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 f3204e25a..895b5d0a8 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;