Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion core/parser/src/parser/cursor/buffered_lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(super) struct BufferedLexer<R> {
read_index: usize,
write_index: usize,
last_linear_pos: LinearPosition,
first_token: bool,
}

impl<R> From<Lexer<R>> for BufferedLexer<R>
Expand All @@ -53,6 +54,7 @@ where
read_index: 0,
write_index: 0,
last_linear_pos: LinearPosition::default(),
first_token: true,
}
}
}
Expand Down Expand Up @@ -134,7 +136,23 @@ where

let previous_index = self.write_index.checked_sub(1).unwrap_or(PEEK_BUF_SIZE - 1);

if let Some(ref token) = self.peeked[previous_index]
if self.first_token {
self.first_token = false;
let next = loop {
self.lexer.skip_html_close(interner)?;
let next = self.lexer.next_no_skip(interner)?;
if let Some(ref token) = next {
match token.kind() {
TokenKind::Comment => self.lexer.skip_html_close(interner)?,
_ => break next,
}
} else {
break None;
}
};

self.peeked[self.write_index] = next;
} else if let Some(ref token) = self.peeked[previous_index]
&& token.kind() == &TokenKind::LineTerminator
{
// We don't want to have multiple contiguous line terminators in the buffer, since
Expand Down
36 changes: 36 additions & 0 deletions core/parser/src/parser/cursor/buffered_lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,39 @@ fn issue_1768() {

assert!(cur.peek(3, true, interner).unwrap().is_none());
}

#[test]
#[cfg(feature = "annex-b")]
fn html_close_comment_first_line() {
let interner = &mut Interner::default();

// 1. Direct `-->` on the first line.
let mut cur = BufferedLexer::from(&b"--> comment\nA"[..]);
assert_eq!(
*cur.peek(0, true, interner)
.unwrap()
.expect("Some value expected")
.kind(),
TokenKind::identifier(interner.get_or_intern_static("A", utf16!("A")))
);

// 2. Spaces preceding `-->` on the first line.
let mut cur = BufferedLexer::from(&b" --> comment\nB"[..]);
assert_eq!(
*cur.peek(0, true, interner)
.unwrap()
.expect("Some value expected")
.kind(),
TokenKind::identifier(interner.get_or_intern_static("B", utf16!("B")))
);

// 3. Single-line delimited comments and spaces preceding `-->` on the first line.
let mut cur = BufferedLexer::from(&b"/* comment 1 */ /* comment 2 */--> comment 3\nC"[..]);
assert_eq!(
*cur.peek(0, true, interner)
.unwrap()
.expect("Some value expected")
.kind(),
TokenKind::identifier(interner.get_or_intern_static("C", utf16!("C")))
);
}
1 change: 1 addition & 0 deletions tests/tester/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down