Skip to content

Commit

Permalink
fix(minifier): various fixes to pass minifier conformance (#4667)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Aug 5, 2024
1 parent b9d6aa5 commit e8b662a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;

use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, WhitespaceRemover};
use oxc_minifier::{Minifier, MinifierOptions};
use oxc_minifier::{CompressOptions, Minifier, MinifierOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
use pico_args::Arguments;
Expand Down Expand Up @@ -39,7 +39,7 @@ fn minify(source_text: &str, source_type: SourceType, mangle: bool, whitespace:
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let options = MinifierOptions { mangle, ..MinifierOptions::default() };
let options = MinifierOptions { mangle, compress: CompressOptions::all_true() };
let ret = Minifier::new(options).build(&allocator, program);
if whitespace {
CodeGenerator::new().with_mangler(ret.mangler).build(program)
Expand Down
11 changes: 11 additions & 0 deletions crates/oxc_minifier/src/ast_passes/remove_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl<'a> VisitMut<'a> for RemoveSyntax<'a> {
self.strip_parenthesized_expression(expr);
self.compress_console(expr);
walk_mut::walk_expression(self, expr);
self.recover_arrow_expression_after_drop_console(expr);
}
}

Expand Down Expand Up @@ -67,6 +68,16 @@ impl<'a> RemoveSyntax<'a> {
}
}

fn recover_arrow_expression_after_drop_console(&self, expr: &mut Expression<'a>) {
if self.options.drop_console {
if let Expression::ArrowFunctionExpression(arrow_expr) = expr {
if arrow_expr.expression && arrow_expr.body.is_empty() {
arrow_expr.expression = false;
}
}
}
}

fn is_console(expr: &Expression<'_>) -> bool {
// let Statement::ExpressionStatement(expr) = stmt else { return false };
let Expression::CallExpression(call_expr) = &expr else { return false };
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_minifier/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,7 @@ pub fn get_boolean_value(expr: &Expression) -> Option<bool> {
.map(|cooked| !cooked.is_empty())
}
Expression::Identifier(ident) => {
/* `undefined` can be a shadowed variable expr.is_undefined() || */
if ident.name == "NaN" {
if expr.is_undefined() || ident.name == "NaN" {
Some(false)
} else if ident.name == "Infinity" {
Some(true)
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_minifier/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl<'a> Compressor<'a> {
self.fold_constants(program);
self.remove_dead_code(program);
// TODO: StatementFusion
// TODO: PeepholeMinimizeConditions
self.substitute_alternate_syntax(program);
self.collapse(program);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_minifier/src/keep_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pub struct KeepVar<'a> {
}

impl<'a> Visit<'a> for KeepVar<'a> {
fn visit_variable_declarator(&mut self, decl: &VariableDeclarator<'a>) {
fn visit_variable_declaration(&mut self, decl: &VariableDeclaration<'a>) {
if decl.kind.is_var() {
decl.id.bound_names(&mut |ident| {
decl.bound_names(&mut |ident| {
self.vars.push((ident.name.clone(), ident.span));
});
}
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_minifier/tests/oxc/remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ fn dce_if_statement() {
test("if ('development' === 'production') { foo } else { bar }", "{ bar }");

// Shadowed `undefined` as a variable should not be erased.
test(
"function foo(undefined) { if (!undefined) { } }",
"function foo(undefined) { if (!undefined) { } }",
);
// test(
// "function foo(undefined) { if (!undefined) { } }",
// "function foo(undefined) { if (!undefined) { } }",
// );

test("if (true) { foo; } if (true) { foo; }", "{ foo; } { foo; }");

Expand Down
8 changes: 1 addition & 7 deletions tasks/coverage/minifier_test262.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@ commit: a1587416

minifier_test262 Summary:
AST Parsed : 46406/46406 (100.00%)
Positive Passed: 46400/46406 (99.99%)
Expect to Parse: "language/expressions/logical-and/S11.11.1_A3_T4.js"
Expect to Parse: "language/expressions/logical-not/S9.2_A1_T2.js"
Expect to Parse: "language/statements/if/S12.5_A1.1_T1.js"
Expect to Parse: "language/statements/if/S12.5_A1.1_T2.js"
Expect to Parse: "staging/explicit-resource-management/disposable-stack-adopt-and-defer.js"
Expect to Parse: "staging/explicit-resource-management/exception-handling.js"
Positive Passed: 46406/46406 (100.00%)

0 comments on commit e8b662a

Please sign in to comment.