Skip to content

Commit 4b6e0de

Browse files
Copilotsapphi-red
andcommitted
Move validation check to add_source_mapping_end
Move position validation from add_source_mapping() to add_source_mapping_end() as suggested in code review. This is more precise since add_source_mapping_end() is specifically where span.end is used, which is the position that can be beyond source bounds when codegen adds punctuation. Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
1 parent 28744db commit 4b6e0de

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

crates/oxc_codegen/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,16 @@ impl<'a> Codegen<'a> {
889889
if let Some(sourcemap_builder) = self.sourcemap_builder.as_mut()
890890
&& !span.is_empty()
891891
{
892+
// Validate that span.end is within source content bounds.
893+
// When oxc_codegen adds punctuation (semicolons, newlines) that don't exist in the
894+
// original source, span.end may be at or beyond the source content length.
895+
// We should not create sourcemap tokens for such positions as they would be invalid.
896+
if let Some(source_text) = self.source_text {
897+
#[expect(clippy::cast_possible_truncation)]
898+
if span.end >= source_text.len() as u32 {
899+
return;
900+
}
901+
}
892902
sourcemap_builder.add_source_mapping(self.code.as_bytes(), span.end, None);
893903
}
894904
}

crates/oxc_codegen/src/sourcemap_builder.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,6 @@ impl<'a> SourcemapBuilder<'a> {
105105
if self.last_position == Some(position) {
106106
return;
107107
}
108-
109-
// Validate that position is within source content bounds.
110-
// When oxc_codegen adds punctuation (semicolons, newlines) that don't exist in the
111-
// original source, it may call this method with positions beyond the source content.
112-
// We should not create sourcemap tokens for such positions as they would be invalid.
113-
// Position must be < source.len() to be valid (position == source.len() is EOF, beyond content).
114-
#[expect(clippy::cast_possible_truncation)]
115-
if position >= self.original_source.len() as u32 {
116-
return;
117-
}
118-
119108
let (original_line, original_column) = self.search_original_line_and_column(position);
120109
self.update_generated_line_and_column(output);
121110
let name_id = name.map(|s| self.sourcemap_builder.add_name(s));

0 commit comments

Comments
 (0)