feat(yaml): prefer implicit key for empty value (#4972)

* feat(yaml): prefer implicit key for empty value

* fix: prefer `?` for `!!set`

* refactor
master
Ika 2018-08-12 13:38:14 +08:00 committed by GitHub
parent 82a12a58a8
commit 6ba6138d09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 11 deletions

View File

@ -323,7 +323,21 @@ function _print(node, parentNode, path, options, print) {
return node.type === "flowMappingItem" &&
path.getParentNode().type !== "flowSequence"
? key
: concat(["? ", align(2, key)]);
: node.type === "mappingItem" &&
node.key.type !== "null" &&
isAbsolutelyPrintedAsSingleLineNode(node.key.node, options) &&
!hasTrailingComments(node.key.node) &&
!(
parentNode.tag.type === "shorthandTag" &&
parentNode.tag.handle === "!!" &&
parentNode.tag.suffix === "set"
)
? concat([
key,
needsSpaceInFrontOfMappingValue(node) ? " " : "",
":"
])
: concat(["? ", align(2, key)]);
}
if (node.key.type === "null") {

View File

@ -88,7 +88,7 @@ a:
#a
A:
? B
B:
#A
#A

View File

@ -382,7 +382,7 @@ merge:
null:
# This mapping has four keys,
# one has a value.
? empty
empty:
canonical: ~
english: null
~: null key

View File

@ -349,9 +349,9 @@ exports[`block-mapping-with-missing-values.yml - yaml-verify 1`] = `
? b
c:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
? a
? b
? c
a:
b:
c:
`;
@ -360,9 +360,9 @@ exports[`block-mapping-with-missing-values.yml - yaml-verify 2`] = `
? b
c:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
? a
? b
? c
a:
b:
c:
`;
@ -954,7 +954,7 @@ a: 1
&anchor c: 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a: 1
? b
b:
&anchor c: 3
`;
@ -966,7 +966,7 @@ a: 1
&anchor c: 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a: 1
? b
b:
&anchor c: 3
`;