From cb7fe008635689240eb42ceab878de9692fae92a Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 18 Jul 2018 09:37:53 +0800 Subject: [PATCH] fix(yaml): support end comments (#4854) --- package.json | 4 +- src/language-yaml/parser-yaml.js | 3 +- src/language-yaml/printer-yaml.js | 19 ++++-- src/language-yaml/utils.js | 5 ++ .../__snapshots__/jsfmt.spec.js.snap | 64 ++++++++++++++++++- tests/yaml_comment/collection.yml | 29 +++++++++ .../__snapshots__/jsfmt.spec.js.snap | 14 ---- ...ing-with-anchor-on-document-start-line.yml | 1 - yarn.lock | 12 ++-- 9 files changed, 120 insertions(+), 31 deletions(-) create mode 100644 tests/yaml_comment/collection.yml delete mode 100644 tests/yaml_spec/mapping-with-anchor-on-document-start-line.yml diff --git a/package.json b/package.json index 88ea05d6..345cb504 100644 --- a/package.json +++ b/package.json @@ -60,8 +60,8 @@ "typescript-eslint-parser": "eslint/typescript-eslint-parser#6eec85b1466fbef087838203b246f70fa71ac0ac", "unicode-regex": "1.0.1", "unified": "6.1.6", - "yaml": "1.0.0-rc.4", - "yaml-unist-parser": "1.0.0-rc.1" + "yaml": "1.0.0-rc.7", + "yaml-unist-parser": "1.0.0-rc.2" }, "devDependencies": { "@babel/cli": "7.0.0-beta.49", diff --git a/src/language-yaml/parser-yaml.js b/src/language-yaml/parser-yaml.js index b679d60d..e4b0271e 100644 --- a/src/language-yaml/parser-yaml.js +++ b/src/language-yaml/parser-yaml.js @@ -32,7 +32,8 @@ function parse(text) { (node.type === "mappingKey" || node.type === "mappingValue") && node.children[0].type === "null" && node.leadingComments.length === 0 && - node.trailingComments.length === 0 + node.trailingComments.length === 0 && + node.endComments.length === 0 ) { return createNull(); } diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index 8b8d9017..ea69da10 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -11,6 +11,7 @@ const { hasLeadingComments, hasMiddleComments, hasTrailingComments, + hasEndComments, hasPrettierIgnore, isLastDescendantNode, isNextLineEmpty, @@ -100,7 +101,13 @@ function genericPrint(path, options, print) { ]) ) : "", - nextEmptyLine + nextEmptyLine, + hasEndComments(node) + ? (endComments => + node.type === "sequenceItem" ? align(2, endComments) : endComments)( + concat([hardline, join(hardline, path.map(print, "endComments"))]) + ) + : "" ]); } @@ -432,12 +439,12 @@ function _print(node, parentNode, path, options, print) { function indent(doc) { return docBuilders.align(" ".repeat(options.tabWidth), doc); } +} - function align(n, doc) { - return typeof n === "number" && n > 0 - ? docBuilders.align(" ".repeat(n), doc) - : docBuilders.align(n, doc); - } +function align(n, doc) { + return typeof n === "number" && n > 0 + ? docBuilders.align(" ".repeat(n), doc) + : docBuilders.align(n, doc); } function isInlineNode(node) { diff --git a/src/language-yaml/utils.js b/src/language-yaml/utils.js index 0dd85e6d..e0cf6c8c 100644 --- a/src/language-yaml/utils.js +++ b/src/language-yaml/utils.js @@ -161,6 +161,10 @@ function hasTrailingComments(node) { return "trailingComments" in node && node.trailingComments.length !== 0; } +function hasEndComments(node) { + return "endComments" in node && node.endComments.length !== 0; +} + /** * " a b c d e f " -> [" a b", "c d", "e f "] */ @@ -346,5 +350,6 @@ module.exports = { hasLeadingComments, hasMiddleComments, hasTrailingComments, + hasEndComments, hasExplicitDocumentEndMarker }; diff --git a/tests/yaml_comment/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_comment/__snapshots__/jsfmt.spec.js.snap index 1c4f4795..d8bb4438 100644 --- a/tests/yaml_comment/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_comment/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,67 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`collection.yml - yaml-verify 1`] = ` +a: + 123 + # impicitMappginValue + +? b + # explicitMappingKey +: c + # explicitMappingValue + +d: + - 123 + # sequence + +e: + - 123 + # sequenceItem + +f: + - a + # b.leadingComments + - b + # b.endComments + - c + # c.endComments + # sequence.endComments +# documentBody.children + +empty_content: + # hello world +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +a: 123 + # impicitMappginValue + +? b + # explicitMappingKey +: c + # explicitMappingValue + +d: + - 123 + # sequence + +e: + - 123 + # sequenceItem + +f: + - a + # b.leadingComments + - b + # b.endComments + - c + # c.endComments + # sequence.endComments +# documentBody.children + +empty_content: + # hello world + +`; + exports[`root.yml - yaml-verify 1`] = ` #hello world ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -12,6 +74,6 @@ exports[`set.yml - yaml-verify 1`] = ` # 456 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 123 -# 456 + # 456 `; diff --git a/tests/yaml_comment/collection.yml b/tests/yaml_comment/collection.yml new file mode 100644 index 00000000..c93a0832 --- /dev/null +++ b/tests/yaml_comment/collection.yml @@ -0,0 +1,29 @@ +a: + 123 + # impicitMappginValue + +? b + # explicitMappingKey +: c + # explicitMappingValue + +d: + - 123 + # sequence + +e: + - 123 + # sequenceItem + +f: + - a + # b.leadingComments + - b + # b.endComments + - c + # c.endComments + # sequence.endComments +# documentBody.children + +empty_content: + # hello world diff --git a/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap index 367dc9cd..7720b221 100644 --- a/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap @@ -1061,20 +1061,6 @@ exports[`mapping-key-and-flow-sequence-item-anchors.yml - yaml-verify 2`] = ` `; -exports[`mapping-with-anchor-on-document-start-line.yml - yaml-verify 1`] = ` ---- &anchor a: b -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -&anchor a: b - -`; - -exports[`mapping-with-anchor-on-document-start-line.yml - yaml-verify 2`] = ` ---- &anchor a: b -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -&anchor a: b - -`; - exports[`mixed-block-mapping-explicit-to-implicit.yml - yaml-verify 1`] = ` ? a : 13 diff --git a/tests/yaml_spec/mapping-with-anchor-on-document-start-line.yml b/tests/yaml_spec/mapping-with-anchor-on-document-start-line.yml deleted file mode 100644 index 9c001207..00000000 --- a/tests/yaml_spec/mapping-with-anchor-on-document-start-line.yml +++ /dev/null @@ -1 +0,0 @@ ---- &anchor a: b diff --git a/yarn.lock b/yarn.lock index 7e90e86e..1407a410 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6008,16 +6008,16 @@ yallist@^3.0.0, yallist@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9" -yaml-unist-parser@1.0.0-rc.1: - version "1.0.0-rc.1" - resolved "https://registry.yarnpkg.com/yaml-unist-parser/-/yaml-unist-parser-1.0.0-rc.1.tgz#c70d0642c800e83535fbf6984a41b38b4d9c777e" +yaml-unist-parser@1.0.0-rc.2: + version "1.0.0-rc.2" + resolved "https://registry.yarnpkg.com/yaml-unist-parser/-/yaml-unist-parser-1.0.0-rc.2.tgz#a9c3597fe8507d7b80c7c51fc1c1ae4773ccdd30" dependencies: lines-and-columns "^1.1.6" tslib "^1.9.1" -yaml@1.0.0-rc.4: - version "1.0.0-rc.4" - resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.0.0-rc.4.tgz#850dc77d9b03975b5e5ea8a9b37cde252a8f1f0b" +yaml@1.0.0-rc.7: + version "1.0.0-rc.7" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.0.0-rc.7.tgz#7cf9dba3c78992542b7a2d7cb9a7eeacbff63f77" yargs-parser@^7.0.0: version "7.0.0"