From b8eff73829572403008bad14ff4be4d47835330b Mon Sep 17 00:00:00 2001 From: mao <118530756+maoxor@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:29:54 +0100 Subject: [PATCH] fix: keep single quotes around strings containing \u{ In a double-quoted string, PHP treats any `\u{` as the start of a code point escape and fails to parse the file when it is not a valid one, so `'\u{%04X}'` must not become `"\u{%04X}"`. --- src/util.mjs | 3 ++- tests/string-double-quote/__snapshots__/jsfmt.spec.mjs.snap | 6 ++++++ tests/string-double-quote/quoting.php | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/util.mjs b/src/util.mjs index 8a4aa944d..d97087240 100644 --- a/src/util.mjs +++ b/src/util.mjs @@ -538,8 +538,9 @@ function useDoubleQuote(node, options) { if (node.isDoubleQuote === options.singleQuote) { // We have a double quote and the user passed singleQuote:true, or the other way around. const rawValue = node.raw.slice(node.raw[0] === "b" ? 2 : 1, -1); + // In double quotes, PHP reads any `\u{` as a code point escape and rejects an invalid one. const isComplex = rawValue.match( - /\\([$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}|u{([0-9a-fA-F]+)})|\r?\n|'|"|\$/ + /\\([$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}|u{)|\r?\n|'|"|\$/ ); return node.isDoubleQuote ? isComplex : !isComplex; } diff --git a/tests/string-double-quote/__snapshots__/jsfmt.spec.mjs.snap b/tests/string-double-quote/__snapshots__/jsfmt.spec.mjs.snap index 451d79ebd..848bcfbfa 100644 --- a/tests/string-double-quote/__snapshots__/jsfmt.spec.mjs.snap +++ b/tests/string-double-quote/__snapshots__/jsfmt.spec.mjs.snap @@ -34,6 +34,9 @@ singleQuote: false '\\u{aa}'; '\\u{0000aa}'; '\\u{9999}'; +'\\u{%04X}'; +'\\u{'; +'\\u{zz}'; 'multi line'; '\\\\'; @@ -73,6 +76,9 @@ b'binary string\\n'; '\\u{aa}'; '\\u{0000aa}'; '\\u{9999}'; +'\\u{%04X}'; +'\\u{'; +'\\u{zz}'; 'multi line'; "\\\\"; diff --git a/tests/string-double-quote/quoting.php b/tests/string-double-quote/quoting.php index eab1fee4b..38b4d41d1 100644 --- a/tests/string-double-quote/quoting.php +++ b/tests/string-double-quote/quoting.php @@ -25,6 +25,9 @@ '\u{aa}'; '\u{0000aa}'; '\u{9999}'; +'\u{%04X}'; +'\u{'; +'\u{zz}'; 'multi line'; '\\';