Skip to content

Commit 40dcac4

Browse files
authored
fix: OriginalSource in ReplaceSource generated_line is wrong (#99)
* fix: typing * fix: OriginalSource in ReplaceSource generated_line is wrong * chore: remove unused test * u * fix * fmt * fix: stream_chunks_of_raw_source
1 parent a65b388 commit 40dcac4

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,12 @@ pub fn stream_chunks_of_raw_source(
546546
last_line.filter(|last_line| !last_line.ends_with('\n'))
547547
{
548548
GeneratedInfo {
549-
generated_line: line,
549+
generated_line: line - 1,
550550
generated_column: last_line.len() as u32,
551551
}
552552
} else {
553553
GeneratedInfo {
554-
generated_line: line + 1,
554+
generated_line: line,
555555
generated_column: 0,
556556
}
557557
}

src/original_source.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ impl StreamChunks for OriginalSource {
214214
last_line.filter(|last_line| !last_line.ends_with('\n'))
215215
{
216216
GeneratedInfo {
217-
generated_line: line,
217+
generated_line: line - 1,
218218
generated_column: last_line.len() as u32,
219219
}
220220
} else {
221221
GeneratedInfo {
222-
generated_line: line + 1,
222+
generated_line: line,
223223
generated_column: 0,
224224
}
225225
}
@@ -229,6 +229,8 @@ impl StreamChunks for OriginalSource {
229229

230230
#[cfg(test)]
231231
mod tests {
232+
use crate::{ConcatSource, ReplaceSource, SourceExt};
233+
232234
use super::*;
233235

234236
#[test]
@@ -301,4 +303,19 @@ mod tests {
301303
"AAAA;AACA",
302304
);
303305
}
306+
307+
// Fix https://github.com/web-infra-dev/rspack/issues/6793
308+
#[test]
309+
fn fix_rspack_issue_6793() {
310+
let code1 = "hello\n\n";
311+
let source1 = OriginalSource::new(code1, "hello.txt");
312+
let source1 = ReplaceSource::new(source1);
313+
314+
let code2 = "world";
315+
let source2 = OriginalSource::new(code2, "world.txt");
316+
317+
let concat = ConcatSource::new([source1.boxed(), source2.boxed()]);
318+
let map = concat.map(&MapOptions::new(false)).unwrap();
319+
assert_eq!(map.mappings(), "AAAA;AACA;ACDA",);
320+
}
304321
}

src/raw_source.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,21 @@ impl StreamChunks for RawSource {
156156
}
157157
}
158158
}
159+
160+
#[cfg(test)]
161+
mod tests {
162+
use crate::{ConcatSource, OriginalSource, ReplaceSource, SourceExt};
163+
164+
use super::*;
165+
166+
// Fix https://github.com/web-infra-dev/rspack/issues/6793
167+
#[test]
168+
fn fix_rspack_issue_6793() {
169+
let source1 = RawSource::Source("hello\n\n".to_string());
170+
let source1 = ReplaceSource::new(source1);
171+
let source2 = OriginalSource::new("world".to_string(), "world.txt");
172+
let concat = ConcatSource::new([source1.boxed(), source2.boxed()]);
173+
let map = concat.map(&MapOptions::new(false)).unwrap();
174+
assert_eq!(map.mappings(), ";;AAAA",);
175+
}
176+
}

0 commit comments

Comments
 (0)