Skip to content

Commit 81495f5

Browse files
authoredMar 7, 2023
feat(common): Add an API to create a SourceFile without allocation (#7029)
1 parent e93c79b commit 81495f5

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed
 

‎crates/swc_common/src/source_map.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,17 @@ impl SourceMap {
202202

203203
/// Creates a new source_file.
204204
/// This does not ensure that only one SourceFile exists per file name.
205-
pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
205+
pub fn new_source_file(&self, filename: FileName, mut src: String) -> Lrc<SourceFile> {
206+
remove_bom(&mut src);
207+
208+
self.new_source_file_from(filename, Lrc::new(src))
209+
}
210+
211+
/// Creates a new source_file.
212+
/// This does not ensure that only one SourceFile exists per file name.
213+
///
214+
/// `src` should not have UTF8 BOM
215+
pub fn new_source_file_from(&self, filename: FileName, src: Lrc<String>) -> Lrc<SourceFile> {
206216
// The path is used to determine the directory for loading submodules and
207217
// include files, so it must be before remapping.
208218
// Note that filename may not be a valid path, eg it may be `<anon>` etc,
@@ -224,7 +234,7 @@ impl SourceMap {
224234

225235
let start_pos = self.next_start_pos(src.len());
226236

227-
let source_file = Lrc::new(SourceFile::new(
237+
let source_file = Lrc::new(SourceFile::new_from(
228238
filename,
229239
was_remapped,
230240
unmapped_path,

‎crates/swc_common/src/syntax_pos.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -869,15 +869,32 @@ impl SourceFile {
869869
unmapped_path: FileName,
870870
mut src: String,
871871
start_pos: BytePos,
872+
) -> SourceFile {
873+
remove_bom(&mut src);
874+
875+
Self::new_from(
876+
name,
877+
name_was_remapped,
878+
unmapped_path,
879+
Lrc::new(src),
880+
start_pos,
881+
)
882+
}
883+
884+
/// `src` should not have UTF8 BOM
885+
pub fn new_from(
886+
name: FileName,
887+
name_was_remapped: bool,
888+
unmapped_path: FileName,
889+
src: Lrc<String>,
890+
start_pos: BytePos,
872891
) -> SourceFile {
873892
debug_assert_ne!(
874893
start_pos,
875894
BytePos::DUMMY,
876895
"BytePos::DUMMY is reserved and `SourceFile` should not use it"
877896
);
878897

879-
remove_bom(&mut src);
880-
881898
let src_hash = {
882899
let mut hasher: StableHasher = StableHasher::new();
883900
hasher.write(src.as_bytes());
@@ -898,7 +915,7 @@ impl SourceFile {
898915
name_was_remapped,
899916
unmapped_path: Some(unmapped_path),
900917
crate_of_origin: 0,
901-
src: Lrc::new(src),
918+
src,
902919
src_hash,
903920
start_pos,
904921
end_pos: Pos::from_usize(end_pos),
@@ -988,7 +1005,7 @@ impl SourceFile {
9881005
}
9891006

9901007
/// Remove utf-8 BOM if any.
991-
fn remove_bom(src: &mut String) {
1008+
pub(super) fn remove_bom(src: &mut String) {
9921009
if src.starts_with('\u{feff}') {
9931010
src.drain(..3);
9941011
}

0 commit comments

Comments
 (0)