Repository: swc-project/swc
File: crates/swc_ecma_parser/src/lexer/mod.rs (Rust)

Code before:
                        let bytes = self.input().as_str().as_bytes();
                        if bytes.get(pos_offset + 1) == Some(&b'/') {
                            // Consume "*/"
                            pos_offset += 2;

                            // Decide trailing / leading
                            let mut is_for_next =
                                had_line_break_before_last || !self.state().can_have_trailing_comment();

                            // If next char is ';' without newline, treat as trailing
                            if !had_line_break_before_last && bytes.get(pos_offset) == Some(&b';') {
                                is_for_next = false;
                            }

                            if self.comments_buffer().is_some() {
                                let s = unsafe {
                                    let cur = self.input().cur_pos();
                                    // Safety: We got slice_start and end from self.input so those are
                                    // valid.
                                    let s = self.input_mut().slice(slice_start, BytePos((pos_offset - 2) as u32));
                                    self.input_mut().reset_to(cur);
                                    s
                                };
                                let cmt = Comment {
                                    kind: CommentKind::Block,
                                    span: Span::new_with_checked(start, BytePos(pos_offset as u32)),
                                    text: self.atom(s),
                                };

                                if is_for_next {
                                    self.comments_buffer_mut().unwrap().push_pending(cmt);
                                } else {
                                    let pos = self.state().prev_hi();
                                    self.comments_buffer_mut()
                                        .unwrap()
                                        .push_comment(BufferedComment {
                                            kind: BufferedCommentKind::Trailing,
                                            pos,
                                            comment: cmt,
                                        });
                                }
                            }

                            false
                        } else {
                            true
                        }
                    }
                    _ => {
                        self.state_mut().mark_had_line_break();
                        true

Review comment (bug):
The span end position is incorrectly calculated. `pos_offset` is a relative offset from the current position (which is at `slice_start`), not an absolute byte position. This should be `slice_start + BytePos(pos_offset as u32)` to get the correct absolute end position of the comment span.
```suggestion
                                    span: Span::new_with_checked(start, slice_start + BytePos(pos_offset as u32)),
```

Code after:
                        let bytes = self.input().as_str().as_bytes();
                        if bytes.get(pos_offset + 1) == Some(&b'/') {
                            // Consume "*/"
                            pos_offset += 2;

                            // Decide trailing / leading
                            let mut is_for_next =
                                had_line_break_before_last || !self.state().can_have_trailing_comment();

                            // If next char is ';' without newline, treat as trailing
                            if !had_line_break_before_last && bytes.get(pos_offset) == Some(&b';') {
                                is_for_next = false;
                            }

                            if self.comments_buffer().is_some() {
                                let s = unsafe {
                                    let cur = self.input().cur_pos();
                                    // Safety: We got slice_start and end from self.input so those are
                                    // valid.
                                    let s = self.input_mut().slice(slice_start, slice_start + BytePos((pos_offset - 2) as u32));
                                    self.input_mut().reset_to(cur);
                                    s
                                };
                                let cmt = Comment {
                                    kind: CommentKind::Block,
                                    span: Span::new_with_checked(start, slice_start + BytePos(pos_offset as u32)),
                                    text: self.atom(s),
                                };

                                if is_for_next {
                                    self.comments_buffer_mut().unwrap().push_pending(cmt);
                                } else {
                                    let pos = self.state().prev_hi();
                                    self.comments_buffer_mut()
                                        .unwrap()
                                        .push_comment(BufferedComment {
                                            kind: BufferedCommentKind::Trailing,
                                            pos,
                                            comment: cmt,
                                        });
                                }
                            }

                            false
                        } else {
                            true
                        }
                    }
                    _ => {
                        self.state_mut().mark_had_line_break();
                        true