Skip to content

Commit 8027b1e

Browse files
committed
refactor(minifier): change prepass to ast_passes::remove_parens (#3801)
1 parent 49fab9d commit 8027b1e

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod remove_parens;
2+
3+
pub use remove_parens::RemoveParens;

crates/oxc_minifier/src/compressor/prepass.rs renamed to crates/oxc_minifier/src/ast_passes/remove_parens.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use oxc_ast::visit::walk_mut::{walk_expression_mut, walk_statements_mut};
33
#[allow(clippy::wildcard_imports)]
44
use oxc_ast::{ast::*, AstBuilder, VisitMut};
55

6+
/// Remove Parenthesized Expression from the AST.
67
#[derive(Clone, Copy)]
7-
pub struct Prepass<'a> {
8+
pub struct RemoveParens<'a> {
89
ast: AstBuilder<'a>,
910
}
1011

11-
impl<'a> Prepass<'a> {
12+
impl<'a> RemoveParens<'a> {
1213
pub fn new(allocator: &'a Allocator) -> Self {
1314
Self { ast: AstBuilder::new(allocator) }
1415
}
@@ -25,7 +26,7 @@ impl<'a> Prepass<'a> {
2526
}
2627
}
2728

28-
impl<'a> VisitMut<'a> for Prepass<'a> {
29+
impl<'a> VisitMut<'a> for RemoveParens<'a> {
2930
fn visit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>) {
3031
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
3132
walk_statements_mut(self, stmts);

crates/oxc_minifier/src/compressor/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
mod ast_util;
44
mod fold;
55
mod options;
6-
mod prepass;
76
mod util;
87

98
use oxc_allocator::{Allocator, Vec};
@@ -20,20 +19,22 @@ use oxc_syntax::{
2019
precedence::GetPrecedence,
2120
};
2221

23-
pub use self::{options::CompressOptions, prepass::Prepass};
22+
use crate::ast_passes::RemoveParens;
23+
24+
pub use self::options::CompressOptions;
2425

2526
pub struct Compressor<'a> {
2627
ast: AstBuilder<'a>,
2728
options: CompressOptions,
2829

29-
prepass: Prepass<'a>,
30+
prepass: RemoveParens<'a>,
3031
}
3132

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

3435
impl<'a> Compressor<'a> {
3536
pub fn new(allocator: &'a Allocator, options: CompressOptions) -> Self {
36-
Self { ast: AstBuilder::new(allocator), options, prepass: Prepass::new(allocator) }
37+
Self { ast: AstBuilder::new(allocator), options, prepass: RemoveParens::new(allocator) }
3738
}
3839

3940
pub fn build(mut self, program: &mut Program<'a>) {

crates/oxc_minifier/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! ECMAScript Minifier
22
3+
mod ast_passes;
34
mod compressor;
45
mod mangler;
56

67
use oxc_allocator::Allocator;
78
use oxc_ast::ast::Program;
89

910
pub use crate::{
10-
compressor::{CompressOptions, Compressor, Prepass},
11+
ast_passes::RemoveParens,
12+
compressor::{CompressOptions, Compressor},
1113
mangler::ManglerBuilder,
1214
};
1315

tasks/benchmark/benches/minifier.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use oxc_allocator::Allocator;
22
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
3-
use oxc_minifier::{Minifier, MinifierOptions, Prepass};
3+
use oxc_minifier::{Minifier, MinifierOptions, RemoveParens};
44
use oxc_parser::Parser;
55
use oxc_span::SourceType;
66
use oxc_tasks_common::TestFiles;
@@ -39,7 +39,7 @@ fn bench_passes(criterion: &mut Criterion) {
3939
let allocator = Allocator::default();
4040
let program = Parser::new(&allocator, source_text, source_type).parse().program;
4141
let program = allocator.alloc(program);
42-
b.iter(|| Prepass::new(&allocator).build(program));
42+
b.iter(|| RemoveParens::new(&allocator).build(program));
4343
},
4444
);
4545
}

0 commit comments

Comments
 (0)