Skip to content

Commit 961cc45

Browse files
committed
chore: merge origin main
2 parents 6ad4e2d + 920711b commit 961cc45

File tree

6 files changed

+103
-297
lines changed

6 files changed

+103
-297
lines changed

src/concat_source.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hash::FxHashMap as HashMap;
88

99
use crate::{
1010
helpers::{get_map, GeneratedInfo, OnChunk, OnName, OnSource, StreamChunks},
11+
linear_map::LinearMap,
1112
source::{Mapping, OriginalLocation},
1213
BoxSource, MapOptions, Source, SourceExt, SourceMap,
1314
};
@@ -163,10 +164,10 @@ impl<'a> StreamChunks<'a> for ConcatSource {
163164
let mut name_mapping: HashMap<Cow<str>, u32> = HashMap::default();
164165
let mut need_to_close_mapping = false;
165166

166-
let source_index_mapping: RefCell<HashMap<u32, u32>> =
167-
RefCell::new(HashMap::default());
168-
let name_index_mapping: RefCell<HashMap<u32, u32>> =
169-
RefCell::new(HashMap::default());
167+
let source_index_mapping: RefCell<LinearMap<u32>> =
168+
RefCell::new(LinearMap::default());
169+
let name_index_mapping: RefCell<LinearMap<u32>> =
170+
RefCell::new(LinearMap::default());
170171

171172
for item in self.children() {
172173
source_index_mapping.borrow_mut().clear();

src/helpers.rs

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use arrayvec::ArrayVec;
2-
use rustc_hash::FxHashMap as HashMap;
32
use std::{
43
borrow::{BorrowMut, Cow},
54
cell::RefCell,
65
rc::Rc,
76
};
87

8+
use rustc_hash::FxHashMap as HashMap;
9+
910
use crate::{
1011
encoder::create_encoder,
12+
linear_map::LinearMap,
1113
source::{Mapping, OriginalLocation},
1214
vlq::decode,
1315
with_indices::WithIndices,
@@ -16,7 +18,7 @@ use crate::{
1618

1719
// Adding this type because sourceContentLine not happy
1820
type InnerSourceContentLine<'a> =
19-
RefCell<HashMap<i64, Option<Rc<Vec<WithIndices<&'a str>>>>>>;
21+
RefCell<HashMap<u32, Option<Rc<Vec<WithIndices<&'a str>>>>>>;
2022

2123
pub fn get_map<'a, S: StreamChunks<'a>>(
2224
stream: &'a S,
@@ -747,7 +749,7 @@ struct SourceMapLineData<'a> {
747749
}
748750

749751
type InnerSourceIndexValueMapping<'a> =
750-
HashMap<i64, (Cow<'a, str>, Option<&'a str>)>;
752+
LinearMap<(Cow<'a, str>, Option<&'a str>)>;
751753

752754
#[allow(clippy::too_many_arguments)]
753755
pub fn stream_chunks_of_combined_source_map<'a>(
@@ -767,27 +769,28 @@ pub fn stream_chunks_of_combined_source_map<'a>(
767769
let source_mapping: RefCell<HashMap<Cow<str>, u32>> =
768770
RefCell::new(HashMap::default());
769771
let mut name_mapping: HashMap<Cow<str>, u32> = HashMap::default();
770-
let source_index_mapping: RefCell<HashMap<i64, i64>> =
771-
RefCell::new(HashMap::default());
772-
let name_index_mapping: RefCell<HashMap<i64, i64>> =
773-
RefCell::new(HashMap::default());
774-
let name_index_value_mapping: RefCell<HashMap<i64, Cow<str>>> =
775-
RefCell::new(HashMap::default());
772+
let source_index_mapping: RefCell<LinearMap<i64>> =
773+
RefCell::new(LinearMap::default());
774+
let name_index_mapping: RefCell<LinearMap<i64>> =
775+
RefCell::new(LinearMap::default());
776+
let name_index_value_mapping: RefCell<LinearMap<Cow<str>>> =
777+
RefCell::new(LinearMap::default());
776778
let inner_source_index: RefCell<i64> = RefCell::new(-2);
777-
let inner_source_index_mapping: RefCell<HashMap<i64, i64>> =
778-
RefCell::new(HashMap::default());
779+
let inner_source_index_mapping: RefCell<LinearMap<i64>> =
780+
RefCell::new(LinearMap::default());
779781
let inner_source_index_value_mapping: RefCell<InnerSourceIndexValueMapping> =
780-
RefCell::new(HashMap::default());
781-
let inner_source_contents: RefCell<HashMap<i64, Option<&str>>> =
782-
RefCell::new(HashMap::default());
782+
RefCell::new(LinearMap::default());
783+
let inner_source_contents: RefCell<LinearMap<Option<&str>>> =
784+
RefCell::new(LinearMap::default());
783785
let inner_source_content_lines: InnerSourceContentLine =
784786
RefCell::new(HashMap::default());
785-
let inner_name_index_mapping: RefCell<HashMap<i64, i64>> =
786-
RefCell::new(HashMap::default());
787-
let inner_name_index_value_mapping: RefCell<HashMap<i64, Cow<str>>> =
788-
RefCell::new(HashMap::default());
787+
let inner_name_index_mapping: RefCell<LinearMap<i64>> =
788+
RefCell::new(LinearMap::default());
789+
let inner_name_index_value_mapping: RefCell<LinearMap<Cow<str>>> =
790+
RefCell::new(LinearMap::default());
789791
let inner_source_map_line_data: RefCell<Vec<SourceMapLineData>> =
790792
RefCell::new(Vec::new());
793+
791794
let find_inner_mapping = |line: i64, column: i64| -> Option<u32> {
792795
let inner_source_map_line_data = inner_source_map_line_data.borrow();
793796
if line as usize > inner_source_map_line_data.len() {
@@ -835,6 +838,8 @@ pub fn stream_chunks_of_combined_source_map<'a>(
835838

836839
// Check if this is a mapping to the inner source
837840
if source_index == *inner_source_index.borrow() {
841+
let source_index = source_index as u32;
842+
838843
// Check if there is a mapping in the inner source
839844
if let Some(idx) = find_inner_mapping(original_line, original_column) {
840845
let idx = idx as usize;
@@ -848,6 +853,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
848853
let mut inner_original_column = mappings_data[mi + 3];
849854
let mut inner_name_index = mappings_data[mi + 4];
850855
if inner_source_index >= 0 {
856+
let inner_source_index = inner_source_index as u32;
851857
// Check for an identity mapping
852858
// where we are allowed to adjust the original column
853859
let inner_chunk = &chunks[idx];
@@ -924,6 +930,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
924930
// emit name when needed and compute global name index
925931
let mut final_name_index = -1;
926932
if inner_name_index >= 0 {
933+
let inner_name_index = inner_name_index as u32;
927934
// when we have a inner name
928935
let mut inner_name_index_mapping =
929936
inner_name_index_mapping.borrow_mut();
@@ -951,6 +958,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
951958
.insert(inner_name_index, final_name_index);
952959
}
953960
} else if name_index >= 0 {
961+
let name_index = name_index as u32;
954962
// when we don't have an inner name,
955963
// but we have an outer name
956964
// it can be used when inner original code equals to the name
@@ -1065,11 +1073,16 @@ pub fn stream_chunks_of_combined_source_map<'a>(
10651073
}
10661074
}
10671075

1068-
let final_source_index = source_index_mapping
1069-
.borrow()
1070-
.get(&source_index)
1071-
.copied()
1072-
.unwrap_or(-1);
1076+
let final_source_index = if source_index < 0 {
1077+
-1
1078+
} else {
1079+
let source_index = source_index as u32;
1080+
source_index_mapping
1081+
.borrow()
1082+
.get(&source_index)
1083+
.copied()
1084+
.unwrap_or(-1)
1085+
};
10731086
if final_source_index < 0 {
10741087
// no source, so we make it a generated chunk
10751088
on_chunk(
@@ -1083,9 +1096,14 @@ pub fn stream_chunks_of_combined_source_map<'a>(
10831096
} else {
10841097
// Pass through the chunk with mapping
10851098
let mut name_index_mapping = name_index_mapping.borrow_mut();
1086-
let mut final_name_index =
1087-
name_index_mapping.get(&name_index).copied().unwrap_or(-1);
1099+
let mut final_name_index = if name_index >= 0 {
1100+
let name_index = name_index as u32;
1101+
name_index_mapping.get(&name_index).copied().unwrap_or(-1)
1102+
} else {
1103+
-1
1104+
};
10881105
if final_name_index == -2 {
1106+
let name_index = name_index as u32;
10891107
let name_index_value_mapping = name_index_value_mapping.borrow();
10901108
let name = name_index_value_mapping.get(&name_index).unwrap();
10911109
let mut global_index = name_mapping.get(name).copied();
@@ -1115,9 +1133,8 @@ pub fn stream_chunks_of_combined_source_map<'a>(
11151133
}
11161134
},
11171135
&mut |i, source, mut source_content| {
1118-
let i = i as i64;
11191136
if source == inner_source_name {
1120-
*inner_source_index.borrow_mut() = i;
1137+
*inner_source_index.borrow_mut() = i as i64;
11211138
let mut inner_source = inner_source.borrow_mut();
11221139
if let Some(inner_source) = inner_source.as_ref() {
11231140
source_content = Some(inner_source);
@@ -1181,7 +1198,6 @@ pub fn stream_chunks_of_combined_source_map<'a>(
11811198
data.chunks.push(chunk);
11821199
},
11831200
&mut |i, source, source_content| {
1184-
let i = i as i64;
11851201
inner_source_contents
11861202
.borrow_mut()
11871203
.insert(i, source_content.map(Into::into));
@@ -1192,7 +1208,6 @@ pub fn stream_chunks_of_combined_source_map<'a>(
11921208
.insert(i, (source, source_content));
11931209
},
11941210
&mut |i, name| {
1195-
let i = i as i64;
11961211
inner_name_index_mapping.borrow_mut().insert(i, -2);
11971212
inner_name_index_value_mapping.borrow_mut().insert(i, name);
11981213
},
@@ -1216,7 +1231,6 @@ pub fn stream_chunks_of_combined_source_map<'a>(
12161231
}
12171232
},
12181233
&mut |i, name| {
1219-
let i = i as i64;
12201234
name_index_mapping.borrow_mut().insert(i, -2);
12211235
name_index_value_mapping.borrow_mut().insert(i, name);
12221236
},

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod concat_source;
55
mod encoder;
66
mod error;
77
mod helpers;
8+
mod linear_map;
89
mod original_source;
910
mod raw_source;
1011
mod replace_source;

src/linear_map.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
pub struct LinearMap<V: Default> {
2+
inner: Vec<V>,
3+
}
4+
5+
impl<V: Default> LinearMap<V> {
6+
pub fn new() -> Self {
7+
Self {
8+
inner: Default::default(),
9+
}
10+
}
11+
12+
pub fn get(&self, key: &u32) -> Option<&V> {
13+
self.inner.get(*key as usize)
14+
}
15+
16+
pub fn insert(&mut self, key: u32, value: V) {
17+
let key = key as usize;
18+
if key >= self.inner.len() {
19+
self.inner.resize_with(key + 1, Default::default);
20+
}
21+
self.inner[key] = value;
22+
}
23+
24+
pub fn clear(&mut self) {
25+
self.inner.clear()
26+
}
27+
}
28+
29+
impl<V: Default> Default for LinearMap<V> {
30+
fn default() -> Self {
31+
Self::new()
32+
}
33+
}

src/replace_source.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_hash::FxHashMap as HashMap;
1212

1313
use crate::{
1414
helpers::{get_map, split_into_lines, GeneratedInfo, StreamChunks},
15+
linear_map::LinearMap,
1516
MapOptions, Mapping, OriginalLocation, Source, SourceMap,
1617
};
1718

@@ -258,8 +259,8 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
258259
let mut generated_line_offset: i64 = 0;
259260
let mut generated_column_offset: i64 = 0;
260261
let mut generated_column_offset_line = 0;
261-
let source_content_lines: RefCell<Vec<Option<Vec<&str>>>> =
262-
RefCell::new(Vec::new());
262+
let source_content_lines: RefCell<LinearMap<Option<Vec<&str>>>> =
263+
RefCell::new(LinearMap::default());
263264
let name_mapping: RefCell<HashMap<Cow<str>, u32>> =
264265
RefCell::new(HashMap::default());
265266
let name_index_mapping: RefCell<HashMap<u32, u32>> =
@@ -294,7 +295,7 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
294295
let check_original_content =
295296
|source_index: u32, line: u32, column: u32, expected_chunk: &str| {
296297
if let Some(Some(content_lines)) =
297-
source_content_lines.borrow().get(source_index as usize)
298+
source_content_lines.borrow().get(&source_index)
298299
{
299300
if let Some(content_line) = content_lines.get(line as usize - 1) {
300301
match content_line
@@ -585,11 +586,9 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
585586
},
586587
&mut |source_index, source, source_content| {
587588
let mut source_content_lines = source_content_lines.borrow_mut();
588-
while source_content_lines.len() <= source_index as usize {
589-
source_content_lines.push(None);
590-
}
591-
source_content_lines[source_index as usize] = source_content
589+
let lines = source_content
592590
.map(|source_content| split_into_lines(source_content).collect());
591+
source_content_lines.insert(source_index, lines);
593592
on_source(source_index, source, source_content);
594593
},
595594
&mut |name_index, name| {

0 commit comments

Comments
 (0)