From b3edd9486966b8ec611a117335bce676dbc12161 Mon Sep 17 00:00:00 2001 From: davidot Date: Mon, 14 Nov 2022 21:52:58 +0100 Subject: [PATCH] LibJS: Treat '\\' as an escaped character in template literals Before this change we would ignore that the second backslash is escaped and template strings ending with ` \\` would be unterminated as the second slash was used to escape the closing quote. --- Userland/Libraries/LibJS/Lexer.cpp | 9 ++++++++- Userland/Libraries/LibJS/Tests/template-literals.js | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Lexer.cpp b/Userland/Libraries/LibJS/Lexer.cpp index 17dec6c963f..0baad82b588 100644 --- a/Userland/Libraries/LibJS/Lexer.cpp +++ b/Userland/Libraries/LibJS/Lexer.cpp @@ -610,8 +610,15 @@ Token Lexer::next() consume(); m_template_states.last().in_expr = true; } else { + // TemplateCharacter :: + // $ [lookahead ≠ {] + // \ TemplateEscapeSequence + // \ NotEscapeSequence + // LineContinuation + // LineTerminatorSequence + // SourceCharacter but not one of ` or \ or $ or LineTerminator while (!match('$', '{') && m_current_char != '`' && !is_eof()) { - if (match('\\', '$') || match('\\', '`')) + if (match('\\', '$') || match('\\', '`') || match('\\', '\\')) consume(); consume(); } diff --git a/Userland/Libraries/LibJS/Tests/template-literals.js b/Userland/Libraries/LibJS/Tests/template-literals.js index 985be79fa15..5bbe8590cca 100644 --- a/Userland/Libraries/LibJS/Tests/template-literals.js +++ b/Userland/Libraries/LibJS/Tests/template-literals.js @@ -7,6 +7,9 @@ test("plain literals with expression-like characters", () => { test("plain literals with escaped special characters", () => { expect(`foo\``).toBe("foo`"); + expect(`foo\\`).toBe("foo\\"); + expect(`foo\\\``).toBe("foo\\`"); + expect(`foo\\\\`).toBe("foo\\\\"); expect(`foo\$`).toBe("foo$"); expect(`foo \${"bar"}`).toBe('foo ${"bar"}'); });