-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
59 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
"Finalizer", | ||
"hasher", | ||
"importee", | ||
"Renamer", | ||
"rolldown", | ||
"rollup", | ||
"rustc" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod renamer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::borrow::Cow; | ||
|
||
use oxc::span::Atom; | ||
use rolldown_common::SymbolRef; | ||
use rustc_hash::FxHashMap; | ||
|
||
use crate::bundler::graph::symbols::Symbols; | ||
|
||
#[derive(Debug)] | ||
pub struct Renamer<'name> { | ||
name_to_count: FxHashMap<Cow<'name, Atom>, u32>, | ||
canonical_names: FxHashMap<SymbolRef, Atom>, | ||
symbols: &'name Symbols, | ||
} | ||
|
||
impl<'name> Renamer<'name> { | ||
pub fn new(symbols: &'name Symbols) -> Self { | ||
Self { name_to_count: FxHashMap::default(), canonical_names: FxHashMap::default(), symbols } | ||
} | ||
|
||
pub fn inc(&mut self, name: Cow<'name, Atom>) { | ||
*self.name_to_count.entry(name).or_default() += 1; | ||
} | ||
|
||
pub fn add_top_level_symbol(&mut self, symbol_ref: SymbolRef) { | ||
let canonical_ref = self.symbols.par_get_canonical_ref(symbol_ref); | ||
let original_name = self.symbols.get_original_name(canonical_ref); | ||
|
||
match self.canonical_names.entry(canonical_ref) { | ||
std::collections::hash_map::Entry::Occupied(_) => {} | ||
std::collections::hash_map::Entry::Vacant(vacant) => { | ||
let count = self.name_to_count.entry(Cow::Borrowed(original_name)).or_default(); | ||
if *count == 0 { | ||
vacant.insert(original_name.clone()); | ||
} else { | ||
vacant.insert(format!("{}${}", original_name, *count).into()); | ||
} | ||
*count += 1; | ||
} | ||
} | ||
} | ||
|
||
pub fn into_canonical_names(self) -> FxHashMap<SymbolRef, Atom> { | ||
self.canonical_names | ||
} | ||
} |