Skip to content

Commit

Permalink
fix(sourcemap): avoid negative line if token_chunks has same prev_dst…
Browse files Browse the repository at this point in the history
…_line (#4348)
  • Loading branch information
underfin committed Jul 19, 2024
1 parent 58f6ec2 commit 4cd5df0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
26 changes: 21 additions & 5 deletions crates/oxc_sourcemap/src/concat_sourcemap_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl ConcatSourceMapBuilder {
}
}

#[cfg(feature = "concurrent")]
#[test]
fn test_concat_sourcemap_builder() {
let sm1 = SourceMap::new(
Expand All @@ -108,18 +109,32 @@ fn test_concat_sourcemap_builder() {
vec![Token::new(1, 1, 1, 1, Some(0), Some(0))],
None,
);
let sm3 = SourceMap::new(
None,
vec!["abc".into()],
None,
vec!["abc.js".into()],
None,
vec![Token::new(1, 2, 2, 2, Some(0), Some(0))],
None,
);

let mut builder = ConcatSourceMapBuilder::default();
builder.add_sourcemap(&sm1, 0);
builder.add_sourcemap(&sm2, 2);
builder.add_sourcemap(&sm3, 2);

let sm = SourceMap::new(
None,
vec!["foo".into(), "foo2".into(), "bar".into()],
vec!["foo".into(), "foo2".into(), "bar".into(), "abc".into()],
None,
vec!["foo.js".into(), "bar.js".into()],
vec!["foo.js".into(), "bar.js".into(), "abc.js".into()],
None,
vec![Token::new(1, 1, 1, 1, Some(0), Some(0)), Token::new(3, 1, 1, 1, Some(1), Some(2))],
vec![
Token::new(1, 1, 1, 1, Some(0), Some(0)),
Token::new(3, 1, 1, 1, Some(1), Some(2)),
Token::new(3, 2, 2, 2, Some(2), Some(3)),
],
None,
);
let concat_sm = builder.into_sourcemap();
Expand All @@ -131,9 +146,10 @@ fn test_concat_sourcemap_builder() {
concat_sm.token_chunks,
Some(vec![
TokenChunk::new(0, 1, 0, 0, 0, 0, 0, 0,),
TokenChunk::new(1, 2, 1, 1, 1, 1, 0, 0,)
TokenChunk::new(1, 2, 1, 1, 1, 1, 0, 0,),
TokenChunk::new(2, 3, 3, 1, 1, 1, 2, 1,)
])
);

assert_eq!(sm.to_json_string().unwrap(), sm.to_json_string().unwrap());
assert_eq!(sm.to_json().mappings, concat_sm.to_json().mappings);
}
5 changes: 3 additions & 2 deletions crates/oxc_sourcemap/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,15 @@ fn serialize_mappings(tokens: &[Token], token_chunk: &TokenChunk) -> String {
} = *token_chunk;

for (idx, token) in tokens[start as usize..end as usize].iter().enumerate() {
let index = start as usize + idx;
if token.get_dst_line() != prev_dst_line {
prev_dst_col = 0;
while token.get_dst_line() != prev_dst_line {
rv.push(';');
prev_dst_line += 1;
}
} else if idx > 0 {
if Some(token) == tokens.get(idx - 1) {
} else if index > 0 {
if Some(token) == tokens.get(index - 1) {
continue;
}
rv.push(',');
Expand Down

0 comments on commit 4cd5df0

Please sign in to comment.