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

Code before:
use swc_atoms::Atom;
use swc_common::sync::Lrc;
use swc_ecma_ast::*;
use swc_ecma_transforms_base::perf::{ParVisitMut, Parallel};
use swc_ecma_utils::{collect_decls, parallel::cpu_count, NodeIgnoringSpan};
use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith};

/// The key will be compared using [EqIgnoreSpan::eq_ignore_span], and matched
/// expressions will be replaced with the value.
pub type GlobalExprMap = Lrc<FxHashMap<NodeIgnoringSpan<'static, Expr>, Expr>>;

/// Create a global inlining pass, which replaces expressions with the specified
/// value.
///
/// See [GlobalExprMap] for description.
///
/// Note: Values specified in `global_exprs` have higher precedence than
pub fn inline_globals(
    envs: Lrc<FxHashMap<Atom, Expr>>,
    globals: Lrc<FxHashMap<Atom, Expr>>,
    typeofs: Lrc<FxHashMap<Atom, Atom>>,
) -> impl Pass {
    inline_globals2(envs, globals, Default::default(), typeofs)
}

pub fn inline_globals2(
    envs: Lrc<FxHashMap<Atom, Expr>>,
    globals: Lrc<FxHashMap<Atom, Expr>>,
    global_exprs: GlobalExprMap,
    typeofs: Lrc<FxHashMap<Atom, Atom>>,
) -> impl Pass {
    visit_mut_pass(InlineGlobals {
        envs,
        globals,
        global_exprs,
        typeofs,
        bindings: Default::default(),
    })
}

#[derive(Clone)]
struct InlineGlobals {
    envs: Lrc<FxHashMap<Atom, Expr>>,
    globals: Lrc<FxHashMap<Atom, Expr>>,
    global_exprs: Lrc<FxHashMap<NodeIgnoringSpan<'static, Expr>, Expr>>,

    typeofs: Lrc<FxHashMap<Atom, Atom>>,

    bindings: Lrc<FxHashSet<Id>>,
}


Review comment (suggestion):
Can you remove API changes from this PR?
I think `inline_global` can still take the identical arguments as now for future enhancement

Code after:
use swc_atoms::Atom;
use swc_common::sync::Lrc;
use swc_ecma_ast::*;
use swc_ecma_transforms_base::perf::{ParVisitMut, Parallel};
use swc_ecma_utils::{collect_decls, parallel::cpu_count, NodeIgnoringSpan};
use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith};

/// The key will be compared using [EqIgnoreSpan::eq_ignore_span], and matched
/// expressions will be replaced with the value.
pub type GlobalExprMap = Lrc<FxHashMap<NodeIgnoringSpan<'static, Expr>, Expr>>;

/// Create a global inlining pass, which replaces expressions with the specified
/// value.
///
/// See [GlobalExprMap] for description.
///
/// Note: Values specified in `global_exprs` have higher precedence than
pub fn inline_globals(
    envs: Lrc<FxHashMap<Atom, Expr>>,
    globals: Lrc<FxHashMap<Atom, Expr>>,
    global_exprs: GlobalExprMap,
    typeofs: Lrc<FxHashMap<Atom, Atom>>,
) -> impl Pass {
    visit_mut_pass(InlineGlobals {
        envs,
        globals,
        global_exprs,
        typeofs,
        bindings: Default::default(),
    })
}

#[derive(Clone)]
struct InlineGlobals {
    envs: Lrc<FxHashMap<Atom, Expr>>,
    globals: Lrc<FxHashMap<Atom, Expr>>,
    global_exprs: Lrc<FxHashMap<NodeIgnoringSpan<'static, Expr>, Expr>>,

    typeofs: Lrc<FxHashMap<Atom, Atom>>,

    bindings: Lrc<FxHashSet<Id>>,
}

impl Parallel for InlineGlobals {
    fn create(&self) -> Self {
        self.clone()
    }

    fn merge(&mut self, _: Self) {}
}