Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,16 @@ function printMisc(path, opts, print) {

function printProlog(path, opts, print) {
const { XMLDeclOpen, attribute, SPECIAL_CLOSE } = path.node;
const parts = [XMLDeclOpen];

// The XMLDeclOpen token includes the single whitespace character that follows
// "<?xml" (its pattern is /<\?xml[ \t\r\n]/). Printing that trailing
// whitespace verbatim is not stable: when the declaration breaks onto
// multiple lines a captured newline turns into an extra blank line on the
// next pass. Drop it and let the layout below supply the spacing.
const parts = ["<?xml"];

if (attribute) {
parts.push(indent([softline, join(line, path.map(print, "attribute"))]));
parts.push(indent([line, join(line, path.map(print, "attribute"))]));
}

return group([
Expand Down
10 changes: 10 additions & 0 deletions test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,13 @@ test("xmlQuoteAttributes => double", async () => {

expect(formatted).toMatchSnapshot();
});

test("wrapped XML declaration is stable on reformat", async () => {
const input = '<?xml version="1.0" encoding="UTF-8"?>\n<root />\n';
const opts = { printWidth: 20 };

const once = await format(input, opts);
const twice = await format(once, opts);

expect(twice).toEqual(once);
});