Skip to content

Commit

Permalink
fix: OriginalSource in ReplaceSource generated_line is wrong (#99)
Browse files Browse the repository at this point in the history
* fix: typing

* fix: OriginalSource in ReplaceSource generated_line is wrong

* chore: remove unused test

* u

* fix

* fmt

* fix: stream_chunks_of_raw_source
  • Loading branch information
SyMind committed Jun 17, 2024
1 parent a65b388 commit 40dcac4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,12 @@ pub fn stream_chunks_of_raw_source(
last_line.filter(|last_line| !last_line.ends_with('\n'))
{
GeneratedInfo {
generated_line: line,
generated_line: line - 1,
generated_column: last_line.len() as u32,
}
} else {
GeneratedInfo {
generated_line: line + 1,
generated_line: line,
generated_column: 0,
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/original_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ impl StreamChunks for OriginalSource {
last_line.filter(|last_line| !last_line.ends_with('\n'))
{
GeneratedInfo {
generated_line: line,
generated_line: line - 1,
generated_column: last_line.len() as u32,
}
} else {
GeneratedInfo {
generated_line: line + 1,
generated_line: line,
generated_column: 0,
}
}
Expand All @@ -229,6 +229,8 @@ impl StreamChunks for OriginalSource {

#[cfg(test)]
mod tests {
use crate::{ConcatSource, ReplaceSource, SourceExt};

use super::*;

#[test]
Expand Down Expand Up @@ -301,4 +303,19 @@ mod tests {
"AAAA;AACA",
);
}

// Fix https://github.com/web-infra-dev/rspack/issues/6793
#[test]
fn fix_rspack_issue_6793() {
let code1 = "hello\n\n";
let source1 = OriginalSource::new(code1, "hello.txt");
let source1 = ReplaceSource::new(source1);

let code2 = "world";
let source2 = OriginalSource::new(code2, "world.txt");

let concat = ConcatSource::new([source1.boxed(), source2.boxed()]);
let map = concat.map(&MapOptions::new(false)).unwrap();
assert_eq!(map.mappings(), "AAAA;AACA;ACDA",);
}
}
18 changes: 18 additions & 0 deletions src/raw_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,21 @@ impl StreamChunks for RawSource {
}
}
}

#[cfg(test)]
mod tests {
use crate::{ConcatSource, OriginalSource, ReplaceSource, SourceExt};

use super::*;

// Fix https://github.com/web-infra-dev/rspack/issues/6793
#[test]
fn fix_rspack_issue_6793() {
let source1 = RawSource::Source("hello\n\n".to_string());
let source1 = ReplaceSource::new(source1);
let source2 = OriginalSource::new("world".to_string(), "world.txt");
let concat = ConcatSource::new([source1.boxed(), source2.boxed()]);
let map = concat.map(&MapOptions::new(false)).unwrap();
assert_eq!(map.mappings(), ";;AAAA",);
}
}

0 comments on commit 40dcac4

Please sign in to comment.