From 85e5cfaf2fcd6477fcee77e5290d3f8fb2f91ac0 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 17 Nov 2020 21:38:50 +0800 Subject: [PATCH] YAML: Fix comments in `flowMapping` and `flowSequence` (#9669) --- changelog_unreleased/yaml/pr-9669.md | 60 ++++++++++++++ src/language-yaml/printer-yaml.js | 14 +++- .../comment/__snapshots__/jsfmt.spec.js.snap | 78 +++++++++++++++++++ tests/yaml/comment/flow-sequence-mapping.yml | 33 ++++++++ 4 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/yaml/pr-9669.md create mode 100644 tests/yaml/comment/flow-sequence-mapping.yml diff --git a/changelog_unreleased/yaml/pr-9669.md b/changelog_unreleased/yaml/pr-9669.md new file mode 100644 index 000000000..70aa1fba0 --- /dev/null +++ b/changelog_unreleased/yaml/pr-9669.md @@ -0,0 +1,60 @@ +#### Fix comments inside `flowMapping` and `flowSequence` (#9669 by @fisker) + + +```yaml +# Input +a: + [ + a, b, + # comment + ] + +b: + # prettier-ignore + { + a: 1, b: 2, + # comment + } + +# Prettier stable +a: [a, b] + # comment + +b: + # prettier-ignore + { + a: 1, b: 2, + # comment + } + # comment + + +# Prettier stable (second format) +a: + [a, b] + # comment + +b: + # prettier-ignore + { + a: 1, b: 2, + # comment + } + # comment + # comment + + +# Prettier master +a: [ + a, + b, + # comment + ] + +b: + # prettier-ignore + { + a: 1, b: 2, + # comment + } +``` diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index fa219a50e..fa13a5081 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -527,6 +527,12 @@ function _print(node, parentNode, path, options, print) { ) ), ifBreak(",", ""), + hasEndComments(node) + ? concat([ + hardline, + join(hardline, path.map(print, "endComments")), + ]) + : "", ]) ), isLastItemEmptyMappingItem ? "" : bracketSpacing, @@ -703,7 +709,13 @@ function needsSpaceInFrontOfMappingValue(node) { function shouldPrintEndComments(node) { return ( - hasEndComments(node) && !isNode(node, ["documentHead", "documentBody"]) + hasEndComments(node) && + !isNode(node, [ + "documentHead", + "documentBody", + "flowMapping", + "flowSequence", + ]) ); } diff --git a/tests/yaml/comment/__snapshots__/jsfmt.spec.js.snap b/tests/yaml/comment/__snapshots__/jsfmt.spec.js.snap index 9e899d762..40c5720ed 100644 --- a/tests/yaml/comment/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml/comment/__snapshots__/jsfmt.spec.js.snap @@ -108,6 +108,84 @@ A: ================================================================================ `; +exports[`flow-sequence-mapping.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== + a: + [ + check-format, check-lint, + check-spelling, + # coverage, + # install-and-run-from-git, + ] + + b: + { + a: check-format, b: check-lint, + c: check-spelling, + # d: coverage, + # e: install-and-run-from-git, + } + + d: + # prettier-ignore + [ + check-format, check-lint, + check-spelling, + # coverage, + # install-and-run-from-git, + ] + + e: + # prettier-ignore + { + a: check-format, b: check-lint, + c: check-spelling, + # d: coverage, + # e: install-and-run-from-git, + } + +=====================================output===================================== +a: [ + check-format, + check-lint, + check-spelling, + # coverage, + # install-and-run-from-git, + ] + +b: { + a: check-format, + b: check-lint, + c: check-spelling, + # d: coverage, + # e: install-and-run-from-git, + } + +d: + # prettier-ignore + [ + check-format, check-lint, + check-spelling, + # coverage, + # install-and-run-from-git, + ] + +e: + # prettier-ignore + { + a: check-format, b: check-lint, + c: check-spelling, + # d: coverage, + # e: install-and-run-from-git, + } + +================================================================================ +`; + exports[`in-empty-item-without-newlline.yml format 1`] = ` ====================================options===================================== parsers: ["yaml"] diff --git a/tests/yaml/comment/flow-sequence-mapping.yml b/tests/yaml/comment/flow-sequence-mapping.yml new file mode 100644 index 000000000..6a2e1d952 --- /dev/null +++ b/tests/yaml/comment/flow-sequence-mapping.yml @@ -0,0 +1,33 @@ + a: + [ + check-format, check-lint, + check-spelling, + # coverage, + # install-and-run-from-git, + ] + + b: + { + a: check-format, b: check-lint, + c: check-spelling, + # d: coverage, + # e: install-and-run-from-git, + } + + d: + # prettier-ignore + [ + check-format, check-lint, + check-spelling, + # coverage, + # install-and-run-from-git, + ] + + e: + # prettier-ignore + { + a: check-format, b: check-lint, + c: check-spelling, + # d: coverage, + # e: install-and-run-from-git, + }