From 8fc18c68241bc7c1fe3dfdcd604ecbd547a218de Mon Sep 17 00:00:00 2001 From: Boshen Date: Sat, 24 May 2025 13:34:31 +0800 Subject: [PATCH] feat(napi/minify): `preserve_parens: false` when parsing Parser with `preserve_parens: false` now works correctly for the minification pipeline. --- napi/minify/src/lib.rs | 7 +++++-- tasks/benchmark/benches/minifier.rs | 10 ++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/napi/minify/src/lib.rs b/napi/minify/src/lib.rs index a14ccd534a806..43462c71dfa0b 100644 --- a/napi/minify/src/lib.rs +++ b/napi/minify/src/lib.rs @@ -17,7 +17,7 @@ use napi_derive::napi; use oxc_allocator::Allocator; use oxc_codegen::{Codegen, CodegenOptions}; use oxc_minifier::Minifier; -use oxc_parser::Parser; +use oxc_parser::{ParseOptions, Parser}; use oxc_span::SourceType; use crate::options::{MinifyOptions, MinifyResult}; @@ -40,7 +40,10 @@ pub fn minify( let source_type = SourceType::from_path(&filename).unwrap_or_default(); - let mut program = Parser::new(&allocator, &source_text, source_type).parse().program; + let mut program = Parser::new(&allocator, &source_text, source_type) + .with_options(ParseOptions { preserve_parens: false, ..ParseOptions::default() }) + .parse() + .program; let scoping = Minifier::new(minifier_options).build(&allocator, &mut program).scoping; diff --git a/tasks/benchmark/benches/minifier.rs b/tasks/benchmark/benches/minifier.rs index a7b2f6524c0f4..e789fbbcf4584 100644 --- a/tasks/benchmark/benches/minifier.rs +++ b/tasks/benchmark/benches/minifier.rs @@ -4,7 +4,7 @@ use oxc_allocator::Allocator; use oxc_benchmark::{BenchmarkId, Criterion, criterion_group, criterion_main}; use oxc_mangler::Mangler; use oxc_minifier::{CompressOptions, Compressor}; -use oxc_parser::Parser; +use oxc_parser::{ParseOptions, Parser}; use oxc_semantic::SemanticBuilder; use oxc_span::SourceType; use oxc_tasks_common::TestFiles; @@ -28,7 +28,13 @@ fn bench_minifier(criterion: &mut Criterion) { allocator.reset(); // Create fresh AST + semantic data for each iteration - let mut program = Parser::new(&allocator, source_text, source_type).parse().program; + let mut program = Parser::new(&allocator, source_text, source_type) + .with_options(ParseOptions { + preserve_parens: false, + ..ParseOptions::default() + }) + .parse() + .program; let scoping = SemanticBuilder::new().build(&program).semantic.into_scoping(); // Minifier only works on esnext.