From 9a5ecbc8e938b3307aba6d8773d09bbd7f06af07 Mon Sep 17 00:00:00 2001 From: Lucas Mirelmann Date: Sat, 16 Mar 2024 13:41:42 +0100 Subject: [PATCH] Properly handle line breaks when looking for directives (#1283) FIX: Properly recognize \"use strict\" when preceded by a string with an escaped newline. --- acorn/src/parseutil.js | 2 +- test/tests-directive.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/acorn/src/parseutil.js b/acorn/src/parseutil.js index efcad6ce9..730642c37 100644 --- a/acorn/src/parseutil.js +++ b/acorn/src/parseutil.js @@ -6,7 +6,7 @@ const pp = Parser.prototype // ## Parser utilities -const literal = /^(?:'((?:\\.|[^'\\])*?)'|"((?:\\.|[^"\\])*?)")/ +const literal = /^(?:'((?:\\.|[^'\\])*?)'|"((?:\\.|[^"\\])*?)")/s pp.strictDirective = function(start) { if (this.options.ecmaVersion < 5) return false for (;;) { diff --git a/test/tests-directive.js b/test/tests-directive.js index 39d9fb18e..eb23c2477 100644 --- a/test/tests-directive.js +++ b/test/tests-directive.js @@ -2,6 +2,7 @@ if (typeof exports !== "undefined") { var driver = require("./driver.js"); var test = driver.test; + var testFail = driver.testFail; } //------------------------------------------------------------------------ @@ -1384,3 +1385,8 @@ test("(a = () => { \"use strict\"; foo }) => { \"use strict\" }", { } ] }, { ecmaVersion: 6 }) + +testFail( + "function invalid() { \"\\7\\\n\"; \"use strict\"; }", + "Octal literal in strict mode (1:22)", + { ecmaVersion: 6 })