Skip to content

Commit 4cd5df0

Browse files
authored
fix(sourcemap): avoid negative line if token_chunks has same prev_dst_line (#4348)
1 parent 58f6ec2 commit 4cd5df0

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

crates/oxc_sourcemap/src/concat_sourcemap_builder.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl ConcatSourceMapBuilder {
8888
}
8989
}
9090

91+
#[cfg(feature = "concurrent")]
9192
#[test]
9293
fn test_concat_sourcemap_builder() {
9394
let sm1 = SourceMap::new(
@@ -108,18 +109,32 @@ fn test_concat_sourcemap_builder() {
108109
vec![Token::new(1, 1, 1, 1, Some(0), Some(0))],
109110
None,
110111
);
112+
let sm3 = SourceMap::new(
113+
None,
114+
vec!["abc".into()],
115+
None,
116+
vec!["abc.js".into()],
117+
None,
118+
vec![Token::new(1, 2, 2, 2, Some(0), Some(0))],
119+
None,
120+
);
111121

112122
let mut builder = ConcatSourceMapBuilder::default();
113123
builder.add_sourcemap(&sm1, 0);
114124
builder.add_sourcemap(&sm2, 2);
125+
builder.add_sourcemap(&sm3, 2);
115126

116127
let sm = SourceMap::new(
117128
None,
118-
vec!["foo".into(), "foo2".into(), "bar".into()],
129+
vec!["foo".into(), "foo2".into(), "bar".into(), "abc".into()],
119130
None,
120-
vec!["foo.js".into(), "bar.js".into()],
131+
vec!["foo.js".into(), "bar.js".into(), "abc.js".into()],
121132
None,
122-
vec![Token::new(1, 1, 1, 1, Some(0), Some(0)), Token::new(3, 1, 1, 1, Some(1), Some(2))],
133+
vec![
134+
Token::new(1, 1, 1, 1, Some(0), Some(0)),
135+
Token::new(3, 1, 1, 1, Some(1), Some(2)),
136+
Token::new(3, 2, 2, 2, Some(2), Some(3)),
137+
],
123138
None,
124139
);
125140
let concat_sm = builder.into_sourcemap();
@@ -131,9 +146,10 @@ fn test_concat_sourcemap_builder() {
131146
concat_sm.token_chunks,
132147
Some(vec![
133148
TokenChunk::new(0, 1, 0, 0, 0, 0, 0, 0,),
134-
TokenChunk::new(1, 2, 1, 1, 1, 1, 0, 0,)
149+
TokenChunk::new(1, 2, 1, 1, 1, 1, 0, 0,),
150+
TokenChunk::new(2, 3, 3, 1, 1, 1, 2, 1,)
135151
])
136152
);
137153

138-
assert_eq!(sm.to_json_string().unwrap(), sm.to_json_string().unwrap());
154+
assert_eq!(sm.to_json().mappings, concat_sm.to_json().mappings);
139155
}

crates/oxc_sourcemap/src/encode.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,15 @@ fn serialize_mappings(tokens: &[Token], token_chunk: &TokenChunk) -> String {
129129
} = *token_chunk;
130130

131131
for (idx, token) in tokens[start as usize..end as usize].iter().enumerate() {
132+
let index = start as usize + idx;
132133
if token.get_dst_line() != prev_dst_line {
133134
prev_dst_col = 0;
134135
while token.get_dst_line() != prev_dst_line {
135136
rv.push(';');
136137
prev_dst_line += 1;
137138
}
138-
} else if idx > 0 {
139-
if Some(token) == tokens.get(idx - 1) {
139+
} else if index > 0 {
140+
if Some(token) == tokens.get(index - 1) {
140141
continue;
141142
}
142143
rv.push(',');

0 commit comments

Comments
 (0)