Fix L24T-2.yaml (value of blockLiteral) bug in yaml-test-suite (#18331)

Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
Konv Suuandfisker authored and GitHub committed 2025-12-05 22:03:45 +08:00
1 parent 7dc8894993
commit a51ccb5779
7 files changed
+8186 -3

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
#### Fix block value ends with whitespace (#18331 by @kovsu)
```js
import { inspect } from "node:util";
import * as prettier from "prettier";
const input = "foo: |\n x\n ";
const output = await prettier.format(input, { parser: "yaml" });
console.log(inspect(output));
// Prettier stable
//-> 'foo: |\n x\n\n'
// Prettier main
//-> 'foo: |\n x\n'
```
+12
View File
@@ -21,6 +21,18 @@ function clean(original, cloned /* , parent */) {
delete cloned.documentEndMarker;
}
break;
case "blockLiteral":
case "blockFolded":
// The doc printer currently have bug when print trailing space/tab, they get wiped.
// But at least we can still make sure the new lines doesn't change in `value` property.
if (original.chomping === "keep") {
cloned.value = original.value
.split("\n")
.map((line) => line.replace(/[ \t]+$/u, ""))
.join("\n");
}
break;
}
}
clean.ignoredProperties = new Set(["position"]);
+8 -2
View File
@@ -209,6 +209,10 @@ function getBlockValueLineContents(
// exclude open line `>` or `|`
.match(/^[^\n]*\n(.*)$/su)[1];
if (content === "") {
return [];
}
/** @type {number} */
let leadingSpaceCount;
if (node.indent === null) {
@@ -275,14 +279,16 @@ function getBlockValueLineContents(
*/
function removeUnnecessaryTrailingNewlines(lineContents) {
if (node.chomping === "keep") {
return lineContents.at(-1).length === 0
return content.endsWith("\n") && lineContents.at(-1).length === 0
? lineContents.slice(0, -1)
: lineContents;
}
let trailingNewlineCount = 0;
for (let i = lineContents.length - 1; i >= 0; i--) {
if (lineContents[i].length === 0) {
if (
lineContents[i].every((line) => line.replace(/[ \t]+$/u, "") === "")
) {
trailingNewlineCount++;
} else {
break;
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,27 @@
const blockStyles = ["|", ">", "|+", "|-", ">+", ">-"];
const trailingSpace = ["", " ", "\t"];
const newlines = ["", "\n", "\n\n", "\n\n\n"];
const snippets = blockStyles.flatMap((blockStyle) =>
trailingSpace.flatMap((space) =>
newlines.flatMap((lines) =>
[
`foo: ${blockStyle}\n${lines}`,
// `foo: ${blockStyle}\n ${space}${lines}`,
`foo: ${blockStyle}\n x\n${lines}`,
`foo: ${blockStyle}\n x\n${space}\n${lines}`,
].flatMap((code) => [code, `${code}\n...`]),
),
),
);
runFormatTest(
{
importMeta: import.meta,
snippets: snippets.map((code) => ({
name: JSON.stringify(code),
code,
})),
},
["yaml"],
);
@@ -4120,6 +4120,22 @@ foo: |
================================================================================
`;
exports[`snippet: L24T-2.yaml - Trailing line of spaces format 1`] = `
====================================options=====================================
parsers: ["yaml"]
printWidth: 80
| printWidth
=====================================input======================================
foo: |
x
=====================================output=====================================
foo: |
x
================================================================================
`;
exports[`snippet: L94M.yaml - Tags in Explicit Mapping format 1`] = `
====================================options=====================================
parsers: ["yaml"]
@@ -15,7 +15,6 @@ const BUGS = new Set([
"DE56-4.yaml",
"JEF9-2.yaml",
"JEF9-3.yaml",
"L24T-2.yaml",
]);
runFormatTest(