From e5ddcadd3cca81b7ad52339bb0fa7e95c93340aa Mon Sep 17 00:00:00 2001 From: Marcin Gasperowicz Date: Sun, 25 Oct 2020 14:46:51 +0100 Subject: [PATCH] LibJS: Parse line continuations in string literals properly Newlines after line continuation were inserted into the string literals. This patch makes the parser ignore the newlines after \ and also makes it so that "use strict" containing a line continuation is not a valid "use strict". --- Libraries/LibJS/Tests/functions/function-strict-mode.js | 6 ++++++ Libraries/LibJS/Tests/template-literals.js | 7 +++++++ Libraries/LibJS/Tests/test-common-tests.js | 3 +++ Libraries/LibJS/Token.cpp | 2 ++ 4 files changed, 18 insertions(+) diff --git a/Libraries/LibJS/Tests/functions/function-strict-mode.js b/Libraries/LibJS/Tests/functions/function-strict-mode.js index 85f022603d..08145bf089 100644 --- a/Libraries/LibJS/Tests/functions/function-strict-mode.js +++ b/Libraries/LibJS/Tests/functions/function-strict-mode.js @@ -30,6 +30,12 @@ test("use strict with double quotes after statement does not yield strict mode c expect(isStrictMode()).toBeFalse(); }); +test("use strict interrupted by a line continuation does not yield strict mode code", () => { + "use \ + strict"; + expect(isStrictMode()).toBeFalse(); +}); + test("strict mode propagates down the scope chain", () => { "use strict"; expect(isStrictMode()).toBeTrue(); diff --git a/Libraries/LibJS/Tests/template-literals.js b/Libraries/LibJS/Tests/template-literals.js index 17cb548641..07d81509a8 100644 --- a/Libraries/LibJS/Tests/template-literals.js +++ b/Libraries/LibJS/Tests/template-literals.js @@ -53,6 +53,13 @@ test("newline literals (not characters)", () => { ).toBe("foo\n bar"); }); +test("line continuation in literals (not characters)", () => { + expect( + `foo\ + bar` + ).toBe("foo bar"); +}); + test("reference error from expressions", () => { expect(() => `${b}`).toThrowWithMessage(ReferenceError, "'b' is not defined"); }); diff --git a/Libraries/LibJS/Tests/test-common-tests.js b/Libraries/LibJS/Tests/test-common-tests.js index 31951fffde..a2071f15a0 100644 --- a/Libraries/LibJS/Tests/test-common-tests.js +++ b/Libraries/LibJS/Tests/test-common-tests.js @@ -53,6 +53,9 @@ test("toHaveLength", () => { expect([1]).toHaveLength(1); expect({ length: 1 }).toHaveLength(1); + expect("a\ +b").toHaveLength(2); + expect(() => { expect(1).toHaveLength(); }).toThrow(ExpectationError); diff --git a/Libraries/LibJS/Token.cpp b/Libraries/LibJS/Token.cpp index 57190ef487..89a3b212ac 100644 --- a/Libraries/LibJS/Token.cpp +++ b/Libraries/LibJS/Token.cpp @@ -155,6 +155,8 @@ String Token::string_value(StringValueStatus& status) const case '\\': builder.append('\\'); break; + case '\n': + break; case 'x': { if (i + 2 >= m_value.length() - offset) return encoding_failure(StringValueStatus::MalformedHexEscape);