refactor(minifier): change prepass to ast_passes::remove_parens (#3801)

This commit is contained in:
Boshen 2024-06-21 07:18:08 +00:00
parent 49fab9d6be
commit 8027b1e894
5 changed files with 17 additions and 10 deletions

View file

@ -0,0 +1,3 @@
mod remove_parens;
pub use remove_parens::RemoveParens;

View file

@ -3,12 +3,13 @@ use oxc_ast::visit::walk_mut::{walk_expression_mut, walk_statements_mut};
#[allow(clippy::wildcard_imports)] #[allow(clippy::wildcard_imports)]
use oxc_ast::{ast::*, AstBuilder, VisitMut}; use oxc_ast::{ast::*, AstBuilder, VisitMut};
/// Remove Parenthesized Expression from the AST.
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Prepass<'a> { pub struct RemoveParens<'a> {
ast: AstBuilder<'a>, ast: AstBuilder<'a>,
} }
impl<'a> Prepass<'a> { impl<'a> RemoveParens<'a> {
pub fn new(allocator: &'a Allocator) -> Self { pub fn new(allocator: &'a Allocator) -> Self {
Self { ast: AstBuilder::new(allocator) } Self { ast: AstBuilder::new(allocator) }
} }
@ -25,7 +26,7 @@ impl<'a> Prepass<'a> {
} }
} }
impl<'a> VisitMut<'a> for Prepass<'a> { impl<'a> VisitMut<'a> for RemoveParens<'a> {
fn visit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>) { fn visit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>) {
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_))); stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
walk_statements_mut(self, stmts); walk_statements_mut(self, stmts);

View file

@ -3,7 +3,6 @@
mod ast_util; mod ast_util;
mod fold; mod fold;
mod options; mod options;
mod prepass;
mod util; mod util;
use oxc_allocator::{Allocator, Vec}; use oxc_allocator::{Allocator, Vec};
@ -20,20 +19,22 @@ use oxc_syntax::{
precedence::GetPrecedence, precedence::GetPrecedence,
}; };
pub use self::{options::CompressOptions, prepass::Prepass}; use crate::ast_passes::RemoveParens;
pub use self::options::CompressOptions;
pub struct Compressor<'a> { pub struct Compressor<'a> {
ast: AstBuilder<'a>, ast: AstBuilder<'a>,
options: CompressOptions, options: CompressOptions,
prepass: Prepass<'a>, prepass: RemoveParens<'a>,
} }
const SPAN: Span = Span::new(0, 0); const SPAN: Span = Span::new(0, 0);
impl<'a> Compressor<'a> { impl<'a> Compressor<'a> {
pub fn new(allocator: &'a Allocator, options: CompressOptions) -> Self { pub fn new(allocator: &'a Allocator, options: CompressOptions) -> Self {
Self { ast: AstBuilder::new(allocator), options, prepass: Prepass::new(allocator) } Self { ast: AstBuilder::new(allocator), options, prepass: RemoveParens::new(allocator) }
} }
pub fn build(mut self, program: &mut Program<'a>) { pub fn build(mut self, program: &mut Program<'a>) {

View file

@ -1,5 +1,6 @@
//! ECMAScript Minifier //! ECMAScript Minifier
mod ast_passes;
mod compressor; mod compressor;
mod mangler; mod mangler;
@ -7,7 +8,8 @@ use oxc_allocator::Allocator;
use oxc_ast::ast::Program; use oxc_ast::ast::Program;
pub use crate::{ pub use crate::{
compressor::{CompressOptions, Compressor, Prepass}, ast_passes::RemoveParens,
compressor::{CompressOptions, Compressor},
mangler::ManglerBuilder, mangler::ManglerBuilder,
}; };

View file

@ -1,6 +1,6 @@
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion}; use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
use oxc_minifier::{Minifier, MinifierOptions, Prepass}; use oxc_minifier::{Minifier, MinifierOptions, RemoveParens};
use oxc_parser::Parser; use oxc_parser::Parser;
use oxc_span::SourceType; use oxc_span::SourceType;
use oxc_tasks_common::TestFiles; use oxc_tasks_common::TestFiles;
@ -39,7 +39,7 @@ fn bench_passes(criterion: &mut Criterion) {
let allocator = Allocator::default(); let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program; let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program); let program = allocator.alloc(program);
b.iter(|| Prepass::new(&allocator).build(program)); b.iter(|| RemoveParens::new(&allocator).build(program));
}, },
); );
} }