From a9c30b19023b9746ce843a5eace09740d7b46fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 7 Mar 2023 23:00:55 +0900 Subject: [PATCH 1/2] API --- crates/swc_common/src/source_map.rs | 10 +++++++++- crates/swc_common/src/syntax_pos.rs | 23 ++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/swc_common/src/source_map.rs b/crates/swc_common/src/source_map.rs index 8029b6c57c61..75177ccbb7b3 100644 --- a/crates/swc_common/src/source_map.rs +++ b/crates/swc_common/src/source_map.rs @@ -203,6 +203,14 @@ impl SourceMap { /// Creates a new source_file. /// This does not ensure that only one SourceFile exists per file name. pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc { + self.new_source_file_from(filename, Lrc::new(src)) + } + + /// Creates a new source_file. + /// This does not ensure that only one SourceFile exists per file name. + /// + /// `src` should not have UTF8 BOM + pub fn new_source_file_from(&self, filename: FileName, src: Lrc) -> Lrc { // The path is used to determine the directory for loading submodules and // include files, so it must be before remapping. // Note that filename may not be a valid path, eg it may be `` etc, @@ -224,7 +232,7 @@ impl SourceMap { let start_pos = self.next_start_pos(src.len()); - let source_file = Lrc::new(SourceFile::new( + let source_file = Lrc::new(SourceFile::new_from( filename, was_remapped, unmapped_path, diff --git a/crates/swc_common/src/syntax_pos.rs b/crates/swc_common/src/syntax_pos.rs index d23c2f455c4f..e8d90afc62ec 100644 --- a/crates/swc_common/src/syntax_pos.rs +++ b/crates/swc_common/src/syntax_pos.rs @@ -869,6 +869,25 @@ impl SourceFile { unmapped_path: FileName, mut src: String, start_pos: BytePos, + ) -> SourceFile { + remove_bom(&mut src); + + Self::new_from( + name, + name_was_remapped, + unmapped_path, + Lrc::new(src), + start_pos, + ) + } + + /// `src` should not have UTF8 BOM + pub fn new_from( + name: FileName, + name_was_remapped: bool, + unmapped_path: FileName, + src: Lrc, + start_pos: BytePos, ) -> SourceFile { debug_assert_ne!( start_pos, @@ -876,8 +895,6 @@ impl SourceFile { "BytePos::DUMMY is reserved and `SourceFile` should not use it" ); - remove_bom(&mut src); - let src_hash = { let mut hasher: StableHasher = StableHasher::new(); hasher.write(src.as_bytes()); @@ -898,7 +915,7 @@ impl SourceFile { name_was_remapped, unmapped_path: Some(unmapped_path), crate_of_origin: 0, - src: Lrc::new(src), + src, src_hash, start_pos, end_pos: Pos::from_usize(end_pos), From edd97c0cc7e982721f03d0df9769196a6d8313ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 7 Mar 2023 23:03:27 +0900 Subject: [PATCH 2/2] remove bom --- crates/swc_common/src/source_map.rs | 4 +++- crates/swc_common/src/syntax_pos.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/swc_common/src/source_map.rs b/crates/swc_common/src/source_map.rs index 75177ccbb7b3..25780e63168f 100644 --- a/crates/swc_common/src/source_map.rs +++ b/crates/swc_common/src/source_map.rs @@ -202,7 +202,9 @@ impl SourceMap { /// Creates a new source_file. /// This does not ensure that only one SourceFile exists per file name. - pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc { + pub fn new_source_file(&self, filename: FileName, mut src: String) -> Lrc { + remove_bom(&mut src); + self.new_source_file_from(filename, Lrc::new(src)) } diff --git a/crates/swc_common/src/syntax_pos.rs b/crates/swc_common/src/syntax_pos.rs index e8d90afc62ec..75f42ec5a546 100644 --- a/crates/swc_common/src/syntax_pos.rs +++ b/crates/swc_common/src/syntax_pos.rs @@ -1005,7 +1005,7 @@ impl SourceFile { } /// Remove utf-8 BOM if any. -fn remove_bom(src: &mut String) { +pub(super) fn remove_bom(src: &mut String) { if src.starts_with('\u{feff}') { src.drain(..3); }