YAML: Preserve blank lines in comments (#8356)
* YAML: Fix ugly comments * Add changelog * Use common utils
This commit is contained in:
1 parent
5da4c21219
commit
ea84392af2
6 files changed
+277
-2
No files matched your search
@@ -0,0 +1,54 @@
|
||||
#### Preserve blank lines in comments ([#8356](https://github.com/prettier/prettier/pull/8356) by [@fisker](https://github.com/fisker))
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```yaml
|
||||
# Input
|
||||
a:
|
||||
- a: a
|
||||
|
||||
# - b: b
|
||||
|
||||
# - c: c
|
||||
|
||||
- d: d
|
||||
|
||||
|
||||
b:
|
||||
- a: a
|
||||
|
||||
# - b: b
|
||||
|
||||
# - c: c
|
||||
|
||||
# Prettier stable
|
||||
a:
|
||||
- a: a
|
||||
|
||||
# - b: b
|
||||
|
||||
# - c: c
|
||||
|
||||
- d: d
|
||||
|
||||
b:
|
||||
- a: a
|
||||
# - b: b
|
||||
# - c: c
|
||||
|
||||
// Prettier master
|
||||
a:
|
||||
- a: a
|
||||
|
||||
# - b: b
|
||||
|
||||
# - c: c
|
||||
|
||||
- d: d
|
||||
|
||||
b:
|
||||
- a: a
|
||||
|
||||
# - b: b
|
||||
|
||||
# - c: c
|
||||
```
|
||||
@@ -38,7 +38,7 @@ const {
|
||||
markAsRoot,
|
||||
softline,
|
||||
} = docBuilders;
|
||||
const { replaceEndOfLineWith } = require("../common/util");
|
||||
const { replaceEndOfLineWith, isPreviousLineEmpty } = require("../common/util");
|
||||
|
||||
function preprocess(ast) {
|
||||
return mapNode(ast, defineShortcuts);
|
||||
@@ -131,7 +131,26 @@ function genericPrint(path, options, print) {
|
||||
hasEndComments(node) && !isNode(node, ["documentHead", "documentBody"])
|
||||
? align(
|
||||
node.type === "sequenceItem" ? 2 : 0,
|
||||
concat([hardline, join(hardline, path.map(print, "endComments"))])
|
||||
concat([
|
||||
hardline,
|
||||
join(
|
||||
hardline,
|
||||
path.map(
|
||||
(path) =>
|
||||
concat([
|
||||
isPreviousLineEmpty(
|
||||
options.originalText,
|
||||
path.getValue(),
|
||||
options.locStart
|
||||
)
|
||||
? hardline
|
||||
: "",
|
||||
print(path),
|
||||
]),
|
||||
"endComments"
|
||||
)
|
||||
),
|
||||
])
|
||||
)
|
||||
: "",
|
||||
]);
|
||||
|
||||
@@ -123,6 +123,151 @@ a:
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`map.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
foo1:
|
||||
- foo: item1
|
||||
bar: item1
|
||||
|
||||
# - foo: item2
|
||||
# bar: item2
|
||||
|
||||
# - foo: item3
|
||||
# bar: item3
|
||||
|
||||
- foo: item4
|
||||
bar: item4
|
||||
|
||||
foo2:
|
||||
- foo: item11
|
||||
bar: item11
|
||||
|
||||
# - foo: item22
|
||||
# bar: item22
|
||||
|
||||
# - foo: item33
|
||||
# bar: item33
|
||||
|
||||
=====================================output=====================================
|
||||
foo1:
|
||||
- foo: item1
|
||||
bar: item1
|
||||
|
||||
# - foo: item2
|
||||
# bar: item2
|
||||
|
||||
# - foo: item3
|
||||
# bar: item3
|
||||
|
||||
- foo: item4
|
||||
bar: item4
|
||||
|
||||
foo2:
|
||||
- foo: item11
|
||||
bar: item11
|
||||
|
||||
# - foo: item22
|
||||
# bar: item22
|
||||
|
||||
# - foo: item33
|
||||
# bar: item33
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`map-2.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
foo1:
|
||||
- foo
|
||||
|
||||
# - foo
|
||||
|
||||
# - foo
|
||||
|
||||
- foo
|
||||
|
||||
foo2:
|
||||
- foo2
|
||||
|
||||
# - foo2
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# - foo2
|
||||
# - foo2
|
||||
|
||||
=====================================output=====================================
|
||||
foo1:
|
||||
- foo
|
||||
|
||||
# - foo
|
||||
|
||||
# - foo
|
||||
|
||||
- foo
|
||||
|
||||
foo2:
|
||||
- foo2
|
||||
|
||||
# - foo2
|
||||
|
||||
# - foo2
|
||||
# - foo2
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`map-3.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
foo1:
|
||||
- foo
|
||||
|
||||
# - foo
|
||||
|
||||
# - foo
|
||||
|
||||
- foo
|
||||
|
||||
foo2:
|
||||
- foo2
|
||||
|
||||
# first line
|
||||
# next line
|
||||
|
||||
=====================================output=====================================
|
||||
foo1:
|
||||
- foo
|
||||
|
||||
# - foo
|
||||
|
||||
# - foo
|
||||
|
||||
- foo
|
||||
|
||||
foo2:
|
||||
- foo2
|
||||
|
||||
# first line
|
||||
# next line
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`root.yml format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["yaml"]
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
foo1:
|
||||
- foo
|
||||
|
||||
# - foo
|
||||
|
||||
# - foo
|
||||
|
||||
- foo
|
||||
|
||||
foo2:
|
||||
- foo2
|
||||
|
||||
# - foo2
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# - foo2
|
||||
# - foo2
|
||||
@@ -0,0 +1,14 @@
|
||||
foo1:
|
||||
- foo
|
||||
|
||||
# - foo
|
||||
|
||||
# - foo
|
||||
|
||||
- foo
|
||||
|
||||
foo2:
|
||||
- foo2
|
||||
|
||||
# first line
|
||||
# next line
|
||||
@@ -0,0 +1,22 @@
|
||||
foo1:
|
||||
- foo: item1
|
||||
bar: item1
|
||||
|
||||
# - foo: item2
|
||||
# bar: item2
|
||||
|
||||
# - foo: item3
|
||||
# bar: item3
|
||||
|
||||
- foo: item4
|
||||
bar: item4
|
||||
|
||||
foo2:
|
||||
- foo: item11
|
||||
bar: item11
|
||||
|
||||
# - foo: item22
|
||||
# bar: item22
|
||||
|
||||
# - foo: item33
|
||||
# bar: item33
|
||||
Reference in new issue
Block a user