Skip to content

Commit 772f023

Browse files
authoredNov 6, 2024··
fix(es/plugin): Revert #9696 (#9717)
1 parent 6194044 commit 772f023

File tree

7 files changed

+39
-179
lines changed

7 files changed

+39
-179
lines changed
 

‎.changeset/clever-jobs-hug.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_common: major
3+
---
4+
5+
fix(es/plugin): Revert #9696

‎crates/swc_common/src/cache.rs

-115
This file was deleted.

‎crates/swc_common/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ pub use self::{
5151
source_map::{FileLines, FileLoader, FilePathMapping, SourceMap, SpanSnippetError},
5252
syntax_pos::LineCol,
5353
};
54+
#[doc(hidden)]
55+
pub mod private;
5456

5557
/// A trait for ast nodes.
5658
pub trait AstNode: Debug + PartialEq + Clone + Spanned {
5759
const TYPE: &'static str;
5860
}
5961

60-
pub mod cache;
6162
pub mod collections;
6263
pub mod comments;
6364
mod eq;
@@ -67,8 +68,6 @@ pub mod iter;
6768
pub mod pass;
6869
pub mod plugin;
6970
mod pos;
70-
#[doc(hidden)]
71-
pub mod private;
7271
mod rustc_data_structures;
7372
pub mod serializer;
7473
pub mod source_map;

