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:
1 parent
7dc8894993
commit
a51ccb5779
7 files changed
+8186
-3
No files matched your search
@@ -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'
|
||||||
|
```
|
||||||
@@ -21,6 +21,18 @@ function clean(original, cloned /* , parent */) {
|
|||||||
delete cloned.documentEndMarker;
|
delete cloned.documentEndMarker;
|
||||||
}
|
}
|
||||||
break;
|
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"]);
|
clean.ignoredProperties = new Set(["position"]);
|
||||||
|
|||||||
@@ -209,6 +209,10 @@ function getBlockValueLineContents(
|
|||||||
// exclude open line `>` or `|`
|
// exclude open line `>` or `|`
|
||||||
.match(/^[^\n]*\n(.*)$/su)[1];
|
.match(/^[^\n]*\n(.*)$/su)[1];
|
||||||
|
|
||||||
|
if (content === "") {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
/** @type {number} */
|
/** @type {number} */
|
||||||
let leadingSpaceCount;
|
let leadingSpaceCount;
|
||||||
if (node.indent === null) {
|
if (node.indent === null) {
|
||||||
@@ -275,14 +279,16 @@ function getBlockValueLineContents(
|
|||||||
*/
|
*/
|
||||||
function removeUnnecessaryTrailingNewlines(lineContents) {
|
function removeUnnecessaryTrailingNewlines(lineContents) {
|
||||||
if (node.chomping === "keep") {
|
if (node.chomping === "keep") {
|
||||||
return lineContents.at(-1).length === 0
|
return content.endsWith("\n") && lineContents.at(-1).length === 0
|
||||||
? lineContents.slice(0, -1)
|
? lineContents.slice(0, -1)
|
||||||
: lineContents;
|
: lineContents;
|
||||||
}
|
}
|
||||||
|
|
||||||
let trailingNewlineCount = 0;
|
let trailingNewlineCount = 0;
|
||||||
for (let i = lineContents.length - 1; i >= 0; i--) {
|
for (let i = lineContents.length - 1; i >= 0; i--) {
|
||||||
if (lineContents[i].length === 0) {
|
if (
|
||||||
|
lineContents[i].every((line) => line.replace(/[ \t]+$/u, "") === "")
|
||||||
|
) {
|
||||||
trailingNewlineCount++;
|
trailingNewlineCount++;
|
||||||
} else {
|
} else {
|
||||||
break;
|
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`] = `
|
exports[`snippet: L94M.yaml - Tags in Explicit Mapping format 1`] = `
|
||||||
====================================options=====================================
|
====================================options=====================================
|
||||||
parsers: ["yaml"]
|
parsers: ["yaml"]
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ const BUGS = new Set([
|
|||||||
"DE56-4.yaml",
|
"DE56-4.yaml",
|
||||||
"JEF9-2.yaml",
|
"JEF9-2.yaml",
|
||||||
"JEF9-3.yaml",
|
"JEF9-3.yaml",
|
||||||
"L24T-2.yaml",
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
runFormatTest(
|
runFormatTest(
|
||||||
|
|||||||
Reference in new issue
Block a user