From 6ba6138d09ab742c08184535f47bc7a5c1dfcbba Mon Sep 17 00:00:00 2001 From: Ika Date: Sun, 12 Aug 2018 13:38:14 +0800 Subject: [PATCH] feat(yaml): prefer implicit key for empty value (#4972) * feat(yaml): prefer implicit key for empty value * fix: prefer `?` for `!!set` * refactor --- src/language-yaml/printer-yaml.js | 16 +++++++++++++++- .../__snapshots__/jsfmt.spec.js.snap | 2 +- tests/yaml_root/__snapshots__/jsfmt.spec.js.snap | 2 +- tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap | 16 ++++++++-------- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index 2762b084..d2e403c1 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -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") { diff --git a/tests/yaml_comment/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_comment/__snapshots__/jsfmt.spec.js.snap index 4a751f37..7ed7f64d 100644 --- a/tests/yaml_comment/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_comment/__snapshots__/jsfmt.spec.js.snap @@ -88,7 +88,7 @@ a: #a A: - ? B + B: #A #A diff --git a/tests/yaml_root/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_root/__snapshots__/jsfmt.spec.js.snap index 3da71668..ba59a3bf 100644 --- a/tests/yaml_root/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_root/__snapshots__/jsfmt.spec.js.snap @@ -382,7 +382,7 @@ merge: null: # This mapping has four keys, # one has a value. - ? empty + empty: canonical: ~ english: null ~: null key diff --git a/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap index b9f97fd5..816e9061 100644 --- a/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap @@ -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 `;