‎crates/swc_common/src/source_map.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,10 @@ impl SourceMap {
316316
let line_info = self.lookup_line_with(fm, pos);
317317
match line_info {
318318
Ok(SourceFileAndLine { sf: f, line: a }) => {
319-
let analysis = f.analyze();
320319
let chpos = self.bytepos_to_file_charpos_with(&f, pos);
321320

322321
let line = a + 1; // Line numbers start at 1
323-
let linebpos = f.analyze().lines[a];
322+
let linebpos = f.lines[a];
324323
assert!(
325324
pos >= linebpos,
326325
"{}: bpos = {:?}; linebpos = {:?};",
@@ -333,17 +332,16 @@ impl SourceMap {
333332
let col = chpos - linechpos;
334333

335334
let col_display = {
336-
let start_width_idx = analysis
335+
let start_width_idx = f
337336
.non_narrow_chars
338337
.binary_search_by_key(&linebpos, |x| x.pos())
339338
.unwrap_or_else(|x| x);
340-
let end_width_idx = analysis
339+
let end_width_idx = f
341340
.non_narrow_chars
342341
.binary_search_by_key(&pos, |x| x.pos())
343342
.unwrap_or_else(|x| x);
344343
let special_chars = end_width_idx - start_width_idx;
345-
let non_narrow: usize = analysis.non_narrow_chars
346-
[start_width_idx..end_width_idx]
344+
let non_narrow: usize = f.non_narrow_chars[start_width_idx..end_width_idx]
347345
.iter()
348346
.map(|x| x.width())
349347
.sum();
@@ -369,15 +367,14 @@ impl SourceMap {
369367
})
370368
}
371369
Err(f) => {
372-
let analysis = f.analyze();
373370
let chpos = self.bytepos_to_file_charpos(pos)?;
374371

375372
let col_display = {
376-
let end_width_idx = analysis
373+
let end_width_idx = f
377374
.non_narrow_chars
378375
.binary_search_by_key(&pos, |x| x.pos())
379376
.unwrap_or_else(|x| x);
380-
let non_narrow: usize = analysis.non_narrow_chars[0..end_width_idx]
377+
let non_narrow: usize = f.non_narrow_chars[0..end_width_idx]
381378
.iter()
382379
.map(|x| x.width())
383380
.sum();
@@ -1031,11 +1028,11 @@ impl SourceMap {
10311028
) -> u32 {
10321029
let mut total_extra_bytes = state.total_extra_bytes;
10331030
let mut index = state.mbc_index;
1034-
let analysis = file.analyze();
1031+
10351032
if bpos >= state.pos {
1036-
let range = index..analysis.multibyte_chars.len();
1033+
let range = index..file.multibyte_chars.len();
10371034
for i in range {
1038-
let mbc = &analysis.multibyte_chars[i];
1035+
let mbc = &file.multibyte_chars[i];
10391036
debug!("{}-byte char at {:?}", mbc.bytes, mbc.pos);
10401037
if mbc.pos >= bpos {
10411038
break;
@@ -1055,7 +1052,7 @@ impl SourceMap {
10551052
} else {
10561053
let range = 0..index;
10571054
for i in range.rev() {
1058-
let mbc = &analysis.multibyte_chars[i];
1055+
let mbc = &file.multibyte_chars[i];
10591056
debug!("{}-byte char at {:?}", mbc.bytes, mbc.pos);
10601057
if mbc.pos < bpos {
10611058
break;
@@ -1325,8 +1322,7 @@ impl SourceMap {
13251322
None => continue,
13261323
};
13271324

1328-
let analysis = f.analyze();
1329-
let linebpos = analysis.lines[line as usize];
1325+
let linebpos = f.lines[line as usize];
13301326
debug_assert!(
13311327
pos >= linebpos,
13321328
"{}: bpos = {:?}; linebpos = {:?};",

‎crates/swc_common/src/syntax_pos.rs

+19-44
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use url::Url;
1616

1717
use self::hygiene::MarkData;
1818
pub use self::hygiene::{Mark, SyntaxContext};
19-
use crate::{cache::CacheCell, rustc_data_structures::stable_hasher::StableHasher, sync::Lrc};
19+
use crate::{rustc_data_structures::stable_hasher::StableHasher, sync::Lrc};
2020

2121
mod analyze_source_file;
2222
pub mod hygiene;
@@ -827,26 +827,14 @@ pub struct SourceFile {
827827
pub start_pos: BytePos,
828828
/// The end position of this source in the `SourceMap`
829829
pub end_pos: BytePos,
830-
/// A hash of the filename, used for speeding up the incr. comp. hashing.
831-
pub name_hash: u128,
832-
833-
lazy: CacheCell<SourceFileAnalysis>,
834-
}
835-
836-
#[cfg_attr(
837-
any(feature = "rkyv-impl"),
838-
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
839-
)]
840-
#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))]
841-
#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))]
842-
#[derive(Clone)]
843-
pub struct SourceFileAnalysis {
844830
/// Locations of lines beginnings in the source code
845831
pub lines: Vec<BytePos>,
846832
/// Locations of multi-byte characters in the source code
847833
pub multibyte_chars: Vec<MultiByteChar>,
848834
/// Width of characters that are not narrow in the source code
849835
pub non_narrow_chars: Vec<NonNarrowChar>,
836+
/// A hash of the filename, used for speeding up the incr. comp. hashing.
837+
pub name_hash: u128,
850838
}
851839

852840
impl fmt::Debug for SourceFile {
@@ -900,6 +888,9 @@ impl SourceFile {
900888
};
901889
let end_pos = start_pos.to_usize() + src.len();
902890

891+
let (lines, multibyte_chars, non_narrow_chars) =
892+
analyze_source_file::analyze_source_file(&src[..], start_pos);
893+
903894
SourceFile {
904895
name,
905896
name_was_remapped,
@@ -909,16 +900,17 @@ impl SourceFile {
909900
src_hash,
910901
start_pos,
911902
end_pos: SmallPos::from_usize(end_pos),
903+
lines,
904+
multibyte_chars,
905+
non_narrow_chars,
912906
name_hash,
913-
lazy: CacheCell::new(),
914907
}
915908
}
916909

917910
/// Return the BytePos of the beginning of the current line.
918911
pub fn line_begin_pos(&self, pos: BytePos) -> BytePos {
919912
let line_index = self.lookup_line(pos).unwrap();
920-
let analysis = self.analyze();
921-
analysis.lines[line_index]
913+
self.lines[line_index]
922914
}
923915

924916
/// Get a line from the list of pre-computed line-beginnings.
@@ -936,8 +928,7 @@ impl SourceFile {
936928
}
937929

938930
let begin = {
939-
let analysis = self.analyze();
940-
let line = analysis.lines.get(line_number)?;
931+
let line = self.lines.get(line_number)?;
941932
let begin: BytePos = *line - self.start_pos;
942933
begin.to_usize()
943934
};
@@ -954,22 +945,20 @@ impl SourceFile {
954945
}
955946

956947
pub fn count_lines(&self) -> usize {
957-
let analysis = self.analyze();
958-
analysis.lines.len()
948+
self.lines.len()
959949
}
960950

961951
/// Find the line containing the given position. The return value is the
962952
/// index into the `lines` array of this SourceFile, not the 1-based line
963953
/// number. If the `source_file` is empty or the position is located before
964954
/// the first line, `None` is returned.
965955
pub fn lookup_line(&self, pos: BytePos) -> Option<usize> {
966-
let analysis = self.analyze();
967-
if analysis.lines.is_empty() {
956+
if self.lines.is_empty() {
968957
return None;
969958
}
970959

971-
let line_index = lookup_line(&analysis.lines, pos);
972-
assert!(line_index < analysis.lines.len() as isize);
960+
let line_index = lookup_line(&self.lines[..], pos);
961+
assert!(line_index < self.lines.len() as isize);
973962
if line_index >= 0 {
974963
Some(line_index as usize)
975964
} else {
@@ -982,32 +971,18 @@ impl SourceFile {
982971
return (self.start_pos, self.end_pos);
983972
}
984973

985-
let analysis = self.analyze();
986-
987-
assert!(line_index < analysis.lines.len());
988-
if line_index == (analysis.lines.len() - 1) {
989-
(analysis.lines[line_index], self.end_pos)
974+
assert!(line_index < self.lines.len());
975+
if line_index == (self.lines.len() - 1) {
976+
(self.lines[line_index], self.end_pos)
990977
} else {
991-
(analysis.lines[line_index], analysis.lines[line_index + 1])
978+
(self.lines[line_index], self.lines[line_index + 1])
992979
}
993980
}
994981

995982
#[inline]
996983
pub fn contains(&self, byte_pos: BytePos) -> bool {
997984
byte_pos >= self.start_pos && byte_pos <= self.end_pos
998985
}
999-
1000-
pub fn analyze(&self) -> &SourceFileAnalysis {
1001-
self.lazy.get_or_init(|| {
1002-
let (lines, multibyte_chars, non_narrow_chars) =
1003-
analyze_source_file::analyze_source_file(&self.src[..], self.start_pos);
1004-
SourceFileAnalysis {
1005-
lines,
1006-
multibyte_chars,
1007-
non_narrow_chars,
1008-
}
1009-
})
1010-
}
1011986
}
1012987

1013988
/// Remove utf-8 BOM if any.

‎crates/swc_error_reporters/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl SourceCode for MietteSourceCode<'_> {
131131
}
132132

133133
let loc = self.0.lookup_char_pos(span.lo());
134-
let line_count = loc.file.analyze().lines.len();
134+
let line_count = loc.file.lines.len();
135135

136136
let name = if self.1.skip_filename {
137137
None

‎crates/swc_estree_compat/src/swcify/ctx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Context {
1212

1313
impl Context {
1414
fn locate_line_col(&self, loc: LineCol) -> BytePos {
15-
if let Some(&line_start) = self.fm.analyze().lines.get(loc.line) {
15+
if let Some(&line_start) = self.fm.lines.get(loc.line) {
1616
line_start + BytePos(loc.column as _)
1717
} else {
1818
BytePos(0)

0 commit comments

Comments
 (0)
Please sign in to comment.