mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(minifier): various fixes to pass minifier conformance (#4667)
This commit is contained in:
parent
b9d6aa56a0
commit
e8b662a314
7 changed files with 21 additions and 18 deletions
|
|
@ -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;
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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 };
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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; }");
|
||||
|
||||
|
|
|
|||
|
|
@ -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%)
|
||||
|
|
|
|||
Loading…
Reference in a new issue