From eb7f8a8bb495beacb9ea632b22cd500298cfe3b3 Mon Sep 17 00:00:00 2001 From: mansiverma897993 Date: Wed, 17 Jun 2026 23:23:30 +0530 Subject: [PATCH 1/2] fix: support HTML close comment on the first line of script --- core/parser/src/lexer/mod.rs | 5 ++++ .../src/parser/cursor/buffered_lexer/mod.rs | 8 +++-- .../src/parser/cursor/buffered_lexer/tests.rs | 30 +++++++++++++++++++ tests/tester/src/exec/mod.rs | 1 + 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/core/parser/src/lexer/mod.rs b/core/parser/src/lexer/mod.rs index 3e493ea800f..fe5edb18f42 100644 --- a/core/parser/src/lexer/mod.rs +++ b/core/parser/src/lexer/mod.rs @@ -401,6 +401,11 @@ impl Lexer { pub(super) fn take_source(&mut self) -> boa_ast::SourceText { self.cursor.take_source() } + + /// Gets the current linear position of the lexer. + pub(crate) fn linear_pos(&self) -> boa_ast::LinearPosition { + self.cursor.linear_pos() + } } impl<'a> From<&'a [u8]> for Lexer> { diff --git a/core/parser/src/parser/cursor/buffered_lexer/mod.rs b/core/parser/src/parser/cursor/buffered_lexer/mod.rs index 67ba7aee596..ee84acfe535 100644 --- a/core/parser/src/parser/cursor/buffered_lexer/mod.rs +++ b/core/parser/src/parser/cursor/buffered_lexer/mod.rs @@ -133,10 +133,12 @@ where ); let previous_index = self.write_index.checked_sub(1).unwrap_or(PEEK_BUF_SIZE - 1); + let is_start = self.lexer.linear_pos().pos() == 0; + let is_line_terminator = self.peeked[previous_index] + .as_ref() + .is_some_and(|token| token.kind() == &TokenKind::LineTerminator); - if let Some(ref token) = self.peeked[previous_index] - && token.kind() == &TokenKind::LineTerminator - { + if is_start || is_line_terminator { // We don't want to have multiple contiguous line terminators in the buffer, since // they have no meaning. let next = loop { diff --git a/core/parser/src/parser/cursor/buffered_lexer/tests.rs b/core/parser/src/parser/cursor/buffered_lexer/tests.rs index 7dc8c58ef5b..58d04a169cb 100644 --- a/core/parser/src/parser/cursor/buffered_lexer/tests.rs +++ b/core/parser/src/parser/cursor/buffered_lexer/tests.rs @@ -287,3 +287,33 @@ fn issue_1768() { assert!(cur.peek(3, true, interner).unwrap().is_none()); } + +#[test] +#[cfg(feature = "annex-b")] +fn html_close_comment_first_line() { + let mut cur = BufferedLexer::from(&b"--> this is a comment\nthrow"[..]); + let interner = &mut Interner::default(); + + assert_eq!( + *cur.next(false, interner) + .unwrap() + .expect("Some value expected") + .kind(), + TokenKind::Keyword((boa_ast::Keyword::Throw, false)) + ); +} + +#[test] +#[cfg(feature = "annex-b")] +fn html_close_comment_first_line_with_spaces_and_comments() { + let mut cur = BufferedLexer::from(&b" /* comment */ /*another*/--> this is a comment\nthrow"[..]); + let interner = &mut Interner::default(); + + assert_eq!( + *cur.next(false, interner) + .unwrap() + .expect("Some value expected") + .kind(), + TokenKind::Keyword((boa_ast::Keyword::Throw, false)) + ); +} diff --git a/tests/tester/src/exec/mod.rs b/tests/tester/src/exec/mod.rs index 62c0afaf5ad..fa78ffc9f10 100644 --- a/tests/tester/src/exec/mod.rs +++ b/tests/tester/src/exec/mod.rs @@ -602,6 +602,7 @@ fn is_error_type(error: &JsError, target_type: ErrorType, context: &mut Context) JsNativeErrorKind::Reference if target_type == ErrorType::ReferenceError => {} JsNativeErrorKind::Range if target_type == ErrorType::RangeError => {} JsNativeErrorKind::Type if target_type == ErrorType::TypeError => {} + JsNativeErrorKind::Eval if target_type == ErrorType::EvalError => {} _ => return false, } true From 68f6313c16322603046b455d3156099afeedfe9f Mon Sep 17 00:00:00 2001 From: mansiverma897993 Date: Thu, 18 Jun 2026 10:35:59 +0530 Subject: [PATCH 2/2] style: format html_close_comment tests --- core/parser/src/parser/cursor/buffered_lexer/tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/parser/src/parser/cursor/buffered_lexer/tests.rs b/core/parser/src/parser/cursor/buffered_lexer/tests.rs index 58d04a169cb..66c69886a75 100644 --- a/core/parser/src/parser/cursor/buffered_lexer/tests.rs +++ b/core/parser/src/parser/cursor/buffered_lexer/tests.rs @@ -306,7 +306,8 @@ fn html_close_comment_first_line() { #[test] #[cfg(feature = "annex-b")] fn html_close_comment_first_line_with_spaces_and_comments() { - let mut cur = BufferedLexer::from(&b" /* comment */ /*another*/--> this is a comment\nthrow"[..]); + let mut cur = + BufferedLexer::from(&b" /* comment */ /*another*/--> this is a comment\nthrow"[..]); let interner = &mut Interner::default(); assert_eq!(