Skip to content

Commit

Permalink
refactor(minifier): change prepass to ast_passes::remove_parens (#3801)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 21, 2024
1 parent 49fab9d commit 8027b1e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
3 changes: 3 additions & 0 deletions crates/oxc_minifier/src/ast_passes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod remove_parens;

pub use remove_parens::RemoveParens;
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use oxc_ast::visit::walk_mut::{walk_expression_mut, walk_statements_mut};
#[allow(clippy::wildcard_imports)]
use oxc_ast::{ast::*, AstBuilder, VisitMut};

/// Remove Parenthesized Expression from the AST.
#[derive(Clone, Copy)]
pub struct Prepass<'a> {
pub struct RemoveParens<'a> {
ast: AstBuilder<'a>,
}

impl<'a> Prepass<'a> {
impl<'a> RemoveParens<'a> {
pub fn new(allocator: &'a Allocator) -> Self {
Self { ast: AstBuilder::new(allocator) }
}
Expand All @@ -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>>) {
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
walk_statements_mut(self, stmts);
Expand Down
9 changes: 5 additions & 4 deletions crates/oxc_minifier/src/compressor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
mod ast_util;
mod fold;
mod options;
mod prepass;
mod util;

use oxc_allocator::{Allocator, Vec};
Expand All @@ -20,20 +19,22 @@ use oxc_syntax::{
precedence::GetPrecedence,
};

pub use self::{options::CompressOptions, prepass::Prepass};
use crate::ast_passes::RemoveParens;

pub use self::options::CompressOptions;

pub struct Compressor<'a> {
ast: AstBuilder<'a>,
options: CompressOptions,

prepass: Prepass<'a>,
prepass: RemoveParens<'a>,
}

const SPAN: Span = Span::new(0, 0);

impl<'a> Compressor<'a> {
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>) {
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_minifier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! ECMAScript Minifier

mod ast_passes;
mod compressor;
mod mangler;

use oxc_allocator::Allocator;
use oxc_ast::ast::Program;

pub use crate::{
compressor::{CompressOptions, Compressor, Prepass},
ast_passes::RemoveParens,
compressor::{CompressOptions, Compressor},
mangler::ManglerBuilder,
};

Expand Down
4 changes: 2 additions & 2 deletions tasks/benchmark/benches/minifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use oxc_allocator::Allocator;
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_span::SourceType;
use oxc_tasks_common::TestFiles;
Expand Down Expand Up @@ -39,7 +39,7 @@ fn bench_passes(criterion: &mut Criterion) {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
b.iter(|| Prepass::new(&allocator).build(program));
b.iter(|| RemoveParens::new(&allocator).build(program));
},
);
}
Expand Down

0 comments on commit 8027b1e

Please sign in to comment.