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

Code before:

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(n.is_module()));
                        }

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

            if opts.compress.is_none() && opts.mangle.is_none() {
                return;
            }

            n.visit_mut_with(&mut hygiene_with_config(
                swc_ecma_transforms_base::hygiene::Config {
                    top_level_mark: self.top_level_mark,
                    ..Default::default()
                },
            ));

            let unresolved_mark = Mark::new();

Review comment (refactor):
The new unified logic always sets `v.toplevel` to `Some(TerserTopLevelOptions::Bool(n.is_module()))`, but the original code for scripts did not set `v.toplevel` at all (leaving it as None). This changes behavior for scripts from `toplevel: None` to `toplevel: Some(false)`. Please verify if this behavior change is intentional, as the PR description indicates this should be a pure refactoring with no behavioral changes.

Code after:

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()),
                        _ => None,
                    }),
                ..Default::default()
            };

            if opts.compress.is_none() && opts.mangle.is_none() {
                return;
            }

            n.visit_mut_with(&mut hygiene_with_config(
                swc_ecma_transforms_base::hygiene::Config {
                    top_level_mark: self.top_level_mark,
                    ..Default::default()