Repository: swc-project/swc
File: crates/swc_ecma_transforms_base/src/rename/mod.rs (Rust)

Code before:
        unresolved: Default::default(),
        previous_cache: Default::default(),
        total_map: None,
        marker: std::marker::PhantomData::<Atom>,
    })
}

/// Create correct (unique) syntax contexts. Use this if you need the syntax
/// contexts produces by this pass (unlike the default `hygiene` pass which
/// removes them anyway.)
pub fn renamer_keep_contexts<R>(config: Config, renamer: R) -> impl Pass + VisitMut
where
    R: Renamer<Target = Id>,
{
    visit_mut_pass(RenamePass {
        config,
        renamer,
        preserved: Default::default(),
        unresolved: Default::default(),
        previous_cache: Default::default(),
        total_map: None,
        marker: std::marker::PhantomData::<Id>,
    })
}

pub trait RenamedVariable: Clone + Sized + std::marker::Send + std::marker::Sync + 'static {
    fn new_private(sym: Atom) -> Self;
    fn to_id(&self) -> Id;
}
impl RenamedVariable for Atom {
    fn new_private(sym: Atom) -> Self {
        sym
    }

    fn to_id(&self) -> Id {
        (self.clone(), Default::default())
    }
}
impl RenamedVariable for Id {
    fn new_private(sym: Atom) -> Self {
        (sym, SyntaxContext::empty().apply_mark(Mark::new()))
    }

    fn to_id(&self) -> Id {
        self.clone()
    }
}

#[derive(Debug, Default)]
struct RenamePass<R, V>
where

Review comment (suggestion):
Can you seal this type?



```rust

mod private {
	pub trait Sealed {}
	impl Sealed for Id {}
	impl Sealed for Atom {}
};

```

Code after:
        renamer,
        preserved: Default::default(),
        unresolved: Default::default(),
        previous_cache: Default::default(),
        total_map: None,
        marker: std::marker::PhantomData::<Atom>,
    })
}

/// Create correct (unique) syntax contexts. Use this if you need the syntax
/// contexts produces by this pass (unlike the default `hygiene` pass which
/// removes them anyway.)
pub fn renamer_keep_contexts<R>(config: Config, renamer: R) -> impl Pass + VisitMut
where
    R: Renamer<Target = Id>,
{
    visit_mut_pass(RenamePass {
        config,
        renamer,
        preserved: Default::default(),
        unresolved: Default::default(),
        previous_cache: Default::default(),
        total_map: None,
        marker: std::marker::PhantomData::<Id>,
    })
}

mod private {
    use swc_atoms::Atom;
    use swc_ecma_ast::Id;

    pub trait Sealed {}

    impl Sealed for Atom {}
    impl Sealed for Id {}
}

/// A trait that is used to represent a renamed variable. For
/// `renamer_keep_contexts`, the syntax contexts of the replacements should be
/// correct (unique), while for `hygiene` (which calls `renamer`), the resulting
/// syntax contexts are irrelevant. This type is used to handle both cases
/// without code duplication by using `HashMap<Id, impl RenamedVariable>`
/// everywhere:
/// - For `renamer`, `HashMap<Id, Atom>` is used (and `SyntaxContext::empty()`
///   isn't store unnecessarily). All replaced idents have the same
///   SyntaxContext #0.
/// - For `renamer_keep_contexts`, `HashMap<Id, Id>` is used. All replaced
///   idents have a unique SyntaxContext.
pub trait RenamedVariable:
    private::Sealed + Clone + Sized + std::marker::Send + std::marker::Sync + 'static
{