Repository: swc-project/swc
File: crates/swc/src/builder.rs (Rust)

Code before:
use swc_common::{comments::Comments, sync::Lrc, util::take::Take, Mark, SourceMap};
use swc_ecma_ast::{Pass, Program};
use swc_ecma_minifier::option::{terser::TerserTopLevelOptions, MinifyOptions};
use swc_ecma_transforms::{hygiene::hygiene_with_config, resolver};
use swc_ecma_visit::VisitMutWith;

use crate::config::JsMinifyOptions;

pub struct MinifierPass<'a> {
    pub options: Option<JsMinifyOptions>,
    pub cm: Lrc<SourceMap>,
    pub comments: Option<&'a dyn Comments>,
    pub top_level_mark: Mark,
}

impl Pass for MinifierPass<'_> {
    fn process(&mut self, n: &mut Program) {
        if let Some(options) = &self.options {
            let opts = MinifyOptions {
                compress: options
                    .compress
                    .clone()
                    .unwrap_as_option(|default| match default {
                        Some(true) => Some(Default::default()),
                        _ => None,
                    })
                    .map(|mut v| {
                        if v.const_to_let.is_none() {
                            v.const_to_let = Some(true);
                        }
                        if v.toplevel.is_none() {
                            v.toplevel = Some(TerserTopLevelOptions::Bool(true));
                        }

                        v.into_config(self.cm.clone())
                    }),
                mangle: options
                    .mangle
                    .clone()
                    .unwrap_as_option(|default| match default {
                        Some(true) => Some(Default::default()),
                        _ => None,
                    }),
                ..Default::default()
            };

Review comment (refactor):
This refactoring introduces new behavior by setting `v.toplevel = Some(TerserTopLevelOptions::Bool(true))` which was not present in either the old `visit_mut_module` or `visit_mut_script` methods. This contradicts the PR description which states "pure refactoring with no behavioral changes". If this is an intentional improvement, it should be documented in the PR description or handled in a separate PR to avoid mixing refactoring with behavior changes.

Code after:
use swc_common::{comments::Comments, sync::Lrc, util::take::Take, Mark, SourceMap};
use swc_ecma_ast::{Pass, Program};
use swc_ecma_minifier::option::{terser::TerserTopLevelOptions, MinifyOptions};
use swc_ecma_transforms::{hygiene::hygiene_with_config, resolver};
use swc_ecma_visit::VisitMutWith;

use crate::config::JsMinifyOptions;

pub struct MinifierPass<'a> {
    pub options: Option<JsMinifyOptions>,
    pub cm: Lrc<SourceMap>,
    pub comments: Option<&'a dyn Comments>,
    pub top_level_mark: Mark,
}

impl Pass for MinifierPass<'_> {
    fn process(&mut self, n: &mut Program) {
        if let Some(options) = &self.options {
            let opts = MinifyOptions {
                compress: options
                    .compress
                    .clone()
                    .unwrap_as_option(|default| match default {
                        Some(true) => Some(Default::default()),
                        _ => None,
                    })
                    .map(|mut v| {
                        if v.const_to_let.is_none() {
                            v.const_to_let = Some(true);
                        }
                        if v.toplevel.is_none() && n.is_module() {
                            v.toplevel = Some(TerserTopLevelOptions::Bool(true));
                        }

                        if n.is_script() {
                            v.module = false;
                        }

                        v.into_config(self.cm.clone())
                    }),
                mangle: options
                    .mangle
                    .clone()
                    .unwrap_as_option(|default| match default {
                        Some(true) => Some(Default::default()),