Skip to content

Commit

Permalink
fix(minifier): correct the reference link (#6283)
Browse files Browse the repository at this point in the history
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`?
  • Loading branch information
shulaoda authored Oct 5, 2024
1 parent f0a74ca commit d953a6b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/CollapseVariableDeclarations.java>
pub struct CollapseVariableDeclarations {
options: CompressOptions,

Expand Down Expand Up @@ -99,7 +100,7 @@ impl<'a> CollapseVariableDeclarations {
}
}

/// <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/CollapseVariableDeclarations.java>
/// <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/CollapseVariableDeclarationsTest.java>
#[cfg(test)]
mod test {
use oxc_allocator::Allocator;
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_minifier/src/ast_passes/exploit_assigns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl ExploitAssigns {
}
}

/// <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/ExploitAssignsTest.java>
#[cfg(test)]
mod test {
use oxc_allocator::Allocator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ impl<'a> PeepholeFoldConstants {
}
}

/// <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/PeepholeFoldConstants.java>
/// <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/PeepholeFoldConstantsTest.java>
#[cfg(test)]
mod test {
use oxc_allocator::Allocator;
Expand Down
48 changes: 48 additions & 0 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeRemoveDeadCode.java>
pub struct PeepholeRemoveDeadCode {
changed: bool,
}
Expand Down Expand Up @@ -183,3 +184,50 @@ impl<'a> PeepholeRemoveDeadCode {
}
}
}

/// <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/PeepholeRemoveDeadCodeTest.java>
#[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;");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_traverse::{Traverse, TraverseCtx};
use crate::CompressorPass;

/// Minimize With Known Methods
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeMinimizeReplacements.java>
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeReplaceKnownMethods.java>
pub struct PeepholeReplaceKnownMethods {
changed: bool,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java>
pub struct PeepholeSubstituteAlternateSyntax {
options: CompressOptions,
in_define_export: bool,
Expand Down Expand Up @@ -329,7 +330,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
}
}

/// <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java>
/// <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntaxTest.java>
#[cfg(test)]
mod test {
use oxc_allocator::Allocator;
Expand Down

0 comments on commit d953a6b

Please sign in to comment.