Skip to content

Commit ee80f5c

Browse files
committed
Add comments
1 parent f1f4d32 commit ee80f5c

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

.changeset/red-trees-smoke.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
swc_ecma_transforms_base: major
33
---
44

5-
feat(): Add `renamer_keep_contexts`
5+
feat(es/transforms): Add `renamer_keep_contexts`

crates/swc_ecma_transforms_base/src/hygiene/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl Fold for OnceMarker {
6464
}
6565
}
6666

67+
/// Apply the n-th mark to idents of the form `name$2`.
6768
struct InlineContextMarker {
6869
marks: Vec<Mark>,
6970
}

crates/swc_ecma_transforms_base/src/rename/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ mod eval;
2222
mod ops;
2323

2424
pub trait Renamer: Send + Sync {
25+
/// See the [`RenamedVariable`] documentation, this type determines whether
26+
/// impls can be used with [`renamer`] or [`renamer_keep_contexts`] .
2527
type Target: RenamedVariable;
2628

2729
/// Should reset `n` to 0 for each identifier?
@@ -52,7 +54,7 @@ pub trait Renamer: Send + Sync {
5254

5355
pub type RenameMap = FxHashMap<Id, Atom>;
5456

55-
pub fn rename(map: &RenameMap) -> impl '_ + Pass + VisitMut {
57+
pub fn rename<V: RenamedVariable>(map: &FxHashMap<Id, V>) -> impl '_ + Pass + VisitMut {
5658
rename_with_config(map, Default::default())
5759
}
5860

@@ -110,9 +112,22 @@ mod private {
110112
impl Sealed for Id {}
111113
}
112114

115+
/// A trait that is used to represent a renamed variable. For
116+
/// `renamer_keep_contexts`, the syntax contexts of the replacements should be
117+
/// correct (unique), while for `hygiene` (which calls `renamer`), the resulting
118+
/// syntax contexts are irrelevant. This type is used to handle both cases
119+
/// without code duplication by using `HashMap<Id, impl RenamedVariable>`
120+
/// everywhere:
121+
/// - For `renamer`, `HashMap<Id, Atom>` is used (and `SyntaxContext::empty()`
122+
/// isn't store unnecessarily). All replaced idents have the same
123+
/// SyntaxContext #0.
124+
/// - For `renamer_keep_contexts`, `HashMap<Id, Id>` is used. All replaced
125+
/// idents have a unique SyntaxContext.
113126
pub trait RenamedVariable:
114127
private::Sealed + Clone + Sized + std::marker::Send + std::marker::Sync + 'static
115128
{
129+
/// Potentially create a new private variable, depending on whether the
130+
/// consumer cares about the syntax context after the renaming.
116131
fn new_private(sym: Atom) -> Self;
117132
fn to_id(&self) -> Id;
118133
}

crates/swc_ecma_transforms_base/src/rename/ops.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515

1616
pub(super) struct Operator<'a, V>
1717
where
18-
V: RenamedVariable + Sync,
18+
V: RenamedVariable,
1919
{
2020
pub rename: &'a FxHashMap<Id, V>,
2121
pub config: Config,
@@ -25,7 +25,7 @@ where
2525

2626
impl<V> Operator<'_, V>
2727
where
28-
V: RenamedVariable + Sync,
28+
V: RenamedVariable,
2929
{
3030
fn keep_class_name(&mut self, ident: &mut Ident, class: &mut Class) -> Option<ClassExpr> {
3131
if !self.config.keep_class_names {
@@ -64,7 +64,7 @@ where
6464

6565
impl<V> Parallel for Operator<'_, V>
6666
where
67-
V: RenamedVariable + Sync,
67+
V: RenamedVariable,
6868
{
6969
fn create(&self) -> Self {
7070
Self {
@@ -85,7 +85,7 @@ where
8585

8686
impl<V> ParExplode for Operator<'_, V>
8787
where
88-
V: RenamedVariable + Sync,
88+
V: RenamedVariable,
8989
{
9090
fn after_one_stmt(&mut self, _: &mut Vec<Stmt>) {}
9191

@@ -96,7 +96,7 @@ where
9696

9797
impl<V> VisitMut for Operator<'_, V>
9898
where
99-
V: RenamedVariable + Sync,
99+
V: RenamedVariable,
100100
{
101101
noop_visit_mut_type!();
102102

@@ -611,15 +611,15 @@ where
611611

612612
struct VarFolder<'a, 'b, V>
613613
where
614-
V: RenamedVariable + Sync,
614+
V: RenamedVariable,
615615
{
616616
orig: &'a mut Operator<'b, V>,
617617
renamed: &'a mut Vec<ExportSpecifier>,
618618
}
619619

620620
impl<V> VisitMut for VarFolder<'_, '_, V>
621621
where
622-
V: RenamedVariable + Sync,
622+
V: RenamedVariable,
623623
{
624624
noop_visit_mut_type!();
625625

@@ -645,7 +645,7 @@ where
645645

646646
impl<V> Operator<'_, V>
647647
where
648-
V: RenamedVariable + Sync,
648+
V: RenamedVariable,
649649
{
650650
/// Returns `Ok(renamed_ident)` if ident should be renamed.
651651
fn rename_ident(&mut self, ident: &mut Ident) -> Result<(), ()> {

0 commit comments

Comments
 (0)