YAML: Fix printing doubles a blank line before a comment (#9143)
* Print hardline for endComments only when under mappingValue * Add changelog * Fix from linter * Update tests * Use shouldPrintEndCommentsWithAlign * Add tests * Add test * Fix format * Update test * Test map too * Update tests * `shouldPrintEndCommentsWithAlign` -> `shouldPrintEndComments` Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
1 parent
ab9474eacc
commit
ec1b419307
7 files changed
+322
-3
No files matched your search
@@ -0,0 +1,27 @@
|
||||
#### Fix printing doubles a blank line before a comment (#9143 by @sosukesuzuki)
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```yaml
|
||||
# Input
|
||||
- foo: 0
|
||||
bar: 1
|
||||
|
||||
# baz: 2
|
||||
- quux: 3
|
||||
|
||||
# Prettier stable
|
||||
- foo: 0
|
||||
bar: 1
|
||||
|
||||
|
||||
# baz: 2
|
||||
- quux: 3
|
||||
|
||||
# Prettier master
|
||||
- foo: 0
|
||||
bar: 1
|
||||
|
||||
# baz: 2
|
||||
- quux: 3
|
||||
|
||||
```
|
||||
@@ -127,8 +127,7 @@ function genericPrint(path, options, print) {
|
||||
])
|
||||
)
|
||||
: "",
|
||||
nextEmptyLine,
|
||||
hasEndComments(node) && !isNode(node, ["documentHead", "documentBody"])
|
||||
shouldPrintEndComments(node)
|
||||
? align(
|
||||
node.type === "sequenceItem" ? 2 : 0,
|
||||
concat([
|
||||
@@ -153,6 +152,7 @@ function genericPrint(path, options, print) {
|
||||
])
|
||||
)
|
||||
: "",
|
||||
nextEmptyLine,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -703,6 +703,12 @@ function needsSpaceInFrontOfMappingValue(node) {
|
||||
return node.key.content && node.key.content.type === "alias";
|
||||
}
|
||||
|
||||
function shouldPrintEndComments(node) {
|
||||
return (
|
||||
hasEndComments(node) && !isNode(node, ["documentHead", "documentBody"])
|
||||
);
|
||||
}
|
||||
|
||||
function printNextEmptyLine(path, originalText) {
|
||||
const node = path.getValue();
|
||||
const root = path.stack[0];
|
||||
@@ -713,7 +719,9 @@ function printNextEmptyLine(path, originalText) {
|
||||
if (!root.isNextEmptyLinePrintedChecklist[node.position.end.line]) {
|
||||
if (isNextLineEmpty(node, originalText)) {
|
||||
root.isNextEmptyLinePrintedChecklist[node.position.end.line] = true;
|
||||
return softline;
|
||||
if (!shouldPrintEndComments(path.getParentNode())) {
|
||||
return softline;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -137,6 +137,44 @@ printWidth: 80
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`issue-9130.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
- foo: 0
|
||||
bar: 1
|
||||
|
||||
# baz: 2
|
||||
- quux: 3
|
||||
|
||||
- foo: 0
|
||||
bar: 1
|
||||
|
||||
# baz: 2
|
||||
|
||||
# baz: 3
|
||||
- quux: 3
|
||||
|
||||
=====================================output=====================================
|
||||
- foo: 0
|
||||
bar: 1
|
||||
|
||||
# baz: 2
|
||||
- quux: 3
|
||||
|
||||
- foo: 0
|
||||
bar: 1
|
||||
|
||||
# baz: 2
|
||||
|
||||
# baz: 3
|
||||
- quux: 3
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`map.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
@@ -282,6 +320,69 @@ foo2:
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`map-4.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
before:
|
||||
|
||||
# before.comment
|
||||
after:
|
||||
# after.comment
|
||||
|
||||
before-after:
|
||||
|
||||
# before-after.comment
|
||||
|
||||
none:
|
||||
# none.comment
|
||||
before(2 line):
|
||||
|
||||
|
||||
# before.comment
|
||||
after(2 line):
|
||||
# after.comment
|
||||
|
||||
|
||||
before-after(2 line):
|
||||
|
||||
|
||||
# before-after.comment
|
||||
|
||||
|
||||
none(2):
|
||||
# none.comment
|
||||
=====================================output=====================================
|
||||
before:
|
||||
|
||||
# before.comment
|
||||
after:
|
||||
# after.comment
|
||||
|
||||
before-after:
|
||||
|
||||
# before-after.comment
|
||||
|
||||
none:
|
||||
# none.comment
|
||||
before(2 line):
|
||||
|
||||
# before.comment
|
||||
after(2 line):
|
||||
# after.comment
|
||||
|
||||
before-after(2 line):
|
||||
|
||||
# before-after.comment
|
||||
|
||||
none(2):
|
||||
# none.comment
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`object.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
@@ -336,6 +437,107 @@ printWidth: 80
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`sequence.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
- - a
|
||||
|
||||
# - b
|
||||
|
||||
# - c
|
||||
|
||||
- e
|
||||
|
||||
- - a
|
||||
|
||||
# - b
|
||||
|
||||
# - c
|
||||
|
||||
=====================================output=====================================
|
||||
- - a
|
||||
|
||||
# - b
|
||||
|
||||
# - c
|
||||
|
||||
- e
|
||||
|
||||
- - a
|
||||
|
||||
# - b
|
||||
|
||||
# - c
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`sequence-2.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
- before
|
||||
|
||||
# before.comment
|
||||
- after
|
||||
# after.comment
|
||||
|
||||
- before-after
|
||||
|
||||
# before-after.comment
|
||||
|
||||
- none:
|
||||
# none.comment
|
||||
- before(2 line)
|
||||
|
||||
|
||||
# before.comment
|
||||
- after(2 line)
|
||||
# after.comment
|
||||
|
||||
|
||||
- before-after(2 line)
|
||||
|
||||
|
||||
# before-after.comment
|
||||
|
||||
|
||||
- none(2)
|
||||
# none.comment
|
||||
=====================================output=====================================
|
||||
- before
|
||||
|
||||
# before.comment
|
||||
- after
|
||||
# after.comment
|
||||
|
||||
- before-after
|
||||
|
||||
# before-after.comment
|
||||
|
||||
- none:
|
||||
# none.comment
|
||||
- before(2 line)
|
||||
|
||||
# before.comment
|
||||
- after(2 line)
|
||||
# after.comment
|
||||
|
||||
- before-after(2 line)
|
||||
|
||||
# before-after.comment
|
||||
|
||||
- none(2)
|
||||
# none.comment
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`set.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
- foo: 0
|
||||
bar: 1
|
||||
|
||||
# baz: 2
|
||||
- quux: 3
|
||||
|
||||
- foo: 0
|
||||
bar: 1
|
||||
|
||||
# baz: 2
|
||||
|
||||
# baz: 3
|
||||
- quux: 3
|
||||
@@ -0,0 +1,28 @@
|
||||
before:
|
||||
|
||||
# before.comment
|
||||
after:
|
||||
# after.comment
|
||||
|
||||
before-after:
|
||||
|
||||
# before-after.comment
|
||||
|
||||
none:
|
||||
# none.comment
|
||||
before(2 line):
|
||||
|
||||
|
||||
# before.comment
|
||||
after(2 line):
|
||||
# after.comment
|
||||
|
||||
|
||||
before-after(2 line):
|
||||
|
||||
|
||||
# before-after.comment
|
||||
|
||||
|
||||
none(2):
|
||||
# none.comment
|
||||
@@ -0,0 +1,28 @@
|
||||
- before
|
||||
|
||||
# before.comment
|
||||
- after
|
||||
# after.comment
|
||||
|
||||
- before-after
|
||||
|
||||
# before-after.comment
|
||||
|
||||
- none:
|
||||
# none.comment
|
||||
- before(2 line)
|
||||
|
||||
|
||||
# before.comment
|
||||
- after(2 line)
|
||||
# after.comment
|
||||
|
||||
|
||||
- before-after(2 line)
|
||||
|
||||
|
||||
# before-after.comment
|
||||
|
||||
|
||||
- none(2)
|
||||
# none.comment
|
||||
@@ -0,0 +1,13 @@
|
||||
- - a
|
||||
|
||||
# - b
|
||||
|
||||
# - c
|
||||
|
||||
- e
|
||||
|
||||
- - a
|
||||
|
||||
# - b
|
||||
|
||||
# - c
|
||||
Reference in new issue
Block a user