Skip to content

fix: keep single quotes around strings containing \u{ - #2551

Open
maoxor wants to merge 1 commit into
prettier:mainfrom
maoxor:fix-unicode-escape-quotes
Open

maoxor wants to merge 1 commit into
prettier:mainfrom
maoxor:fix-unicode-escape-quotes

Conversation

@maoxor

@maoxor maoxor commented Oct 29, 2026

Copy link
Copy Markdown

With the default singleQuote: false, a single-quoted string is turned into a double-quoted one when it has no escape sequences. The check only counted a valid \u{...} code point as an escape, but PHP treats any \u{ in a double-quoted string as the start of one and refuses to parse the file when it isn't valid.

PHPUnit's Sanitizer has

static fn (array $matches) => sprintf('\u{%04X}', mb_ord($matches[0], 'UTF-8')),

which was printed as sprintf("\u{%04X}", ...), and php -l then fails with Invalid UTF-8 codepoint escape sequence. Symfony's CliDumper and HtmlDumper hit the same thing with '\u{'.strtoupper(dechex(...)).'}'.

Any \u{ now keeps the string as it is. I added these cases to tests/string-double-quote/quoting.php; the snapshot showed "\u{%04X}", "\u{" and "\u{zz}" before the change. yarn test (including the standalone build), AST_COMPARE=1 yarn test:node and yarn lint pass, and the three files above format to valid PHP again.

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}"`.

@Dextheking1 Dextheking1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked this one. the fix is right: without it the formatter flips the three new cases to double quotes, where they are invalid escapes. ran tests/string-double-quote on this branch, passes. put the old regex back and the snapshot fails on exactly those three cases. looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants