Stabilize XML declaration formatting on reformat#1009
Open
sarathfrancis90 wants to merge 1 commit into
Open
Conversation
The XMLDeclOpen token ("<?xml") carries the single whitespace character
that follows it, since its lexer pattern is /<\?xml[ \t\r\n]/. Printing that
character verbatim is not stable: when the declaration wraps onto multiple
lines, the captured character is a newline, and re-formatting then renders an
extra blank line right after "<?xml". As a result prettier's own output of a
wrapped declaration is not idempotent and fails prettier --check.
Print a normalized "<?xml" and let the surrounding layout provide the
spacing, so a wrapped declaration round-trips unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Running the plugin on its own output isn't stable when the XML declaration wraps. If a
<?xml … ?>declaration is long enough (orprintWidthsmall enough) to break onto multiple lines, the first pass producesbut a second pass inserts a blank line right after
<?xml:So formatted files don't pass
prettier --check.The cause is in
printProlog: theXMLDeclOpentoken's image isn't just<?xml— its lexer pattern is/<\?xml[ \t\r\n]/, so it also carries the one whitespace character that follows. We print that character verbatim. On a wrapped declaration that character is a newline, and printing it ahead of the line break that begins the attributes yields the extra blank line.I print a normalized
<?xmland let the existing layout supply the spacing (the leadingsoftlinebecomes alineso the single-line case keeps its space). The common single-line output is unchanged, and a wrapped declaration now round-trips. Added a test that formats a wrapped declaration twice and asserts the second pass is a no-op.