LibJS: Emit TokenType::Invalid for unterminated multi-line comments

This commit is contained in:
Linus Groh 2020-10-26 20:10:50 +00:00 committed by Andreas Kling
parent 03c1d43f6e
commit 19edcbd79c
Notes: sideshowbarker 2024-07-19 01:41:36 +09:00
2 changed files with 19 additions and 1 deletions

View file

@ -362,6 +362,7 @@ Token Lexer::next()
{
size_t trivia_start = m_position;
auto in_template = !m_template_states.is_empty();
bool unterminated_comment = false;
if (!in_template || m_template_states.last().in_expr) {
// consume whitespace and comments
@ -380,7 +381,11 @@ Token Lexer::next()
do {
consume();
} while (!is_eof() && !is_block_comment_end());
if (is_eof())
unterminated_comment = true;
consume(); // consume *
if (is_eof())
unterminated_comment = true;
consume(); // consume /
} else {
break;
@ -542,7 +547,12 @@ Token Lexer::next()
consume();
}
} else if (m_current_char == EOF) {
token_type = TokenType::Eof;
if (unterminated_comment) {
token_type = TokenType::Invalid;
token_message = "Unterminated multi-line comment";
} else {
token_type = TokenType::Eof;
}
} else {
// There is only one four-char operator: >>>=
bool found_four_char_token = false;

View file

@ -21,3 +21,11 @@ return i;`;
expect(source).toEvalTo(1);
});
test("unterminated multi-line comment", () => {
expect("/*").not.toEval();
expect("/**").not.toEval();
expect("/*/").not.toEval();
expect("/* foo").not.toEval();
expect("foo /*").not.toEval();
});